Merge pull request #2922 from akoeplinger/fix-tvos
[mono.git] / mcs / class / System.IO.Compression / ZipArchive.cs
index b2d6e2f4bfe0e32851ad9e043ac5d666a4f71e9e..f2ef892a910e4432e1331874b5a75c722b4133fc 100644 (file)
@@ -87,36 +87,43 @@ namespace System.IO.Compression
 
                private void CreateZip(Stream stream, ZipArchiveMode mode)
                {
-                       if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
-                               throw new ArgumentOutOfRangeException("mode");
-
-                       // If the mode parameter is set to Read, the stream must support reading.
-                       if (mode == ZipArchiveMode.Read && !stream.CanRead)
-                               throw new ArgumentException("Stream must support reading for Read archive mode");
-
-                       // If the mode parameter is set to Create, the stream must support writing.
-                       if (mode == ZipArchiveMode.Create && !stream.CanWrite)
-                               throw new ArgumentException("Stream must support writing for Create archive mode");
-
-                       // If the mode parameter is set to Update, the stream must support reading, writing, and seeking.
-                       if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek))
-                               throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode");
-
                        try {
-                               zipFile = mode != ZipArchiveMode.Create && stream.Length != 0
-                                       ? SharpCompress.Archive.Zip.ZipArchive.Open(stream)
-                                       : SharpCompress.Archive.Zip.ZipArchive.Create();
-                       } catch (Exception e) {
-                               throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e);
-                       }
+                               if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update)
+                                       throw new ArgumentOutOfRangeException("mode");
+
+                               // If the mode parameter is set to Read, the stream must support reading.
+                               if (mode == ZipArchiveMode.Read && !stream.CanRead)
+                                       throw new ArgumentException("Stream must support reading for Read archive mode");
+
+                               // If the mode parameter is set to Create, the stream must support writing.
+                               if (mode == ZipArchiveMode.Create && !stream.CanWrite)
+                                       throw new ArgumentException("Stream must support writing for Create archive mode");
+
+                               // If the mode parameter is set to Update, the stream must support reading, writing, and seeking.
+                               if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek))
+                                       throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode");
+
+                               try {
+                                       zipFile = mode != ZipArchiveMode.Create && stream.Length != 0
+                                               ? SharpCompress.Archive.Zip.ZipArchive.Open(stream)
+                                               : SharpCompress.Archive.Zip.ZipArchive.Create();
+                               } catch (Exception e) {
+                                       throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e);
+                               }
 
-                       entries = new Dictionary<string, ZipArchiveEntry>();
-                       if (Mode != ZipArchiveMode.Create) {
-                               foreach (var entry in zipFile.Entries) {
-                                       var zipEntry = new ZipArchiveEntry(this, entry);
-                                       entries[entry.Key] = zipEntry;
+                               entries = new Dictionary<string, ZipArchiveEntry>();
+                               if (Mode != ZipArchiveMode.Create) {
+                                       foreach (var entry in zipFile.Entries) {
+                                               var zipEntry = new ZipArchiveEntry(this, entry);
+                                               entries[entry.Key] = zipEntry;
+                                       }
                                }
                        }
+                       catch {
+                               if (!leaveStreamOpen)
+                                       stream.Dispose();
+                               throw;
+                       }
                }
 
                public ReadOnlyCollection<ZipArchiveEntry> Entries {