Merge pull request #3363 from akoeplinger/fix-42938
[mono.git] / mcs / class / System.IO.Compression / ZipArchive.cs
1 //
2 // ZipArchiveEntry.cs
3 //
4 // Author:
5 //       Joao Matos <joao.matos@xamarin.com>
6 //       Martin Baulig <martin.baulig@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 SharpCompress.Common;
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 List<ZipArchiveEntry> entries; 
43                 internal SharpCompress.Archive.Zip.ZipArchive 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(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(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                         leaveStreamOpen = leaveOpen;
73                         CreateZip(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                         leaveStreamOpen = leaveOpen;
84                         this.entryNameEncoding = entryNameEncoding;
85                         CreateZip(mode);
86                 }
87
88                 private void CreateZip(ZipArchiveMode mode)
89                 {
90                         try {
91                                 if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
92                                         throw new ArgumentOutOfRangeException("mode");
93
94                                 // If the mode parameter is set to Read, the stream must support reading.
95                                 if (mode == ZipArchiveMode.Read && !stream.CanRead)
96                                         throw new ArgumentException("Stream must support reading for Read archive mode");
97
98                                 // If the mode parameter is set to Create, the stream must support writing.
99                                 if (mode == ZipArchiveMode.Create && !stream.CanWrite)
100                                         throw new ArgumentException("Stream must support writing for Create archive mode");
101
102                                 // If the mode parameter is set to Update, the stream must support reading, writing, and seeking.
103                                 if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek))
104                                         throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode");
105
106                                 // If the stream is not seekable, then buffer it into memory (same behavior as .NET). 
107                                 if (mode == ZipArchiveMode.Read && !stream.CanSeek)
108                                 {
109                                         var memoryStream = new MemoryStream();
110                                         stream.CopyTo(memoryStream);
111
112                                         if (!leaveStreamOpen)
113                                                 stream.Dispose();
114
115                                         this.stream = memoryStream;
116                                 }
117
118                                 try {
119                                         zipFile = mode != ZipArchiveMode.Create && stream.Length != 0
120                                                 ? SharpCompress.Archive.Zip.ZipArchive.Open(stream)
121                                                 : SharpCompress.Archive.Zip.ZipArchive.Create();
122                                 } catch (Exception e) {
123                                         throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e);
124                                 }
125
126                                 entries = new List<ZipArchiveEntry>();
127                                 if (Mode != ZipArchiveMode.Create) {
128                                         foreach (var entry in zipFile.Entries) {
129                                                 var zipEntry = new ZipArchiveEntry(this, entry);
130                                                 entries.Add(zipEntry);
131                                         }
132                                 }
133                         }
134                         catch {
135                                 if (!leaveStreamOpen)
136                                         stream.Dispose();
137                                 throw;
138                         }
139                 }
140
141                 public ReadOnlyCollection<ZipArchiveEntry> Entries {
142                         get {
143                                 if (disposed)
144                                         throw new ObjectDisposedException("The zip archive has been disposed.");
145
146                                 if (Mode == ZipArchiveMode.Create)
147                                         throw new NotSupportedException("Cannot access entries in Create mode.");
148
149                                 if (zipFile == null)
150                                         throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
151
152                                 if (entries == null)
153                                         return new ReadOnlyCollection<ZipArchiveEntry>(new List<ZipArchiveEntry>());
154
155                                 return new ReadOnlyCollection<ZipArchiveEntry>(entries);
156                         }
157                 }
158
159                 public ZipArchiveMode Mode {
160                         get {
161                                 if (disposed)
162                                         throw new ObjectDisposedException("The zip archive has been disposed.");
163
164                                 return mode;
165                         }
166                 }
167
168                 public ZipArchiveEntry CreateEntry (string entryName)
169                 {
170                         if (disposed)
171                                 throw new ObjectDisposedException("The zip archive has been disposed.");
172
173                         return CreateEntry(entryName, CompressionLevel.Optimal);
174                 }
175
176                 internal SharpCompress.Archive.Zip.ZipArchiveEntry CreateEntryInternal(string entryName)
177                 {
178                         var memoryStream = new MemoryStream();
179                         var entry = zipFile.AddEntry(entryName, memoryStream);
180
181                         return entry;
182                 }
183
184                 public ZipArchiveEntry CreateEntry (string entryName, CompressionLevel compressionLevel)
185                 {
186                         if (disposed)
187                                 throw new ObjectDisposedException("The zip archive has been disposed.");
188
189                         if (entryName == string.Empty)
190                                 throw new ArgumentException("Entry name cannot be empty.");
191
192                         if (entryName == null)
193                                 throw new ArgumentNullException("entryName");
194
195                         if (mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
196                                 throw new NotSupportedException("The zip archive does not support writing.");
197
198                         if (zipFile == null)
199                                 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
200
201                         var internalEntry = CreateEntryInternal(entryName);
202                         var archiveEntry = new ZipArchiveEntry(this, internalEntry);
203                         entries.Add(archiveEntry);
204
205                         return archiveEntry;
206                 }
207
208                 public ZipArchiveEntry GetEntry (string entryName)
209                 {
210                         if (disposed)
211                                 throw new ObjectDisposedException("The zip archive has been disposed.");
212
213                         if (entryName == string.Empty)
214                                 throw new ArgumentException("Entry name cannot be empty.");
215
216                         if (entryName == null)
217                                 throw new ArgumentNullException("entryName");
218
219                         if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Update)
220                                 throw new NotSupportedException("The zip archive does not support reading.");
221
222                         if (zipFile == null)
223                                 throw new InvalidDataException("The zip archive is corrupt, and its entries cannot be retrieved.");
224
225                         return entries.FirstOrDefault(e => e.FullName == entryName);
226                 }
227
228                 private void Save()
229                 {
230                         using (var newZip = new MemoryStream()) {
231                                 zipFile.SaveTo(newZip, CompressionType.Deflate, entryNameEncoding ?? Encoding.UTF8);
232
233                                 stream.SetLength(0);
234                                 stream.Position = 0;
235                                 newZip.Position = 0;
236                                 newZip.CopyTo(stream);
237                         }
238                 }
239
240                 internal void RemoveEntryInternal(ZipArchiveEntry entry)
241                 {
242                         zipFile.RemoveEntry(entry.entry);
243                         entries.Remove(entry);
244                 }
245
246                 protected virtual void Dispose (bool disposing)
247                 {
248                         if (disposed)
249                                 return;
250
251                         if (mode != ZipArchiveMode.Read)
252                                 Save();
253
254                         disposed = true;
255
256                         if (leaveStreamOpen)
257                                 return;
258
259                         if (stream != null) {
260                                 stream.Dispose();
261                                 stream = null;
262                         }
263                 }
264
265                 public void Dispose ()
266                 {
267                         Dispose(true);
268                         GC.SuppressFinalize(this);
269                 }
270         }
271 }
272