-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathSharpZipLibFileSystem.cs
More file actions
136 lines (118 loc) · 3.74 KB
/
Copy pathSharpZipLibFileSystem.cs
File metadata and controls
136 lines (118 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.SharpZipLib.Zip;
using SharpFileSystem.FileSystems;
using SharpFileSystem.IO;
namespace SharpFileSystem.SharpZipLib
{
public class SharpZipLibFileSystem: IFileSystem
{
public ZipFile ZipFile { get; set; }
public static SharpZipLibFileSystem Open(Stream s)
{
return new SharpZipLibFileSystem(new ZipFile(s));
}
public static SharpZipLibFileSystem Create(Stream s)
{
return new SharpZipLibFileSystem(ZipFile.Create(s));
}
private SharpZipLibFileSystem(ZipFile zipFile)
{
ZipFile = zipFile;
}
public void Dispose()
{
if (ZipFile.IsUpdating)
ZipFile.CommitUpdate();
ZipFile.Close();
}
protected FileSystemPath ToPath(ZipEntry entry)
{
return FileSystemPath.Parse(FileSystemPath.DirectorySeparator + entry.Name);
}
protected string ToEntryPath(FileSystemPath path)
{
// Remove heading '/' from path.
return path.Path.TrimStart(FileSystemPath.DirectorySeparator);
}
protected ZipEntry ToEntry(FileSystemPath path)
{
return ZipFile.GetEntry(ToEntryPath(path));
}
protected IEnumerable<ZipEntry> GetZipEntries()
{
return ZipFile.Cast<ZipEntry>();
}
public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
return GetZipEntries()
.Select(ToPath)
.Where(entryPath => path.IsParentOf(entryPath))
.Select(entryPath => entryPath.ParentPath == path
? entryPath
: path.AppendDirectory(entryPath.RemoveParent(path).GetDirectorySegments()[0])
)
.Distinct()
.ToList();
}
public bool Exists(FileSystemPath path)
{
if (path.IsFile)
return ToEntry(path) != null;
return GetZipEntries()
.Select(ToPath)
.Any(entryPath => entryPath.IsChildOf(path));
}
public Stream CreateFile(FileSystemPath path)
{
return CreateFile(path,null);
}
public Stream CreateFile(FileSystemPath path, byte[] data)
{
BeginUpdate();
var entry = new MemoryZipEntry();
ZipFile.Add(entry, ToEntryPath(path));
var s = entry.GetSource();
if (data!=null) s.Write(data);
EndUpdate();
return s;
}
private void BeginUpdate()
{
if (!ZipFile.IsUpdating)
ZipFile.BeginUpdate();
}
private void EndUpdate()
{
if (ZipFile.IsUpdating)
ZipFile.CommitUpdate();
}
public Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (access != FileAccess.Read)
throw new NotSupportedException();
return ZipFile.GetInputStream(ToEntry(path));
}
public void CreateDirectory(FileSystemPath path)
{
BeginUpdate();
ZipFile.AddDirectory(ToEntryPath(path));
EndUpdate();
}
public void Delete(FileSystemPath path)
{
BeginUpdate();
ZipFile.Delete(ToEntryPath(path));
EndUpdate();
}
public class MemoryZipEntry: MemoryFileSystem.MemoryFile, IStaticDataSource
{
public Stream GetSource()
{
return new MemoryFileSystem.MemoryFileStream(this);
}
}
}
}