forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFile.Create.cs
More file actions
454 lines (403 loc) · 19.1 KB
/
ZipFile.Create.cs
File metadata and controls
454 lines (403 loc) · 19.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Compression.Tests
{
public class ZipFile_Create : ZipFileTestBase
{
[Fact]
public async Task CreateFromDirectoryNormal()
{
string folderName = zfolder("normal");
string noBaseDir = GetTestFilePath();
ZipFile.CreateFromDirectory(folderName, noBaseDir);
await IsZipSameAsDirAsync(noBaseDir, folderName, ZipArchiveMode.Read, requireExplicit: false, checkTimes: false);
}
[Fact]
public void CreateFromDirectory_IncludeBaseDirectory()
{
string folderName = zfolder("normal");
string withBaseDir = GetTestFilePath();
ZipFile.CreateFromDirectory(folderName, withBaseDir, CompressionLevel.Optimal, true);
IEnumerable<string> expected = Directory.EnumerateFiles(zfolder("normal"), "*", SearchOption.AllDirectories);
using (ZipArchive actual_withbasedir = ZipFile.Open(withBaseDir, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry actualEntry in actual_withbasedir.Entries)
{
string expectedFile = expected.Single(i => Path.GetFileName(i).Equals(actualEntry.Name));
Assert.StartsWith("normal", actualEntry.FullName);
Assert.Equal(new FileInfo(expectedFile).Length, actualEntry.Length);
using (Stream expectedStream = File.OpenRead(expectedFile))
using (Stream actualStream = actualEntry.Open())
{
StreamsEqual(expectedStream, actualStream);
}
}
}
}
[Fact]
public void CreateFromDirectoryUnicode()
{
string folderName = zfolder("unicode");
string noBaseDir = GetTestFilePath();
ZipFile.CreateFromDirectory(folderName, noBaseDir);
using (ZipArchive archive = ZipFile.OpenRead(noBaseDir))
{
IEnumerable<string> actual = archive.Entries.Select(entry => entry.Name);
IEnumerable<string> expected = Directory.EnumerateFileSystemEntries(zfolder("unicode"), "*", SearchOption.AllDirectories).ToList();
Assert.True(Enumerable.SequenceEqual(expected.Select(i => Path.GetFileName(i)), actual.Select(i => i)));
}
}
[Fact]
public void CreatedEmptyDirectoriesRoundtrip()
{
using (var tempFolder = new TempDirectory(GetTestFilePath()))
{
DirectoryInfo rootDir = new DirectoryInfo(tempFolder.Path);
rootDir.CreateSubdirectory("empty1");
string archivePath = GetTestFilePath();
ZipFile.CreateFromDirectory(
rootDir.FullName, archivePath,
CompressionLevel.Optimal, false, Encoding.UTF8);
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
{
Assert.Equal(1, archive.Entries.Count);
Assert.StartsWith("empty1", archive.Entries[0].FullName);
}
}
}
[Fact]
public void CreatedEmptyUtf32DirectoriesRoundtrip()
{
using (var tempFolder = new TempDirectory(GetTestFilePath()))
{
Encoding entryEncoding = Encoding.UTF32;
DirectoryInfo rootDir = new DirectoryInfo(tempFolder.Path);
rootDir.CreateSubdirectory("empty1");
string archivePath = GetTestFilePath();
ZipFile.CreateFromDirectory(
rootDir.FullName, archivePath,
CompressionLevel.Optimal, false, entryEncoding);
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Read, entryEncoding))
{
Assert.Equal(1, archive.Entries.Count);
Assert.StartsWith("empty1", archive.Entries[0].FullName);
}
}
}
[Fact]
public void CreatedEmptyRootDirectoryRoundtrips()
{
using (var tempFolder = new TempDirectory(GetTestFilePath()))
{
DirectoryInfo emptyRoot = new DirectoryInfo(tempFolder.Path);
string archivePath = GetTestFilePath();
ZipFile.CreateFromDirectory(
emptyRoot.FullName, archivePath,
CompressionLevel.Optimal, true);
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
{
Assert.Equal(1, archive.Entries.Count);
}
}
}
[Fact]
public void InvalidInstanceMethods()
{
using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath()))
using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update))
{
//non-existent entry
Assert.True(null == archive.GetEntry("nonExistentEntry"));
//null/empty string
Assert.Throws<ArgumentNullException>(() => archive.GetEntry(null));
ZipArchiveEntry entry = archive.GetEntry("first.txt");
//null/empty string
AssertExtensions.Throws<ArgumentException>("entryName", () => archive.CreateEntry(""));
Assert.Throws<ArgumentNullException>(() => archive.CreateEntry(null));
}
}
[Fact]
public void InvalidConstructors()
{
//out of range enum values
Assert.Throws<ArgumentOutOfRangeException>(() => ZipFile.Open("bad file", (ZipArchiveMode)(10)));
}
[Fact]
public void InvalidFiles()
{
Assert.Throws<InvalidDataException>(() => ZipFile.OpenRead(bad("EOCDmissing.zip")));
using (TempFile testArchive = CreateTempCopyFile(bad("EOCDmissing.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
Assert.Throws<InvalidDataException>(() => ZipFile.OpenRead(bad("CDoffsetOutOfBounds.zip")));
using (TempFile testArchive = CreateTempCopyFile(bad("CDoffsetOutOfBounds.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
using (ZipArchive archive = ZipFile.OpenRead(bad("CDoffsetInBoundsWrong.zip")))
{
Assert.Throws<InvalidDataException>(() => { var x = archive.Entries; });
}
using (TempFile testArchive = CreateTempCopyFile(bad("CDoffsetInBoundsWrong.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
using (ZipArchive archive = ZipFile.OpenRead(bad("numberOfEntriesDifferent.zip")))
{
Assert.Throws<InvalidDataException>(() => { var x = archive.Entries; });
}
using (TempFile testArchive = CreateTempCopyFile(bad("numberOfEntriesDifferent.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
//read mode on empty file
using (var memoryStream = new MemoryStream())
{
Assert.Throws<InvalidDataException>(() => new ZipArchive(memoryStream));
}
//offset out of bounds
using (ZipArchive archive = ZipFile.OpenRead(bad("localFileOffsetOutOfBounds.zip")))
{
ZipArchiveEntry e = archive.Entries[0];
Assert.Throws<InvalidDataException>(() => e.Open());
}
using (TempFile testArchive = CreateTempCopyFile(bad("localFileOffsetOutOfBounds.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
//compressed data offset + compressed size out of bounds
using (ZipArchive archive = ZipFile.OpenRead(bad("compressedSizeOutOfBounds.zip")))
{
ZipArchiveEntry e = archive.Entries[0];
Assert.Throws<InvalidDataException>(() => e.Open());
}
using (TempFile testArchive = CreateTempCopyFile(bad("compressedSizeOutOfBounds.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
//signature wrong
using (ZipArchive archive = ZipFile.OpenRead(bad("localFileHeaderSignatureWrong.zip")))
{
ZipArchiveEntry e = archive.Entries[0];
Assert.Throws<InvalidDataException>(() => e.Open());
}
using (TempFile testArchive = CreateTempCopyFile(bad("localFileHeaderSignatureWrong.zip"), GetTestFilePath()))
{
Assert.Throws<InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
}
}
[Theory]
[InlineData("LZMA.zip", true)]
[InlineData("invalidDeflate.zip", false)]
public void UnsupportedCompressionRoutine(string zipName, bool throwsOnOpen)
{
string filename = bad(zipName);
using (ZipArchive archive = ZipFile.OpenRead(filename))
{
ZipArchiveEntry e = archive.Entries[0];
if (throwsOnOpen)
{
Assert.Throws<InvalidDataException>(() => e.Open());
}
else
{
using (Stream s = e.Open())
{
Assert.Throws<InvalidDataException>(() => s.ReadByte());
}
}
}
using (TempFile updatedCopy = CreateTempCopyFile(filename, GetTestFilePath()))
{
string name;
long length, compressedLength;
DateTimeOffset lastWriteTime;
using (ZipArchive archive = ZipFile.Open(updatedCopy.Path, ZipArchiveMode.Update))
{
ZipArchiveEntry e = archive.Entries[0];
name = e.FullName;
lastWriteTime = e.LastWriteTime;
length = e.Length;
compressedLength = e.CompressedLength;
Assert.Throws<InvalidDataException>(() => e.Open());
}
//make sure that update mode preserves that unreadable file
using (ZipArchive archive = ZipFile.Open(updatedCopy.Path, ZipArchiveMode.Update))
{
ZipArchiveEntry e = archive.Entries[0];
Assert.Equal(name, e.FullName);
Assert.Equal(lastWriteTime, e.LastWriteTime);
Assert.Equal(length, e.Length);
Assert.Equal(compressedLength, e.CompressedLength);
Assert.Throws<InvalidDataException>(() => e.Open());
}
}
}
[Fact]
public void InvalidDates()
{
using (ZipArchive archive = ZipFile.OpenRead(bad("invaliddate.zip")))
{
Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime);
}
// Browser VFS does not support saving file attributes, so skip
if (!PlatformDetection.IsBrowser)
{
FileInfo fileWithBadDate = new FileInfo(GetTestFilePath());
fileWithBadDate.Create().Dispose();
fileWithBadDate.LastWriteTimeUtc = new DateTime(1970, 1, 1, 1, 1, 1);
string archivePath = GetTestFilePath();
using (FileStream output = File.Open(archivePath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(output, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(fileWithBadDate.FullName, "SomeEntryName");
}
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
{
Assert.Equal(new DateTime(1980, 1, 1, 0, 0, 0), archive.Entries[0].LastWriteTime.DateTime);
}
}
}
[Fact]
public void FilesOutsideDirectory()
{
string archivePath = GetTestFilePath();
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
using (StreamWriter writer = new StreamWriter(archive.CreateEntry(Path.Combine("..", "entry1"), CompressionLevel.Optimal).Open()))
{
writer.Write("This is a test.");
}
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(archivePath, GetTestFilePath()));
}
[Fact]
public void DirectoryEntryWithData()
{
string archivePath = GetTestFilePath();
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
using (StreamWriter writer = new StreamWriter(archive.CreateEntry("testdir" + Path.DirectorySeparatorChar, CompressionLevel.Optimal).Open()))
{
writer.Write("This is a test.");
}
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(archivePath, GetTestFilePath()));
}
[Fact]
public void ReadStreamOps()
{
using (ZipArchive archive = ZipFile.OpenRead(zfile("normal.zip")))
{
foreach (ZipArchiveEntry e in archive.Entries)
{
using (Stream s = e.Open())
{
Assert.True(s.CanRead, "Can read to read archive");
Assert.False(s.CanWrite, "Can't write to read archive");
Assert.False(s.CanSeek, "Can't seek on archive");
Assert.Equal(LengthOfUnseekableStream(s), e.Length);
}
}
}
}
/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// </summary>
[Theory]
[InlineData("NullCharFileName_FromWindows")]
[InlineData("NullCharFileName_FromUnix")]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Checks Unix-specific invalid file path
public void Unix_ZipWithInvalidFileNames_ThrowsArgumentException(string zipName)
{
Assert.Throws<ArgumentException>(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
}
[Fact]
public void UpdateReadTwice()
{
using (TempFile testArchive = CreateTempCopyFile(zfile("small.zip"), GetTestFilePath()))
using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update))
{
ZipArchiveEntry entry = archive.Entries[0];
string contents1, contents2;
using (StreamReader s = new StreamReader(entry.Open()))
{
contents1 = s.ReadToEnd();
}
using (StreamReader s = new StreamReader(entry.Open()))
{
contents2 = s.ReadToEnd();
}
Assert.Equal(contents1, contents2);
}
}
[Fact]
public async Task UpdateAddFile()
{
//add file
using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath()))
{
using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update))
{
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
}
await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read);
}
//add file and read entries before
using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath()))
{
using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update))
{
var x = archive.Entries;
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
}
await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read);
}
//add file and read entries after
using (TempFile testArchive = CreateTempCopyFile(zfile("normal.zip"), GetTestFilePath()))
{
using (ZipArchive archive = ZipFile.Open(testArchive.Path, ZipArchiveMode.Update))
{
await UpdateArchive(archive, zmodified(Path.Combine("addFile", "added.txt")), "added.txt");
var x = archive.Entries;
}
await IsZipSameAsDirAsync(testArchive.Path, zmodified("addFile"), ZipArchiveMode.Read);
}
}
/// <summary>
/// This test ensures that a zipfile with path names that are invalid to this OS will throw errors
/// when an attempt is made to extract them.
/// </summary>
[Theory]
[InlineData("WindowsInvalid_FromUnix", null)]
[InlineData("WindowsInvalid_FromWindows", null)]
[InlineData("NullCharFileName_FromWindows", "path")]
[InlineData("NullCharFileName_FromUnix", "path")]
[PlatformSpecific(TestPlatforms.Windows)] // Checks Windows-specific invalid file path
public void Windows_ZipWithInvalidFileNames_ThrowsException(string zipName, string paramName)
{
if (paramName == null)
Assert.Throws<IOException>(() => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
else
AssertExtensions.Throws<ArgumentException>(paramName, null, () => ZipFile.ExtractToDirectory(compat(zipName) + ".zip", GetTestFilePath()));
}
private static async Task UpdateArchive(ZipArchive archive, string installFile, string entryName)
{
string fileName = installFile;
ZipArchiveEntry e = archive.CreateEntry(entryName);
var file = FileData.GetFile(fileName);
e.LastWriteTime = file.LastModifiedDate;
using (var stream = await StreamHelpers.CreateTempCopyStream(fileName))
{
using (Stream es = e.Open())
{
es.SetLength(0);
stream.CopyTo(es);
}
}
}
}
}