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 ZipGetEntryDeleteUpdateMode()
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);
198 using (var archive = new ZipArchive(File.Open("delete.zip", FileMode.Open),
199 ZipArchiveMode.Read))
201 var entry = archive.GetEntry("foo.txt");
202 Assert.IsNull(entry);
205 File.Delete ("delete.zip");
209 public void ZipCreateArchive()
211 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Create),
212 ZipArchiveMode.Create))
214 var dir = archive.CreateEntry("foobar/");
216 var entry = archive.CreateEntry("foo.txt");
217 using (var stream = entry.Open())
219 using (var streamWriter = new StreamWriter(stream))
220 streamWriter.Write("foo");
224 using (var archive = new ZipArchive(File.Open("create.zip", FileMode.Open),
225 ZipArchiveMode.Read))
227 Assert.IsNotNull(archive.GetEntry("foobar/"));
229 var entry = archive.GetEntry("foo.txt");
230 Assert.IsNotNull(entry);
232 var streamReader = new StreamReader(entry.Open());
233 var text = streamReader.ReadToEnd();
235 Assert.AreEqual("foo", text);
238 File.Delete ("create.zip");
242 public void ZipEnumerateEntriesModifiedTime()
244 File.Copy("archive.zip", "test.zip", overwrite: true);
245 var date = DateTimeOffset.Now;
246 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
247 ZipArchiveMode.Update))
249 var entry = archive.GetEntry("foo.txt");
250 entry.LastWriteTime = date;
253 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
254 ZipArchiveMode.Read))
256 var entry = archive.GetEntry("foo.txt");
257 Assert.AreEqual(entry.LastWriteTime.Year, date.Year);
258 Assert.AreEqual(entry.LastWriteTime.Month, date.Month);
259 Assert.AreEqual(entry.LastWriteTime.Day, date.Day);
263 File.Delete ("test.zip");
267 public void ZipEnumerateArchiveDefaultLastWriteTime()
269 using (var archive = new ZipArchive(File.Open("test.nupkg", FileMode.Open),
270 ZipArchiveMode.Read))
272 var entry = archive.GetEntry("_rels/.rels");
273 Assert.AreEqual(new DateTime(624511296000000000).Ticks, entry.LastWriteTime.Ticks);
274 Assert.IsNotNull(entry);
279 public void ZipEnumerateEntriesReadMode()
281 File.Copy("archive.zip", "test.zip", overwrite: true);
282 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
283 ZipArchiveMode.Read))
285 var entries = archive.Entries;
286 Assert.AreEqual(5, entries.Count);
288 Assert.AreEqual("bar.txt", entries[0].FullName);
289 Assert.AreEqual("foo.txt", entries[1].FullName);
290 Assert.AreEqual("foobar/", entries[2].FullName);
291 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
292 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
295 File.Delete ("test.zip");
299 public void ZipEnumerateEntriesUpdateMode()
301 File.Copy("archive.zip", "test.zip", overwrite: true);
302 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
303 ZipArchiveMode.Read))
305 var entries = archive.Entries;
306 Assert.AreEqual(5, entries.Count);
308 Assert.AreEqual("bar.txt", entries[0].FullName);
309 Assert.AreEqual("foo.txt", entries[1].FullName);
310 Assert.AreEqual("foobar/", entries[2].FullName);
311 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
312 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
315 File.Delete ("test.zip");
319 public void ZipEnumerateEntriesCreateMode()
321 File.Copy("archive.zip", "test.zip", overwrite: true);
322 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
323 ZipArchiveMode.Create))
326 archive.Entries.ToList();
327 } catch(NotSupportedException ex) {
334 File.Delete ("test.zip");
338 public void ZipUpdateEmptyArchive()
340 File.WriteAllText("empty.zip", string.Empty);
341 using (var archive = new ZipArchive(File.Open("empty.zip", FileMode.Open),
342 ZipArchiveMode.Update))
345 File.Delete ("empty.zip");