diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 0c3c53f9..debf6fd1 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -12,9 +12,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-dotnet@v2 with: - dotnet-version: | - 5.0.x - 6.0.x + dotnet-version: 6.0.x - run: dotnet run -p build/build.csproj - uses: actions/upload-artifact@v3 with: diff --git a/build/Program.cs b/build/Program.cs index b406cdd6..f5c91cbe 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -52,7 +52,7 @@ Target(Build, Target(Test, DependsOn(Build), - ForEach("net5.0", "net6.0", "net461"), + ForEach("net6.0", "net461"), framework => { IEnumerable GetFiles(string d) diff --git a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs index 620d0802..4a315571 100644 --- a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable disable using System; using System.Collections.Generic; @@ -54,7 +54,7 @@ namespace SharpCompress.Archives.GZip { //ensure new stream is at the start, this could be reset stream.Seek(0, SeekOrigin.Begin); - return new NonDisposingStream(stream); + return NonDisposingStream.Create(stream); } internal override void Close() @@ -65,4 +65,4 @@ namespace SharpCompress.Archives.GZip } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs b/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs index a776b44b..1f96ca3d 100644 --- a/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable disable using System; using System.Collections.Generic; @@ -53,7 +53,7 @@ namespace SharpCompress.Archives.Tar { //ensure new stream is at the start, this could be reset stream.Seek(0, SeekOrigin.Begin); - return new NonDisposingStream(stream); + return NonDisposingStream.Create(stream); } internal override void Close() @@ -64,4 +64,4 @@ namespace SharpCompress.Archives.Tar } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs b/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs index 4cd1fe61..7a91993a 100644 --- a/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using SharpCompress.Common; @@ -53,7 +53,7 @@ namespace SharpCompress.Archives.Zip { //ensure new stream is at the start, this could be reset stream.Seek(0, SeekOrigin.Begin); - return new NonDisposingStream(stream); + return NonDisposingStream.Create(stream); } internal override void Close() @@ -65,4 +65,4 @@ namespace SharpCompress.Archives.Zip } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Common/Volume.cs b/src/SharpCompress/Common/Volume.cs index b937b281..f1b93d02 100644 --- a/src/SharpCompress/Common/Volume.cs +++ b/src/SharpCompress/Common/Volume.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using SharpCompress.IO; using SharpCompress.Readers; @@ -14,7 +14,7 @@ namespace SharpCompress.Common ReaderOptions = readerOptions; if (readerOptions.LeaveStreamOpen) { - stream = new NonDisposingStream(stream); + stream = NonDisposingStream.Create(stream); } _actualStream = stream; } @@ -48,4 +48,4 @@ namespace SharpCompress.Common GC.SuppressFinalize(this); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs index f94413f9..fbffe179 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using SharpCompress.Common.Zip.Headers; using SharpCompress.Compressors.Deflate; using SharpCompress.IO; @@ -28,7 +28,7 @@ namespace SharpCompress.Common.Zip _decompressionStream = CreateDecompressionStream(GetCryptoStream(CreateBaseStream()), Header.CompressionMethod); if (LeaveStreamOpen) { - return new NonDisposingStream(_decompressionStream); + return NonDisposingStream.Create(_decompressionStream); } return _decompressionStream; } @@ -56,4 +56,4 @@ namespace SharpCompress.Common.Zip return reader; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index 937c1a8f..5fd6c4ac 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Buffers.Binary; using System.IO; using System.Linq; @@ -37,7 +37,7 @@ namespace SharpCompress.Common.Zip Stream decompressionStream = CreateDecompressionStream(GetCryptoStream(CreateBaseStream()), Header.CompressionMethod); if (LeaveStreamOpen) { - return new NonDisposingStream(decompressionStream); + return NonDisposingStream.Create(decompressionStream); } return decompressionStream; } @@ -142,7 +142,7 @@ namespace SharpCompress.Common.Zip && FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor)) || Header.IsZip64) { - plainStream = new NonDisposingStream(plainStream); //make sure AES doesn't close + plainStream = NonDisposingStream.Create(plainStream); //make sure AES doesn't close } else { diff --git a/src/SharpCompress/Compressors/Xz/XZFooter.cs b/src/SharpCompress/Compressors/Xz/XZFooter.cs index 3d070fe6..52f72619 100644 --- a/src/SharpCompress/Compressors/Xz/XZFooter.cs +++ b/src/SharpCompress/Compressors/Xz/XZFooter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Text; @@ -22,7 +22,7 @@ namespace SharpCompress.Compressors.Xz public static XZFooter FromStream(Stream stream) { - var footer = new XZFooter(new BinaryReader(new NonDisposingStream(stream), Encoding.UTF8)); + var footer = new XZFooter(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8)); footer.Process(); return footer; } diff --git a/src/SharpCompress/Compressors/Xz/XZHeader.cs b/src/SharpCompress/Compressors/Xz/XZHeader.cs index 587f20da..8934e135 100644 --- a/src/SharpCompress/Compressors/Xz/XZHeader.cs +++ b/src/SharpCompress/Compressors/Xz/XZHeader.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Text; @@ -21,7 +21,7 @@ namespace SharpCompress.Compressors.Xz public static XZHeader FromStream(Stream stream) { - var header = new XZHeader(new BinaryReader(new NonDisposingStream(stream), Encoding.UTF8)); + var header = new XZHeader(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8)); header.Process(); return header; } diff --git a/src/SharpCompress/Compressors/Xz/XZIndex.cs b/src/SharpCompress/Compressors/Xz/XZIndex.cs index d634db0c..997d53c3 100644 --- a/src/SharpCompress/Compressors/Xz/XZIndex.cs +++ b/src/SharpCompress/Compressors/Xz/XZIndex.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -30,7 +30,7 @@ namespace SharpCompress.Compressors.Xz public static XZIndex FromStream(Stream stream, bool indexMarkerAlreadyVerified) { - var index = new XZIndex(new BinaryReader(new NonDisposingStream(stream), Encoding.UTF8), indexMarkerAlreadyVerified); + var index = new XZIndex(new BinaryReader(NonDisposingStream.Create(stream), Encoding.UTF8), indexMarkerAlreadyVerified); index.Process(); return index; } diff --git a/src/SharpCompress/IO/NonDisposingStream.cs b/src/SharpCompress/IO/NonDisposingStream.cs index 9b326c99..12b9a6cf 100644 --- a/src/SharpCompress/IO/NonDisposingStream.cs +++ b/src/SharpCompress/IO/NonDisposingStream.cs @@ -1,11 +1,20 @@ -using System; +using System; using System.IO; namespace SharpCompress.IO { public class NonDisposingStream : Stream { - public NonDisposingStream(Stream stream, bool throwOnDispose = false) + public static NonDisposingStream Create(Stream stream, bool throwOnDispose = false) + { + if (stream is NonDisposingStream nonDisposingStream && nonDisposingStream.ThrowOnDispose == throwOnDispose) + { + return nonDisposingStream; + } + return new NonDisposingStream(stream, throwOnDispose); + } + + protected NonDisposingStream(Stream stream, bool throwOnDispose = false) { Stream = stream; ThrowOnDispose = throwOnDispose; diff --git a/src/SharpCompress/IO/ReadOnlySubStream.cs b/src/SharpCompress/IO/ReadOnlySubStream.cs index e05921ca..4ba326dd 100644 --- a/src/SharpCompress/IO/ReadOnlySubStream.cs +++ b/src/SharpCompress/IO/ReadOnlySubStream.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; namespace SharpCompress.IO @@ -65,6 +65,19 @@ namespace SharpCompress.IO return value; } +#if !NETFRAMEWORK && !NETSTANDARD2_0 + public override int Read(Span buffer) + { + var slice_len = BytesLeftToRead < buffer.Length ? BytesLeftToRead : buffer.Length; + var read = Stream.Read(buffer.Slice(0,(int)slice_len)); + if (read > 0) + { + BytesLeftToRead -= read; + } + return read; + } +#endif + public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); @@ -80,4 +93,4 @@ namespace SharpCompress.IO throw new NotSupportedException(); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/IO/RewindableStream.cs b/src/SharpCompress/IO/RewindableStream.cs index e609fbab..fc430aae 100644 --- a/src/SharpCompress/IO/RewindableStream.cs +++ b/src/SharpCompress/IO/RewindableStream.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; namespace SharpCompress.IO @@ -119,8 +119,10 @@ namespace SharpCompress.IO int read; if (isRewound && bufferStream.Position != bufferStream.Length) { - read = bufferStream.Read(buffer, offset, count); - if (read < count) + // don't read more than left + int readCount = Math.Min(count, (int)(bufferStream.Length - bufferStream.Position)); + read = bufferStream.Read(buffer, offset, readCount); + if (read < readCount) { int tempRead = stream.Read(buffer, offset + read, count - read); if (IsRecording) @@ -160,4 +162,4 @@ namespace SharpCompress.IO throw new NotSupportedException(); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/IO/SourceStream.cs b/src/SharpCompress/IO/SourceStream.cs index c13c0269..71918c81 100644 --- a/src/SharpCompress/IO/SourceStream.cs +++ b/src/SharpCompress/IO/SourceStream.cs @@ -144,7 +144,8 @@ namespace SharpCompress.IO if (!IsVolumes && count != 0 && Current.Position == Current.Length) { _prevSize += Current.Length; - SetStream(_stream + 1); //will load next file + if (!SetStream(_stream + 1)) //will load next file if present + break; Current.Seek(0, SeekOrigin.Begin); } } diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index fe6285ff..80e6cf07 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -131,26 +131,27 @@ namespace SharpCompress.Readers private void Skip() { + var part = Entry.Parts.First(); + part.Skipped = true; + if (ArchiveType != ArchiveType.Rar && !Entry.IsSolid && Entry.CompressedSize > 0) { //not solid and has a known compressed size then we can skip raw bytes. - var part = Entry.Parts.First(); var rawStream = part.GetRawStream(); if (rawStream != null) { var bytesToAdvance = Entry.CompressedSize; rawStream.Skip(bytesToAdvance); - part.Skipped = true; return; } } //don't know the size so we have to try to decompress to skip using (var s = OpenEntryStream()) { - s.Skip(); + s.SkipEntry(); } } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 1384264f..adff51cc 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using SharpCompress.Archives.GZip; using SharpCompress.Archives.Rar; @@ -58,7 +58,7 @@ namespace SharpCompress.Readers if (BZip2Stream.IsBZip2(rewindableStream)) { rewindableStream.Rewind(false); - BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false); + BZip2Stream testStream = new BZip2Stream(NonDisposingStream.Create(rewindableStream), CompressionMode.Decompress, false); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); @@ -70,7 +70,7 @@ namespace SharpCompress.Readers if (LZipStream.IsLZipFile(rewindableStream)) { rewindableStream.Rewind(false); - LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress); + LZipStream testStream = new LZipStream(NonDisposingStream.Create(rewindableStream), CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); diff --git a/src/SharpCompress/SharpCompress.csproj b/src/SharpCompress/SharpCompress.csproj index 47230421..f35bf43f 100644 --- a/src/SharpCompress/SharpCompress.csproj +++ b/src/SharpCompress/SharpCompress.csproj @@ -2,11 +2,11 @@ SharpCompress - Pure C# Decompression/Compression en-US - 0.31.0 - 0.31.0 - 0.31.0 + 0.32.1 + 0.32.1 + 0.32.1 Adam Hathcock - net461;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0 + net461;netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0 true false SharpCompress diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.cs b/src/SharpCompress/Writers/GZip/GZipWriter.cs index 1147b686..12e820a2 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using SharpCompress.Common; using SharpCompress.Compressors; @@ -16,7 +16,7 @@ namespace SharpCompress.Writers.GZip { if (WriterOptions.LeaveStreamOpen) { - destination = new NonDisposingStream(destination); + destination = NonDisposingStream.Create(destination); } InitalizeStream(new GZipStream(destination, CompressionMode.Compress, options?.CompressionLevel ?? CompressionLevel.Default, @@ -46,4 +46,4 @@ namespace SharpCompress.Writers.GZip _wroteToStream = true; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index d78e8692..c90e069e 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using SharpCompress.Common; using SharpCompress.Common.Tar.Headers; @@ -25,7 +25,7 @@ namespace SharpCompress.Writers.Tar } if (WriterOptions.LeaveStreamOpen) { - destination = new NonDisposingStream(destination); + destination = NonDisposingStream.Create(destination); } switch (options.CompressionType) { diff --git a/src/SharpCompress/Writers/Zip/ZipCentralDirectoryEntry.cs b/src/SharpCompress/Writers/Zip/ZipCentralDirectoryEntry.cs index 8fe87d1e..d4609ef6 100644 --- a/src/SharpCompress/Writers/Zip/ZipCentralDirectoryEntry.cs +++ b/src/SharpCompress/Writers/Zip/ZipCentralDirectoryEntry.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Buffers.Binary; using System.IO; using System.Text; @@ -98,10 +98,24 @@ namespace SharpCompress.Writers.Zip BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 0); outputStream.Write(intBuf.Slice(0, 2)); // disk=0 - BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)flags); - outputStream.Write(intBuf.Slice(0, 2)); // file type: binary - BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)flags); - outputStream.Write(intBuf.Slice(0, 2)); // Internal file attributes + + // Internal file attributes: + // Bit 0: apparent ASCII/ text file + // Bit 1: reserved + // Bit 2: control field records precede logical records + // Bits 3 - 16: unused + BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 0); + outputStream.Write(intBuf.Slice(0, 2)); // file type: binary, Internal file attributes + + // External flags are host-dependent, this might match DOS + // Bit 0: Read-Only + // Bit 1: Hidden + // Bit 2: System + // Bit 3: Label + // Bit 4: Directory + // Bit 5: Archive + BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 0); + outputStream.Write(intBuf.Slice(0, 2)); // External file attributes BinaryPrimitives.WriteUInt16LittleEndian(intBuf, 0x8100); outputStream.Write(intBuf.Slice(0, 2)); diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index c726c4fb..7ee74e19 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Buffers.Binary; using System.Collections.Generic; using System.IO; @@ -40,7 +40,7 @@ namespace SharpCompress.Writers.Zip if (WriterOptions.LeaveStreamOpen) { - destination = new NonDisposingStream(destination); + destination = NonDisposingStream.Create(destination); } InitalizeStream(destination); } diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 0bf586e9..bfd88569 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -32,31 +32,42 @@ namespace SharpCompress.Test { foreach (var path in testArchives) { - using (var stream = new NonDisposingStream(File.OpenRead(path), true)) - using (var archive = ArchiveFactory.Open(stream)) + using (var stream = NonDisposingStream.Create(File.OpenRead(path), true)) { - Assert.True(archive.IsSolid); - using (var reader = archive.ExtractAllEntries()) + try { - UseReader(reader, compression); - } - VerifyFiles(); + using (var archive = ArchiveFactory.Open(stream)) + { + Assert.True(archive.IsSolid); + using (var reader = archive.ExtractAllEntries()) + { + UseReader(reader, compression); + } + VerifyFiles(); - if (archive.Entries.First().CompressionType == CompressionType.Rar) + if (archive.Entries.First().CompressionType == CompressionType.Rar) + { + stream.ThrowOnDispose = false; + return; + } + foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) + { + entry.WriteToDirectory(SCRATCH_FILES_PATH, + new ExtractionOptions + { + ExtractFullPath = true, + Overwrite = true + }); + } + stream.ThrowOnDispose = false; + } + } + catch (Exception) { + // Otherwise this will hide the original exception. stream.ThrowOnDispose = false; - return; + throw; } - foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) - { - entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractionOptions - { - ExtractFullPath = true, - Overwrite = true - }); - } - stream.ThrowOnDispose = false; } VerifyFiles(); } @@ -77,7 +88,7 @@ namespace SharpCompress.Test { foreach (var path in testArchives) { - using (var stream = new NonDisposingStream(File.OpenRead(path), true)) + using (var stream = NonDisposingStream.Create(File.OpenRead(path), true)) using (var archive = ArchiveFactory.Open(stream, readerOptions)) { try diff --git a/tests/SharpCompress.Test/ReaderTests.cs b/tests/SharpCompress.Test/ReaderTests.cs index 82ca6d29..d5cfd9dd 100644 --- a/tests/SharpCompress.Test/ReaderTests.cs +++ b/tests/SharpCompress.Test/ReaderTests.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Readers; @@ -27,7 +27,7 @@ namespace SharpCompress.Test { using (var file = File.OpenRead(testArchive)) { - using (var protectedStream = new NonDisposingStream(new ForwardOnlyStream(file), throwOnDispose: true)) + using (var protectedStream = NonDisposingStream.Create(new ForwardOnlyStream(file), throwOnDispose: true)) { using (var testStream = new TestStream(protectedStream)) { diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index 35c3134a..a182e0a4 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -1,6 +1,6 @@  - net5.0;net6.0;net461 + net6.0;net461 SharpCompress.Test ../../SharpCompress.snk true diff --git a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs index c8d55283..51d2085b 100644 --- a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs @@ -63,7 +63,7 @@ namespace SharpCompress.Test.Streams private void Compress(Stream input, Stream output, int compressionLevel) { - using (var zlibStream = new ZlibStream(new NonDisposingStream(output), CompressionMode.Compress, (CompressionLevel)compressionLevel)) + using (var zlibStream = new ZlibStream(NonDisposingStream.Create(output), CompressionMode.Compress, (CompressionLevel)compressionLevel)) { zlibStream.FlushMode = FlushType.Sync; input.CopyTo(zlibStream); @@ -72,7 +72,7 @@ namespace SharpCompress.Test.Streams private void Decompress(Stream input, Stream output) { - using (var zlibStream = new ZlibStream(new NonDisposingStream(input), CompressionMode.Decompress)) + using (var zlibStream = new ZlibStream(NonDisposingStream.Create(input), CompressionMode.Decompress)) { zlibStream.CopyTo(output); } diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index 2d99325d..53c66307 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using System.Text; using SharpCompress.Common; using SharpCompress.IO; @@ -41,7 +41,7 @@ namespace SharpCompress.Test readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using (var reader = ReaderFactory.Open(new NonDisposingStream(stream), readerOptions)) + using (var reader = ReaderFactory.Open(NonDisposingStream.Create(stream), readerOptions)) { reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index 0fc9d168..dfe68c30 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -680,6 +680,28 @@ namespace SharpCompress.Test.Zip firstStream.CopyTo(memoryStream); Assert.Equal(199, memoryStream.Length); } + + var len1 = 0; + var buffer1 = new byte[firstEntry.Size + 256]; + + using (var firstStream = firstEntry.OpenEntryStream()) + { + len1 = firstStream.Read(buffer1, 0, buffer.Length); + } + + Assert.Equal(199, len1); + +#if !NETFRAMEWORK && !NETSTANDARD2_0 + var len2 = 0; + var buffer2 = new byte[firstEntry.Size + 256]; + + using (var firstStream = firstEntry.OpenEntryStream()) + { + len2 = firstStream.Read(buffer2.AsSpan()); + } + Assert.Equal(len1, len2); + Assert.Equal(buffer1, buffer2); +#endif } } @@ -713,5 +735,17 @@ namespace SharpCompress.Test.Zip } } + [Fact] + public void Zip_Uncompressed_Skip_All() + { + string zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); + using (var stream = File.Open(zipPath, FileMode.Open, FileAccess.Read)) + { + IArchive archive = ArchiveFactory.Open(stream); + IReader reader = archive.ExtractAllEntries(); + while (reader.MoveToNextEntry()) + ; + } + } } } diff --git a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs index c7eb3c41..d2b23877 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -319,7 +319,7 @@ namespace SharpCompress.Test.Zip stream = new MemoryStream(memory.ToArray()); File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), memory.ToArray()); - using (IReader zipReader = ZipReader.Open(new NonDisposingStream(stream, true))) + using (IReader zipReader = ZipReader.Open(NonDisposingStream.Create(stream, true))) { var i = 0; while (zipReader.MoveToNextEntry()) diff --git a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs index 2c70e416..8d546cec 100644 --- a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Text; using SharpCompress.Common; using Xunit; diff --git a/tests/TestArchives/Archives/Zip.uncompressed.zip b/tests/TestArchives/Archives/Zip.uncompressed.zip new file mode 100644 index 00000000..9b9aea6d Binary files /dev/null and b/tests/TestArchives/Archives/Zip.uncompressed.zip differ