diff --git a/SharpCompress.Test/Zip/ZipArchiveTests.cs b/SharpCompress.Test/Zip/ZipArchiveTests.cs index 0c949923..b6379b6b 100644 --- a/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Linq; +using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using SharpCompress.Archive; using SharpCompress.Archive.Zip; @@ -163,6 +164,25 @@ namespace SharpCompress.Test CompareArchivesByPath(modified, scratchPath); } + [TestMethod] + public void Zip_Save_Twice() + { + string scratchPath1 = Path.Combine(SCRATCH_FILES_PATH, "a.zip"); + string scratchPath2 = Path.Combine(SCRATCH_FILES_PATH, "b.zip"); + + ResetScratch(); + using (var arc = ZipArchive.Create()) + { + string str = "test.txt"; + var source = new MemoryStream(Encoding.UTF8.GetBytes(str)); + arc.AddEntry("test.txt", source, true, source.Length); + arc.SaveTo(scratchPath1, CompressionType.Deflate); + arc.SaveTo(scratchPath2, CompressionType.Deflate); + } + + Assert.AreEqual(new FileInfo(scratchPath1).Length, new FileInfo(scratchPath2).Length); + } + [TestMethod] public void Zip_Create_New() { diff --git a/SharpCompress/Archive/AbstractWritableArchive.cs b/SharpCompress/Archive/AbstractWritableArchive.cs index 2eb10d13..c03f5499 100644 --- a/SharpCompress/Archive/AbstractWritableArchive.cs +++ b/SharpCompress/Archive/AbstractWritableArchive.cs @@ -94,6 +94,8 @@ namespace SharpCompress.Archive public void SaveTo(Stream stream, CompressionInfo compressionType) { + //reset streams of new entries + newEntries.Cast().ForEach(x => x.Stream.Seek(0, SeekOrigin.Begin)); SaveTo(stream, compressionType, OldEntries, newEntries); } @@ -104,6 +106,8 @@ namespace SharpCompress.Archive { throw new ArgumentException("Streams must be readable and seekable to use the Writing Archive API"); } + //ensure new stream is at the start, this could be reset + source.Seek(0, SeekOrigin.Begin); return CreateEntryInternal(filePath, source, size, modified, closeStream); } diff --git a/SharpCompress/Archive/GZip/GZipWritableArchiveEntry.cs b/SharpCompress/Archive/GZip/GZipWritableArchiveEntry.cs index 679ccd08..56c59ead 100644 --- a/SharpCompress/Archive/GZip/GZipWritableArchiveEntry.cs +++ b/SharpCompress/Archive/GZip/GZipWritableArchiveEntry.cs @@ -6,18 +6,19 @@ using SharpCompress.IO; namespace SharpCompress.Archive.GZip { - internal class GZipWritableArchiveEntry : GZipArchiveEntry + internal class GZipWritableArchiveEntry : GZipArchiveEntry, IWritableArchiveEntry { - private string path; - private long size; - private DateTime? lastModified; - private bool closeStream; + private readonly string path; + private readonly long size; + private readonly DateTime? lastModified; + private readonly bool closeStream; + private readonly Stream stream; internal GZipWritableArchiveEntry(GZipArchive archive, Stream stream, string path, long size, DateTime? lastModified, bool closeStream) : base(archive, null) { - this.Stream = stream; + this.stream = stream; this.path = path; this.size = size; this.lastModified = lastModified; @@ -84,18 +85,24 @@ namespace SharpCompress.Archive.GZip get { throw new NotImplementedException(); } } - internal Stream Stream { get; private set; } + Stream IWritableArchiveEntry.Stream + { + get + { + return stream; + } + } public override Stream OpenEntryStream() { - return new NonDisposingStream(Stream); + return new NonDisposingStream(stream); } internal override void Close() { if (closeStream) { - Stream.Dispose(); + stream.Dispose(); } } } diff --git a/SharpCompress/Archive/IWritableArchiveEntry.cs b/SharpCompress/Archive/IWritableArchiveEntry.cs new file mode 100644 index 00000000..2175fdae --- /dev/null +++ b/SharpCompress/Archive/IWritableArchiveEntry.cs @@ -0,0 +1,9 @@ +using System.IO; + +namespace SharpCompress.Archive +{ + internal interface IWritableArchiveEntry + { + Stream Stream { get; } + } +} \ No newline at end of file diff --git a/SharpCompress/Archive/Tar/TarWritableArchiveEntry.cs b/SharpCompress/Archive/Tar/TarWritableArchiveEntry.cs index 82b7de78..639e7ac4 100644 --- a/SharpCompress/Archive/Tar/TarWritableArchiveEntry.cs +++ b/SharpCompress/Archive/Tar/TarWritableArchiveEntry.cs @@ -6,18 +6,19 @@ using SharpCompress.IO; namespace SharpCompress.Archive.Tar { - internal class TarWritableArchiveEntry : TarArchiveEntry + internal class TarWritableArchiveEntry : TarArchiveEntry, IWritableArchiveEntry { private readonly string path; private readonly long size; private readonly DateTime? lastModified; private readonly bool closeStream; + private readonly Stream stream; internal TarWritableArchiveEntry(TarArchive archive, Stream stream, CompressionType compressionType, string path, long size, DateTime? lastModified, bool closeStream) : base(archive, null, compressionType) { - Stream = stream; + this.stream = stream; this.path = path; this.size = size; this.lastModified = lastModified; @@ -83,19 +84,24 @@ namespace SharpCompress.Archive.Tar { get { throw new NotImplementedException(); } } - - internal Stream Stream { get; private set; } + Stream IWritableArchiveEntry.Stream + { + get + { + return stream; + } + } public override Stream OpenEntryStream() { - return new NonDisposingStream(Stream); + return new NonDisposingStream(stream); } internal override void Close() { if (closeStream) { - Stream.Dispose(); + stream.Dispose(); } } } diff --git a/SharpCompress/Archive/Zip/ZipArchive.cs b/SharpCompress/Archive/Zip/ZipArchive.cs index dea6d7a3..ee5e2f29 100644 --- a/SharpCompress/Archive/Zip/ZipArchive.cs +++ b/SharpCompress/Archive/Zip/ZipArchive.cs @@ -14,7 +14,7 @@ namespace SharpCompress.Archive.Zip { public class ZipArchive : AbstractWritableArchive { - private SeekableZipHeaderFactory headerFactory; + private readonly SeekableZipHeaderFactory headerFactory; /// /// Gets or sets the compression level applied to files added to the archive, diff --git a/SharpCompress/Archive/Zip/ZipWritableArchiveEntry.cs b/SharpCompress/Archive/Zip/ZipWritableArchiveEntry.cs index f7bece03..40bd0903 100644 --- a/SharpCompress/Archive/Zip/ZipWritableArchiveEntry.cs +++ b/SharpCompress/Archive/Zip/ZipWritableArchiveEntry.cs @@ -6,19 +6,20 @@ using SharpCompress.IO; namespace SharpCompress.Archive.Zip { - internal class ZipWritableArchiveEntry : ZipArchiveEntry + internal class ZipWritableArchiveEntry : ZipArchiveEntry, IWritableArchiveEntry { private readonly string path; private readonly long size; private readonly DateTime? lastModified; private readonly bool closeStream; + private readonly Stream stream; private bool isDisposed; internal ZipWritableArchiveEntry(ZipArchive archive, Stream stream, string path, long size, DateTime? lastModified, bool closeStream) : base(archive, null) { - Stream = stream; + this.stream = stream; this.path = path; this.size = size; this.lastModified = lastModified; @@ -85,18 +86,24 @@ namespace SharpCompress.Archive.Zip get { throw new NotImplementedException(); } } - internal Stream Stream { get; private set; } + Stream IWritableArchiveEntry.Stream + { + get + { + return stream; + } + } public override Stream OpenEntryStream() { - return new NonDisposingStream(Stream); + return new NonDisposingStream(stream); } internal override void Close() { if (closeStream && !isDisposed) { - Stream.Dispose(); + stream.Dispose(); isDisposed = true; } } diff --git a/SharpCompress/SharpCompress.Portable.csproj b/SharpCompress/SharpCompress.Portable.csproj index 82a955ef..06a9aedb 100644 --- a/SharpCompress/SharpCompress.Portable.csproj +++ b/SharpCompress/SharpCompress.Portable.csproj @@ -62,6 +62,7 @@ + diff --git a/SharpCompress/SharpCompress.PortableTest.csproj b/SharpCompress/SharpCompress.PortableTest.csproj index 9dd72d42..99959ed5 100644 --- a/SharpCompress/SharpCompress.PortableTest.csproj +++ b/SharpCompress/SharpCompress.PortableTest.csproj @@ -85,6 +85,7 @@ + diff --git a/SharpCompress/SharpCompress.WindowsStore.csproj b/SharpCompress/SharpCompress.WindowsStore.csproj index ff2dc34c..54dcba7f 100644 --- a/SharpCompress/SharpCompress.WindowsStore.csproj +++ b/SharpCompress/SharpCompress.WindowsStore.csproj @@ -46,6 +46,7 @@ + diff --git a/SharpCompress/SharpCompress.csproj b/SharpCompress/SharpCompress.csproj index 71bab35b..bbafd71b 100644 --- a/SharpCompress/SharpCompress.csproj +++ b/SharpCompress/SharpCompress.csproj @@ -88,6 +88,7 @@ +