5 // Joao Matos <joao.matos@xamarin.com>
7 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.IO.Compression;
31 using System.Security.Cryptography;
32 using NUnit.Framework;
34 namespace MonoTests.System.IO.Compression
37 public class ZipArchiveTests
39 static string GetSHA1HashFromFile(Stream stream)
41 using (var sha1 = SHA1.Create())
43 return BitConverter.ToString(sha1.ComputeHash(stream))
44 .Replace("-", string.Empty);
49 public void ZipGetEntryReadMode()
51 File.Copy("archive.zip", "test.zip", overwrite: true);
52 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
55 var entry = archive.GetEntry("foo.txt");
56 Assert.IsNotNull(entry);
58 var nullEntry = archive.GetEntry("nonexisting");
59 Assert.IsNull(nullEntry);
62 File.Delete ("test.zip");
66 public void ZipGetEntryCreateMode()
68 File.Copy("archive.zip", "test.zip", overwrite: true);
69 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
70 ZipArchiveMode.Create))
73 archive.GetEntry("foo");
74 } catch(NotSupportedException ex) {
81 File.Delete ("test.zip");
85 public void ZipGetEntryUpdateMode()
87 File.Copy("archive.zip", "test.zip", overwrite: true);
88 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
91 var entry = archive.GetEntry("foo.txt");
92 Assert.IsNotNull(entry);
94 var nullEntry = archive.GetEntry("nonexisting");
95 Assert.IsNull(nullEntry);
98 File.Delete ("test.zip");
102 public void ZipGetEntryOpen()
104 File.Copy("archive.zip", "test.zip", overwrite: true);
105 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
106 ZipArchiveMode.Read))
108 var entry = archive.GetEntry("foo.txt");
109 Assert.IsNotNull(entry);
111 var foo = entry.Open();
114 File.Delete ("test.zip");
118 public void ZipOpenAndReopenEntry()
121 File.Copy("archive.zip", "test.zip", overwrite: true);
122 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
123 ZipArchiveMode.Update))
125 var entry = archive.GetEntry("foo.txt");
126 Assert.IsNotNull(entry);
128 var stream = entry.Open();
131 stream = entry.Open();
132 } catch (global::System.IO.IOException ex) {
139 File.Delete ("test.zip");
145 public void ZipOpenCloseAndReopenEntry()
147 File.Copy("archive.zip", "test.zip", overwrite: true);
148 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
149 ZipArchiveMode.Update))
151 var entry = archive.GetEntry("foo.txt");
152 Assert.IsNotNull(entry);
154 var stream = entry.Open();
156 stream = entry.Open();
159 File.Delete ("test.zip");
163 public void ZipGetEntryDeleteReadMode()
165 File.Copy("archive.zip", "delete.zip", overwrite: true);
166 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
167 ZipArchiveMode.Update))
169 var entry = archive.GetEntry("foo.txt");
170 Assert.IsNotNull(entry);
175 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
176 ZipArchiveMode.Read))
178 var entry = archive.GetEntry("foo.txt");
179 Assert.IsNull(entry);
182 File.Delete ("delete.zip");
186 public void ZipDeleteEntryCheckEntries()
188 File.Copy("archive.zip", "delete.zip", overwrite: true);
189 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
190 ZipArchiveMode.Update))
192 var entry = archive.GetEntry("foo.txt");
193 Assert.IsNotNull(entry);
197 Assert.IsNull(archive.Entries.FirstOrDefault(e => e == entry));
200 File.Delete ("delete.zip");
204 public void ZipGetEntryDeleteUpdateMode()
206 File.Copy("archive.zip", "delete.zip", overwrite: true);
207 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
208 ZipArchiveMode.Update))
210 var entry = archive.GetEntry("foo.txt");
211 Assert.IsNotNull(entry);
216 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
217 ZipArchiveMode.Read))
219 var entry = archive.GetEntry("foo.txt");
220 Assert.IsNull(entry);
223 File.Delete ("delete.zip");
227 public void ZipCreateArchive()
229 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Create),
230 ZipArchiveMode.Create))
232 var dir = archive.CreateEntry("foobar/");
234 var entry = archive.CreateEntry("foo.txt");
235 using (var stream = entry.Open())
237 using (var streamWriter = new StreamWriter(stream))
238 streamWriter.Write("foo");
242 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Open),
243 ZipArchiveMode.Read))
245 Assert.IsNotNull(archive.GetEntry("foobar/"));
247 var entry = archive.GetEntry("foo.txt");
248 Assert.IsNotNull(entry);
250 var streamReader = new StreamReader(entry.Open());
251 var text = streamReader.ReadToEnd();
253 Assert.AreEqual("foo", text);
256 File.Delete ("create.zip");
260 public void ZipEnumerateEntriesModifiedTime()
262 File.Copy("archive.zip", "test.zip", overwrite: true);
263 var date = DateTimeOffset.Now;
264 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
265 ZipArchiveMode.Update))
267 var entry = archive.GetEntry("foo.txt");
268 entry.LastWriteTime = date;
271 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
272 ZipArchiveMode.Read))
274 var entry = archive.GetEntry("foo.txt");
275 Assert.AreEqual(entry.LastWriteTime.Year, date.Year);
276 Assert.AreEqual(entry.LastWriteTime.Month, date.Month);
277 Assert.AreEqual(entry.LastWriteTime.Day, date.Day);
281 File.Delete ("test.zip");
285 public void ZipEnumerateArchiveDefaultLastWriteTime()
287 using (var archive = new ZipArchive(File.Open("test.nupkg", FileMode.Open),
288 ZipArchiveMode.Read))
290 var entry = archive.GetEntry("_rels/.rels");
291 Assert.AreEqual(new DateTime(624511296000000000).Ticks, entry.LastWriteTime.Ticks);
292 Assert.IsNotNull(entry);
296 public void ZipGetArchiveEntryStreamLengthPosition(ZipArchiveMode mode)
298 File.Copy("test.nupkg", "test2.nupkg", overwrite: true);
299 using (var archive = new ZipArchive(File.Open("test2.nupkg", FileMode.Open), mode))
301 var entry = archive.GetEntry("_rels/.rels");
302 using (var stream = entry.Open())
304 Assert.AreEqual(0, stream.Position);
305 Assert.AreEqual(425, stream.Length);
308 // .NET does not support these in Read mode but we do.
309 var entry2 = archive.GetEntry("modernhttpclient.nuspec");
310 using (var stream = entry2.Open())
312 Assert.AreEqual(857, stream.Length);
313 if (mode == ZipArchiveMode.Update)
315 Assert.AreEqual(0, stream.Position);
319 File.Delete ("test2.nupkg");
323 public void ZipGetArchiveEntryStreamLengthPositionReadMode()
325 ZipGetArchiveEntryStreamLengthPosition(ZipArchiveMode.Read);
329 public void ZipGetArchiveEntryStreamLengthPositionUpdateMode()
331 ZipGetArchiveEntryStreamLengthPosition(ZipArchiveMode.Update);
335 public void ZipEnumerateEntriesReadMode()
337 File.Copy("archive.zip", "test.zip", overwrite: true);
338 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
339 ZipArchiveMode.Read))
341 var entries = archive.Entries;
342 Assert.AreEqual(5, entries.Count);
344 Assert.AreEqual("bar.txt", entries[0].FullName);
345 Assert.AreEqual("foo.txt", entries[1].FullName);
346 Assert.AreEqual("foobar/", entries[2].FullName);
347 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
348 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
351 File.Delete ("test.zip");
355 public void ZipWriteEntriesUpdateMode()
357 File.Copy("archive.zip", "test.zip", overwrite: true);
358 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
359 ZipArchiveMode.Update))
361 var foo = archive.GetEntry("foo.txt");
362 using (var stream = foo.Open())
363 using (var sw = new StreamWriter(stream))
369 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
370 ZipArchiveMode.Read))
372 var foo = archive.GetEntry("foo.txt");
373 using (var stream = foo.Open())
374 using (var sr = new StreamReader(stream))
376 var line = sr.ReadLine();
377 Assert.AreEqual("TEST", line);
381 File.Delete ("test.zip");
385 public void ZipWriteEntriesUpdateModeNewEntry()
387 var stream = new MemoryStream();
388 var zipArchive = new ZipArchive(stream, ZipArchiveMode.Update);
390 var newEntry = zipArchive.CreateEntry("testEntry");
392 using (var newStream = newEntry.Open())
394 using (var sw = new StreamWriter(newStream))
402 public void ZipCreateDuplicateEntriesUpdateMode()
404 var stream = new MemoryStream();
405 using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Update, true))
407 var e2 = zipArchive.CreateEntry("BBB");
408 var e3 = zipArchive.CreateEntry("BBB");
412 using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read))
414 Assert.AreEqual(2, zipArchive.Entries.Count);
419 public void ZipWriteEntriesUpdateModeNonZeroPosition()
421 File.Copy("archive.zip", "test.zip", overwrite: true);
422 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
423 ZipArchiveMode.Update))
425 var foo = archive.GetEntry("foo.txt");
426 using (var stream = foo.Open())
428 var line = stream.ReadByte();
429 using (var sw = new StreamWriter(stream))
436 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
437 ZipArchiveMode.Read))
439 var entries = archive.Entries;
440 var foo = archive.GetEntry("foo.txt");
441 using (var stream = foo.Open())
442 using (var sr = new StreamReader(stream))
444 var line = sr.ReadLine();
445 Assert.AreEqual("fTEST", line);
449 File.Delete ("test.zip");
453 public void ZipEnumerateEntriesUpdateMode()
455 File.Copy("archive.zip", "test.zip", overwrite: true);
456 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
457 ZipArchiveMode.Read))
459 var entries = archive.Entries;
460 Assert.AreEqual(5, entries.Count);
462 Assert.AreEqual("bar.txt", entries[0].FullName);
463 Assert.AreEqual("foo.txt", entries[1].FullName);
464 Assert.AreEqual("foobar/", entries[2].FullName);
465 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
466 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
469 File.Delete ("test.zip");
473 public void ZipEnumerateEntriesCreateMode()
475 File.Copy("archive.zip", "test.zip", overwrite: true);
476 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
477 ZipArchiveMode.Create))
480 archive.Entries.ToList();
481 } catch(NotSupportedException ex) {
488 File.Delete ("test.zip");
492 public void ZipUpdateEmptyArchive()
494 File.WriteAllText("empty.zip", string.Empty);
495 using (var archive = new ZipArchive(File.Open("empty.zip", FileMode.Open),
496 ZipArchiveMode.Update))
499 File.Delete ("empty.zip");
502 class MyFakeStream : FileStream
504 public MyFakeStream (string path, FileMode mode) : base(path, mode) {}
507 /// Simulate "CanSeek" is false, which is the case when you are retreiving data from web.
509 public override bool CanSeek => false;
511 public override long Position {
512 get {throw new NotSupportedException();}
513 set {throw new NotSupportedException();}
518 public void ZipReadNonSeekableStream()
520 var stream = new MyFakeStream("test.nupkg", FileMode.Open);
521 using (var archive = new ZipArchive (stream, ZipArchiveMode.Read))
527 public void ZipWriteNonSeekableStream() {
528 var stream = new MyFakeStream( "test.nupkg", FileMode.Open );
529 using ( var archive = new ZipArchive( stream, ZipArchiveMode.Create ) ) {
530 var entry = archive.CreateEntry( "foo" );
531 using ( var es = entry.Open() ) {
532 es.Write( new byte[] { 4, 2 }, 0, 2 );