ProjectArcade

Форк
0
/
MountedFileEntry.cs 
128 строк · 3.9 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.IO;
6
using System.Threading;
7
using EmulatorLauncher.Common.Compression;
8

9
namespace Mount
10
{
11
    class MountedFileEntry : FileEntry
12
    {
13
        private ZipEntry _entry;
14

15
        public MountedFileEntry(ZipEntry entry, DokanOperations fs)
16
        {
17
            _entry = entry;
18

19
            Filename = entry.Filename;
20
            IsDirectory = entry.IsDirectory;
21
            Attributes = (uint)FileAttributes.Normal; // entry.Attributes;
22
            FileSystem = fs;
23
        }
24

25
        public override long Length
26
        {
27
            get
28
            {
29
                if (_len >= 0)
30
                    return _len;
31

32
                if (IsDirectory)
33
                    _len = 0;
34
                else
35
                {
36
                    if (File.Exists(PhysicalPath))
37
                        _len = new FileInfo(PhysicalPath).Length;
38
                    else
39
                        _len = _entry.Length;
40
                }
41

42
                return _len;
43
            }
44
        }
45

46
        private long _len = -1;
47

48
        public override DateTime LastWriteTime { get { return _entry.LastModified; } }
49
        public override DateTime CreationTime { get { return _entry.LastModified; } }
50
        public override DateTime LastAccessTime { get { return _entry.LastModified; } }
51

52
        public DokanOperations FileSystem { get; set; }
53

54
        private object _lock = new object();
55

56
        private string _physicalPath;
57

58
        public override string PhysicalPath
59
        {
60
            get
61
            {
62
                if (_physicalPath == null)
63
                {
64
                    string source = this.Filename;
65
                    if (source.StartsWith("\\") || source.StartsWith("/"))
66
                        source = source.Substring(1);
67

68
                    _physicalPath = Path.Combine(FileSystem.ExtractionDirectory, source);
69
                }
70

71
                return _physicalPath;
72
            }
73
        }
74

75
        public bool Queryed { get; set; }
76
        
77
        public override Stream GetPhysicalFileStream(System.IO.FileAccess access = System.IO.FileAccess.Read)
78
        {
79
            Queryed = true;
80

81
            lock (_lock)
82
            {
83
                string physicalPath = PhysicalPath;
84
                if (File.Exists(physicalPath))
85
                {
86
                    if (access != (System.IO.FileAccess)0)
87
                        return new FileStream(physicalPath, FileMode.Open, access, FileShare.ReadWrite);
88

89
                    return null;
90
                }
91
                else
92
                {
93
                    string source = this.Filename;
94
                    if (source.StartsWith("\\") || source.StartsWith("/"))
95
                        source = source.Substring(1);
96

97
                    var parent = Path.GetDirectoryName(physicalPath);
98
                    if (!Directory.Exists(parent))
99
                        Directory.CreateDirectory(parent);
100

101
                    string tmpDirectory = Path.Combine(FileSystem.ExtractionDirectory, Guid.NewGuid().ToString());
102
                    if (!Directory.Exists(tmpDirectory))
103
                        Directory.CreateDirectory(tmpDirectory);
104

105
                    string tmpFile = Path.GetFullPath(Path.Combine(tmpDirectory, source));
106

107
                    Zip.Extract(FileSystem.FileName, tmpDirectory, source, null, true);
108

109
                    bool exists = File.Exists(tmpFile);
110
                    if (exists)
111
                        File.Move(tmpFile, physicalPath);
112

113
                    ThreadPool.QueueUserWorkItem((a) =>
114
                    {
115
                        try { Directory.Delete(tmpDirectory, true); }
116
                        catch { }
117
                    });
118

119
                    if (exists && access != (System.IO.FileAccess)0)
120
                        return new FileStream(physicalPath, FileMode.Open, access, FileShare.ReadWrite);
121

122
                }
123
                
124
                return null;
125
            }
126
        }
127
    }
128
}
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.