-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFileSystemTests.cs
More file actions
190 lines (160 loc) · 5.59 KB
/
ZipFileSystemTests.cs
File metadata and controls
190 lines (160 loc) · 5.59 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.IO.Compression;
using Ramstack.FileSystem.Specification.Tests;
using Ramstack.FileSystem.Specification.Tests.Utilities;
#pragma warning disable CS0618 // Type or member is obsolete
namespace Ramstack.FileSystem.Zip;
[TestFixture]
public class ZipFileSystemTests : VirtualFileSystemSpecificationTests
{
private readonly string _path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
private readonly TempFileStorage _storage = new();
[OneTimeSetUp]
public void Setup()
{
ZipFile.CreateFromDirectory(_storage.Root, _path, CompressionLevel.Optimal, includeBaseDirectory: false);
}
[OneTimeTearDown]
public void Cleanup()
{
_storage.Dispose();
File.Delete(_path);
}
[Test]
public async Task ZipArchive_WithIdenticalNameEntries()
{
using var fs = new ZipFileSystem(CreateArchive());
var list = await fs
.GetFilesAsync("/1")
.ToArrayAsync();
Assert.That(
list.Length,
Is.EqualTo(1));
Assert.That(
await list[0].ReadAllBytesAsync(),
Is.EquivalentTo("Hello, World!"u8.ToArray()));
static MemoryStream CreateArchive()
{
var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
var a = archive.CreateEntry("1/text.txt");
using (var writer = a.Open())
writer.Write("Hello, World!"u8);
archive.CreateEntry("1/text.txt");
archive.CreateEntry(@"1\text.txt");
}
stream.Position = 0;
return stream;
}
}
[Test]
public async Task ZipArchive_PrefixedEntries()
{
var archive = new ZipArchive(CreateArchive(), ZipArchiveMode.Read, leaveOpen: true);
using var fs = new ZipFileSystem(archive);
var directories = await fs
.GetDirectoriesAsync("/", "**")
.Select(f =>
f.FullName)
.OrderBy(f => f)
.ToArrayAsync();
var files = await fs
.GetFilesAsync("/", "**")
.Select(f =>
f.FullName)
.OrderBy(f => f)
.ToArrayAsync();
Assert.That(files, Is.EquivalentTo(
[
"/1/text.txt",
"/2/text.txt",
"/3/text.txt",
"/4/text.txt",
"/5/text.txt",
"/localhost/backup/text.txt",
"/localhost/share/text.txt",
"/server/backup/text.txt",
"/server/share/text.txt",
"/text.txt",
"/text.xml"
]));
Assert.That(directories, Is.EquivalentTo(
[
"/1",
"/2",
"/3",
"/4",
"/5",
"/localhost",
"/localhost/backup",
"/localhost/share",
"/server",
"/server/backup",
"/server/share"
]));
static MemoryStream CreateArchive()
{
var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
archive.CreateEntry(@"D:\1/text.txt");
archive.CreateEntry(@"D:2\text.txt");
archive.CreateEntry(@"\\?\D:\text.txt");
archive.CreateEntry(@"\\?\D:text.xml");
archive.CreateEntry(@"\\.\D:\3\text.txt");
archive.CreateEntry(@"//?/D:/4\text.txt");
archive.CreateEntry(@"//./D:\5/text.txt");
archive.CreateEntry(@"\\?\UNC\localhost\share\text.txt");
archive.CreateEntry(@"\\.\unc\server\share\text.txt");
archive.CreateEntry(@"//?/UNC/localhost/backup\text.txt");
archive.CreateEntry(@"//./unc/server/backup\text.txt");
}
stream.Position = 0;
return stream;
}
}
[Test]
public async Task ZipArchive_Directories()
{
using var fs = new ZipFileSystem(CreateArchive());
var directories = await fs
.GetDirectoriesAsync("/", "**")
.Select(f =>
f.FullName)
.OrderBy(f => f)
.ToArrayAsync();
Assert.That(directories, Is.EquivalentTo(
[
"/1",
"/2",
"/2/3",
"/4",
"/4/5",
"/4/5/6"
]));
static MemoryStream CreateArchive()
{
var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
archive.CreateEntry(@"\1/");
archive.CreateEntry(@"\2/");
archive.CreateEntry(@"/2\");
archive.CreateEntry(@"/2\");
archive.CreateEntry(@"/2\");
archive.CreateEntry(@"/2\3/");
archive.CreateEntry(@"/2\3/");
archive.CreateEntry(@"/2\3/");
archive.CreateEntry(@"4\5/6\");
}
stream.Position = 0;
return stream;
}
}
/// <inheritdoc />
protected override IVirtualFileSystem GetFileSystem() =>
new ZipFileSystem(_path);
/// <inheritdoc />
protected override DirectoryInfo GetDirectoryInfo() =>
new(_storage.Root);
}