ProjectArcade
128 строк · 3.9 Кб
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using System.Threading;
7using EmulatorLauncher.Common.Compression;
8
9namespace Mount
10{
11class MountedFileEntry : FileEntry
12{
13private ZipEntry _entry;
14
15public MountedFileEntry(ZipEntry entry, DokanOperations fs)
16{
17_entry = entry;
18
19Filename = entry.Filename;
20IsDirectory = entry.IsDirectory;
21Attributes = (uint)FileAttributes.Normal; // entry.Attributes;
22FileSystem = fs;
23}
24
25public override long Length
26{
27get
28{
29if (_len >= 0)
30return _len;
31
32if (IsDirectory)
33_len = 0;
34else
35{
36if (File.Exists(PhysicalPath))
37_len = new FileInfo(PhysicalPath).Length;
38else
39_len = _entry.Length;
40}
41
42return _len;
43}
44}
45
46private long _len = -1;
47
48public override DateTime LastWriteTime { get { return _entry.LastModified; } }
49public override DateTime CreationTime { get { return _entry.LastModified; } }
50public override DateTime LastAccessTime { get { return _entry.LastModified; } }
51
52public DokanOperations FileSystem { get; set; }
53
54private object _lock = new object();
55
56private string _physicalPath;
57
58public override string PhysicalPath
59{
60get
61{
62if (_physicalPath == null)
63{
64string source = this.Filename;
65if (source.StartsWith("\\") || source.StartsWith("/"))
66source = source.Substring(1);
67
68_physicalPath = Path.Combine(FileSystem.ExtractionDirectory, source);
69}
70
71return _physicalPath;
72}
73}
74
75public bool Queryed { get; set; }
76
77public override Stream GetPhysicalFileStream(System.IO.FileAccess access = System.IO.FileAccess.Read)
78{
79Queryed = true;
80
81lock (_lock)
82{
83string physicalPath = PhysicalPath;
84if (File.Exists(physicalPath))
85{
86if (access != (System.IO.FileAccess)0)
87return new FileStream(physicalPath, FileMode.Open, access, FileShare.ReadWrite);
88
89return null;
90}
91else
92{
93string source = this.Filename;
94if (source.StartsWith("\\") || source.StartsWith("/"))
95source = source.Substring(1);
96
97var parent = Path.GetDirectoryName(physicalPath);
98if (!Directory.Exists(parent))
99Directory.CreateDirectory(parent);
100
101string tmpDirectory = Path.Combine(FileSystem.ExtractionDirectory, Guid.NewGuid().ToString());
102if (!Directory.Exists(tmpDirectory))
103Directory.CreateDirectory(tmpDirectory);
104
105string tmpFile = Path.GetFullPath(Path.Combine(tmpDirectory, source));
106
107Zip.Extract(FileSystem.FileName, tmpDirectory, source, null, true);
108
109bool exists = File.Exists(tmpFile);
110if (exists)
111File.Move(tmpFile, physicalPath);
112
113ThreadPool.QueueUserWorkItem((a) =>
114{
115try { Directory.Delete(tmpDirectory, true); }
116catch { }
117});
118
119if (exists && access != (System.IO.FileAccess)0)
120return new FileStream(physicalPath, FileMode.Open, access, FileShare.ReadWrite);
121
122}
123
124return null;
125}
126}
127}
128}
129