Added System.IO.Compression implementation.
[mono.git] / mcs / class / System.IO.Compression / ZipArchive.cs
1 //
2 // ZipArchive.cs
3 //
4 // Author:
5 //         Martin Baulig <martin.baulig@xamarin.com>
6 //         Joao Matos <joao.matos@xamarin.com>
7 //
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
9 //
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:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
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
26 // THE SOFTWARE.
27 using System.Collections.Generic;
28 using System.Collections.ObjectModel;
29 using System.Linq;
30 using System.Text;
31 using Ionic.Zip;
32
33 namespace System.IO.Compression
34 {
35         public class ZipArchive : IDisposable
36         {
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 ZipFile zipFile;
44
45                 public ZipArchive (Stream stream)
46                 {
47                         if (stream == null)
48                                 throw new ArgumentNullException("stream");
49
50                         this.stream = stream;
51                         mode = ZipArchiveMode.Read;
52                         CreateZip(stream, mode);
53                 }
54
55                 public ZipArchive (Stream stream, ZipArchiveMode mode)
56                 {
57                         if (stream == null)
58                                 throw new ArgumentNullException("stream");
59
60                         this.stream = stream;
61                         this.mode = mode;
62                         CreateZip(stream, mode);
63                 }
64
65                 public ZipArchive (Stream stream, ZipArchiveMode mode, bool leaveOpen)
66                 {
67                         if (stream == null)
68                                 throw new ArgumentNullException("stream");
69
70                         this.stream = stream;
71                         this.mode = mode;
72                         this.leaveStreamOpen = leaveOpen;
73                         CreateZip(stream, mode);
74                 }
75
76                 public ZipArchive (Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding entryNameEncoding)
77                 {
78                         if (stream == null)
79                                 throw new ArgumentNullException("stream");
80
81                         this.stream = stream;
82                         this.mode = mode;
83                         this.leaveStreamOpen = leaveOpen;
84                         this.entryNameEncoding = entryNameEncoding;
85                         CreateZip(stream, mode);
86                 }
87
88                 private void CreateZip(Stream stream, ZipArchiveMode mode)
89                 {
90                         if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
91                                 throw new ArgumentOutOfRangeException("mode");
92
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");
96
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");
100
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");
104
105                         try {
106                                 zipFile = new ZipFile(stream, (mode != ZipArchiveMode.Read) ? stream : null, leaveStreamOpen,
107                                         entryNameEncoding);
108
109                                 if (stream.Length != 0) {
110                                         zipFile.FullScan = true;
111                                         zipFile.ReadToInstance();
112                                 }
113
114                                 if (mode == ZipArchiveMode.Create)
115                                         zipFile.Save();
116                         } catch (Exception) {
117                                 throw new InvalidDataException("The contents of the stream are not in the zip archive format.");
118                         }
119
120                         entries = new Dictionary<string, ZipArchiveEntry>();
121                         if (Mode != ZipArchiveMode.Create) {
122                                 foreach (var entry in zipFile.Entries) {
123                                         var zipEntry = new ZipArchiveEntry(this, entry);
124                                         entries[entry.FileName] = zipEntry;
125                                 }
126                         }
127                 }
128
129                 public ReadOnlyCollection<ZipArchiveEntry> Entries {
130                         get {
131                                 if (disposed)
132                                         throw new ObjectDisposedException("The zip archive has been disposed.");
133
134                                 if (Mode == ZipArchiveMode.Create)
135                                         throw new NotSupportedException("Cannot access entries in Create mode.");
136
137                                 if (zipFile == null)
138                                         throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
139
140                                 if (entries == null)
141                                         return new ReadOnlyCollection<ZipArchiveEntry>(new List<ZipArchiveEntry>());
142
143                                 return new ReadOnlyCollection<ZipArchiveEntry>(entries.Values.ToList());
144                         }
145                 }
146
147                 public ZipArchiveMode Mode {
148                         get {
149                                 if (disposed)
150                                         throw new ObjectDisposedException("The zip archive has been disposed.");
151
152                                 return mode;
153                         }
154                 }
155
156                 public ZipArchiveEntry CreateEntry (string entryName)
157                 {
158                         if (disposed)
159                                 throw new ObjectDisposedException("The zip archive has been disposed.");
160
161                         return CreateEntry(entryName, CompressionLevel.Optimal);
162                 }
163
164                 public ZipArchiveEntry CreateEntry (string entryName,
165                                                                                         CompressionLevel compressionLevel)
166                 {
167                         if (disposed)
168                                 throw new ObjectDisposedException("The zip archive has been disposed.");
169
170                         if (entryName == string.Empty)
171                                 throw new ArgumentException("Entry name cannot be empty.");
172
173                         if (entryName == null)
174                                 throw new ArgumentNullException("entryName");
175
176                         if (mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
177                                 throw new NotSupportedException("The zip archive does not support writing.");
178
179                         if (zipFile == null)
180                                 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
181
182                         var memoryStream = new MemoryStream();
183                         var entry = zipFile.AddEntry(entryName, memoryStream);
184                         var archiveEntry = new ZipArchiveEntry(this, entry);
185                         entries[entryName] = archiveEntry;
186
187                         return archiveEntry;
188                 }
189
190                 public ZipArchiveEntry GetEntry (string entryName)
191                 {
192                         if (disposed)
193                                 throw new ObjectDisposedException("The zip archive has been disposed.");
194
195                         if (entryName == string.Empty)
196                                 throw new ArgumentException("Entry name cannot be empty.");
197
198                         if (entryName == null)
199                                 throw new ArgumentNullException("entryName");
200
201                         if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Update)
202                                 throw new NotSupportedException("The zip archive does not support reading.");
203
204                         if (zipFile == null)
205                                 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
206
207                         return entries.ContainsKey(entryName) ? entries[entryName] : null;
208                 }
209
210                 private void Save()
211                 {
212                         // We save to a memory stream first because Ionic does not deal well
213                         // with saving to a file that has previously been open before.
214                         using (var newZip = new MemoryStream()) {
215                                 zipFile.Save(newZip);
216
217                                 stream.Position = 0;
218                                 newZip.Position = 0;
219                                 newZip.CopyTo(stream);
220                         }
221                 }
222
223                 protected virtual void Dispose (bool disposing)
224                 {
225                         if (disposed)
226                                 return;
227
228                         if (mode != ZipArchiveMode.Read)
229                                 Save();
230
231                         disposed = true;
232
233                         if (leaveStreamOpen)
234                                 return;
235
236                         if (stream != null)     {
237                                 stream.Dispose();
238                                 stream = null;
239                         }
240                 }
241
242                 public void Dispose ()
243                 {
244                         Dispose(true);
245                         GC.SuppressFinalize(this);
246                 }
247         }
248 }
249