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 ZipEnumerateEntriesReadMode()
269 File.Copy("archive.zip", "test.zip", overwrite: true);
270 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
271 ZipArchiveMode.Read))
273 var entries = archive.Entries;
274 Assert.AreEqual(5, entries.Count);
276 Assert.AreEqual("bar.txt", entries[0].FullName);
277 Assert.AreEqual("foo.txt", entries[1].FullName);
278 Assert.AreEqual("foobar/", entries[2].FullName);
279 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
280 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
283 File.Delete ("test.zip");
287 public void ZipEnumerateEntriesUpdateMode()
289 File.Copy("archive.zip", "test.zip", overwrite: true);
290 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
291 ZipArchiveMode.Read))
293 var entries = archive.Entries;
294 Assert.AreEqual(5, entries.Count);
296 Assert.AreEqual("bar.txt", entries[0].FullName);
297 Assert.AreEqual("foo.txt", entries[1].FullName);
298 Assert.AreEqual("foobar/", entries[2].FullName);
299 Assert.AreEqual("foobar/bar.txt", entries[3].FullName);
300 Assert.AreEqual("foobar/foo.txt", entries[4].FullName);
303 File.Delete ("test.zip");
307 public void ZipEnumerateEntriesCreateMode()
309 File.Copy("archive.zip", "test.zip", overwrite: true);
310 using (var archive = new ZipArchive(File.Open("test.zip", FileMode.Open),
311 ZipArchiveMode.Create))
314 archive.Entries.ToList();
315 } catch(NotSupportedException ex) {
322 File.Delete ("test.zip");
326 public void ZipUpdateEmptyArchive()
328 File.WriteAllText("empty.zip", string.Empty);
329 using (var archive = new ZipArchive(File.Open("empty.zip", FileMode.Open),
330 ZipArchiveMode.Update))
333 File.Delete ("empty.zip");