Merge pull request #4118 from petertiedemann/fix-bug-48516
authorJoão Matos <joao@tritao.eu>
Tue, 20 Dec 2016 20:38:03 +0000 (20:38 +0000)
committerGitHub <noreply@github.com>
Tue, 20 Dec 2016 20:38:03 +0000 (20:38 +0000)
Fix bug 48516

mcs/class/System.IO.Compression/Test/System.IO.Compression/ZipTest.cs
mcs/class/System.IO.Compression/ZipArchive.cs

index f1b83746b47ccbb1ee5e2474abfee4dc16663a48..343746863e3218ff9f7e054e1fdd72448d0c3724 100644 (file)
@@ -507,6 +507,11 @@ namespace MonoTests.System.IO.Compression
                        /// Simulate "CanSeek" is false, which is the case when you are retreiving data from web.
                        /// </summary>
                        public override bool CanSeek => false;
+
+                       public override long Position {
+                               get {throw new NotSupportedException();}
+                               set {throw new NotSupportedException();}
+                       }
                }
 
                [Test]
@@ -517,5 +522,16 @@ namespace MonoTests.System.IO.Compression
                        {
                        }
                }
+
+               [Test]
+               public void ZipWriteNonSeekableStream() {
+                       var stream = new MyFakeStream( "test.nupkg", FileMode.Open );
+                       using ( var archive = new ZipArchive( stream, ZipArchiveMode.Create ) ) {
+                               var entry = archive.CreateEntry( "foo" );
+                               using ( var es = entry.Open() ) {
+                                       es.Write( new byte[] { 4, 2 }, 0, 2 );
+                               }
+                       }
+               }
        }
 }
index 5df9434321ce2c8761255d6fad9ac38f977f8baf..4aa7318e4c866f87924adaaf4be52ece9cb3735e 100644 (file)
@@ -227,13 +227,20 @@ namespace System.IO.Compression
 
                private void Save()
                {
-                       using (var newZip = new MemoryStream()) {
-                               zipFile.SaveTo(newZip, CompressionType.Deflate, entryNameEncoding ?? Encoding.UTF8);
+                       if (mode == ZipArchiveMode.Create)
+                       {
+                               zipFile.SaveTo(stream, CompressionType.Deflate, entryNameEncoding ?? Encoding.UTF8);
+                       }
+                       else {
+                               using (var newZip = new MemoryStream())
+                               {
+                                       zipFile.SaveTo(newZip, CompressionType.Deflate, entryNameEncoding ?? Encoding.UTF8);
 
-                               stream.SetLength(0);
-                               stream.Position = 0;
-                               newZip.Position = 0;
-                               newZip.CopyTo(stream);
+                                       stream.SetLength(0);
+                                       stream.Position = 0;
+                                       newZip.Position = 0;
+                                       newZip.CopyTo(stream);
+                               }
                        }
                }