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);
64 public void ZipGetEntryCreateMode()
66 File.Copy("archive.zip", "test.zip", overwrite: true);
67 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
68 ZipArchiveMode.Create))
71 archive.GetEntry("foo");
72 } catch(NotSupportedException ex) {
81 public void ZipGetEntryUpdateMode()
83 File.Copy("archive.zip", "test.zip", overwrite: true);
84 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
87 var entry = archive.GetEntry("foo.txt");
88 Assert.IsNotNull(entry);
90 var nullEntry = archive.GetEntry("nonexisting");
91 Assert.IsNull(nullEntry);
96 public void ZipGetEntryOpen()
98 File.Copy("archive.zip", "test.zip", overwrite: true);
99 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
100 ZipArchiveMode.Read))
102 var entry = archive.GetEntry("foo.txt");
103 Assert.IsNotNull(entry);
105 var foo = entry.Open();
106 Assert.AreEqual("F1D2D2F924E986AC86FDF7B36C94BCDF32BEEC15",
107 GetSHA1HashFromFile(foo));
112 public void ZipGetEntryDeleteReadMode()
114 File.Copy("archive.zip", "delete.zip", overwrite: true);
115 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
116 ZipArchiveMode.Update))
118 var entry = archive.GetEntry("foo.txt");
119 Assert.IsNotNull(entry);
124 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
125 ZipArchiveMode.Read))
127 var entry = archive.GetEntry("foo.txt");
128 Assert.IsNull(entry);
133 public void ZipGetEntryDeleteUpdateMode()
135 File.Copy("archive.zip", "delete.zip", overwrite: true);
136 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
137 ZipArchiveMode.Update))
139 var entry = archive.GetEntry("foo.txt");
140 Assert.IsNotNull(entry);
145 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
146 ZipArchiveMode.Read))
148 var entry = archive.GetEntry("foo.txt");
149 Assert.IsNull(entry);
154 public void ZipCreateArchive()
156 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Create),
157 ZipArchiveMode.Create))
159 var entry = archive.CreateEntry("foo.txt");
160 using (var stream = entry.Open())
165 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Open),
166 ZipArchiveMode.Read))
168 var entry = archive.GetEntry("foo.txt");
169 Assert.IsNotNull(entry);
174 public void ZipEnumerateEntriesReadMode()
176 File.Copy("archive.zip", "test.zip", overwrite: true);
177 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
178 ZipArchiveMode.Read))
180 var entries = archive.Entries;
181 Assert.AreEqual(5, entries.Count);
183 Assert.AreEqual("bar.txt", entries[0].FullName);
184 Assert.AreEqual("foo.txt", entries[1].FullName);
185 Assert.AreEqual("foobar/", entries[2].FullName);
186 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
187 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
192 public void ZipEnumerateEntriesUpdateMode()
194 File.Copy("archive.zip", "test.zip", overwrite: true);
195 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
196 ZipArchiveMode.Read))
198 var entries = archive.Entries;
199 Assert.AreEqual(5, entries.Count);
201 Assert.AreEqual("bar.txt", entries[0].FullName);
202 Assert.AreEqual("foo.txt", entries[1].FullName);
203 Assert.AreEqual("foobar/", entries[2].FullName);
204 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
205 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
210 public void ZipEnumerateEntriesCreateMode()
212 File.Copy("archive.zip", "test.zip", overwrite: true);
213 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
214 ZipArchiveMode.Create))
217 archive.Entries.ToList();
218 } catch(NotSupportedException ex) {