forked from bobvanderlinden/sharpfilesystem
-
Notifications
You must be signed in to change notification settings - Fork 6
/
NetZipArchiveFileSystem.cs
118 lines (100 loc) · 3.84 KB
/
NetZipArchiveFileSystem.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.IO.Compression;
namespace SharpFileSystem.FileSystems
{
public class NetZipArchiveFileSystem : AbstractFileSystem
{
public ZipArchive ZipArchive { get; private set; }
public override bool IsReadOnly => false;
public static NetZipArchiveFileSystem Open(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Update, true));
}
public static NetZipArchiveFileSystem OpenReadOnly(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Read, true));
}
public static NetZipArchiveFileSystem Create(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Create, true));
}
private NetZipArchiveFileSystem(ZipArchive archive)
{
ZipArchive = archive;
}
public override void Dispose()
{
ZipArchive.Dispose();
}
protected IEnumerable<ZipArchiveEntry> GetZipEntries()
{
return ZipArchive.Entries;
}
protected FileSystemPath ToPath(ZipArchiveEntry entry)
{
return FileSystemPath.Parse(FileSystemPath.DirectorySeparator + entry.FullName);
}
protected string ToEntryPath(FileSystemPath path)
{
// Remove heading '/' from path.
string rootedPath = path;
if (!Root.IsRoot)
{
rootedPath = Path.Combine(Root, path);
}
return path.Path.TrimStart(FileSystemPath.DirectorySeparator);
}
protected ZipArchiveEntry ToEntry(FileSystemPath path)
{
return ZipArchive.GetEntry(ToEntryPath(path));
}
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
return GetZipEntries().Select(ToPath).Where(path.IsParentOf)
.Select(entryPath => entryPath.ParentPath == path
? entryPath
: path.AppendDirectory(entryPath.RemoveParent(path).GetDirectorySegments()[0])
)
.Distinct()
.ToList();
}
public override bool Exists(FileSystemPath path)
{
if (path.IsFile)
return ToEntry(path) != null;
return GetZipEntries()
.Select(ToPath)
.Any(entryPath => entryPath.IsChildOf(path) || entryPath.Equals(path));
}
public override Stream CreateFile(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
var zae = ZipArchive.CreateEntry(ToEntryPath(path));
return zae.Open();
}
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (ZipArchive.Mode == ZipArchiveMode.Read && access != FileAccess.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
var zae = ZipArchive.GetEntry(ToEntryPath(path));
return zae.Open();
}
public override void CreateDirectory(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
ZipArchive.CreateEntry(ToEntryPath(path));
}
public override void Delete(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
var zae = ZipArchive.GetEntry(ToEntryPath(path));
zae.Delete();
}
}
}