5 // Joao Matos <joao.matos@xamarin.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Collections.Generic;
28 using System.Collections.ObjectModel;
31 using SharpCompress.Common;
33 namespace System.IO.Compression
35 public class ZipArchive : IDisposable
37 internal Stream stream;
38 internal readonly bool leaveStreamOpen;
39 internal readonly ZipArchiveMode mode;
40 internal Encoding entryNameEncoding;
41 internal bool disposed;
42 internal Dictionary<string, ZipArchiveEntry> entries;
43 internal SharpCompress.Archive.Zip.ZipArchive zipFile;
45 public ZipArchive (Stream stream)
48 throw new ArgumentNullException("stream");
51 mode = ZipArchiveMode.Read;
52 CreateZip(stream, mode);
55 public ZipArchive (Stream stream, ZipArchiveMode mode)
58 throw new ArgumentNullException("stream");
62 CreateZip(stream, mode);
65 public ZipArchive (Stream stream, ZipArchiveMode mode, bool leaveOpen)
68 throw new ArgumentNullException("stream");
72 leaveStreamOpen = leaveOpen;
73 CreateZip(stream, mode);
76 public ZipArchive (Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding entryNameEncoding)
79 throw new ArgumentNullException("stream");
83 leaveStreamOpen = leaveOpen;
84 this.entryNameEncoding = entryNameEncoding;
85 CreateZip(stream, mode);
88 private void CreateZip(Stream stream, ZipArchiveMode mode)
90 if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
91 throw new ArgumentOutOfRangeException("mode");
93 // If the mode parameter is set to Read, the stream must support reading.
94 if (mode == ZipArchiveMode.Read && !stream.CanRead)
95 throw new ArgumentException("Stream must support reading for Read archive mode");
97 // If the mode parameter is set to Create, the stream must support writing.
98 if (mode == ZipArchiveMode.Create && !stream.CanWrite)
99 throw new ArgumentException("Stream must support writing for Create archive mode");
101 // If the mode parameter is set to Update, the stream must support reading, writing, and seeking.
102 if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek))
103 throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode");
106 zipFile = mode == ZipArchiveMode.Create ?
107 SharpCompress.Archive.Zip.ZipArchive.Create() :
108 SharpCompress.Archive.Zip.ZipArchive.Open(stream);
109 } catch (Exception e) {
110 throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e);
113 entries = new Dictionary<string, ZipArchiveEntry>();
114 if (Mode != ZipArchiveMode.Create) {
115 foreach (var entry in zipFile.Entries) {
116 var zipEntry = new ZipArchiveEntry(this, entry);
117 entries[entry.Key] = zipEntry;
122 public ReadOnlyCollection<ZipArchiveEntry> Entries {
125 throw new ObjectDisposedException("The zip archive has been disposed.");
127 if (Mode == ZipArchiveMode.Create)
128 throw new NotSupportedException("Cannot access entries in Create mode.");
131 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
134 return new ReadOnlyCollection<ZipArchiveEntry>(new List<ZipArchiveEntry>());
136 return new ReadOnlyCollection<ZipArchiveEntry>(entries.Values.ToList());
140 public ZipArchiveMode Mode {
143 throw new ObjectDisposedException("The zip archive has been disposed.");
149 public ZipArchiveEntry CreateEntry (string entryName)
152 throw new ObjectDisposedException("The zip archive has been disposed.");
154 return CreateEntry(entryName, CompressionLevel.Optimal);
157 public ZipArchiveEntry CreateEntry (string entryName, CompressionLevel compressionLevel)
160 throw new ObjectDisposedException("The zip archive has been disposed.");
162 if (entryName == string.Empty)
163 throw new ArgumentException("Entry name cannot be empty.");
165 if (entryName == null)
166 throw new ArgumentNullException("entryName");
168 if (mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
169 throw new NotSupportedException("The zip archive does not support writing.");
172 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
174 var memoryStream = new MemoryStream();
175 var entry = zipFile.AddEntry(entryName, memoryStream);
176 var archiveEntry = new ZipArchiveEntry(this, entry);
177 entries[entryName] = archiveEntry;
182 public ZipArchiveEntry GetEntry (string entryName)
185 throw new ObjectDisposedException("The zip archive has been disposed.");
187 if (entryName == string.Empty)
188 throw new ArgumentException("Entry name cannot be empty.");
190 if (entryName == null)
191 throw new ArgumentNullException("entryName");
193 if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Update)
194 throw new NotSupportedException("The zip archive does not support reading.");
197 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
199 return entries.ContainsKey(entryName) ? entries[entryName] : null;
204 using (var newZip = new MemoryStream()) {
205 zipFile.SaveTo(newZip, CompressionType.Deflate);
210 newZip.CopyTo(stream);
214 protected virtual void Dispose (bool disposing)
219 if (mode != ZipArchiveMode.Read)
227 if (stream != null) {
233 public void Dispose ()
236 GC.SuppressFinalize(this);