Compare commits

..

11 Commits
0.32 ... 0.32.1

Author SHA1 Message Date
Adam Hathcock
c0e43cc0e5 Mark for 0.32.1 2022-06-20 10:32:47 +01:00
Adam Hathcock
514c3539e6 Merge pull request #672 from MartinDemberger/Task_477
Corrected skip-marker on skip of uncompressed ZIP file with missing size informations.
2022-06-20 10:31:31 +01:00
Adam Hathcock
62c94a178c Merge branch 'master' into Task_477 2022-06-20 10:26:45 +01:00
Adam Hathcock
9fee38b18d Merge pull request #674 from MartinDemberger/DeduplicateNonDisposing
Suppress nested NonDisposingStream
2022-06-20 10:25:25 +01:00
Adam Hathcock
cd3114d39e Merge branch 'master' into DeduplicateNonDisposing 2022-06-20 10:20:02 +01:00
Adam Hathcock
12b4e15812 Merge pull request #673 from Erior/feature/Malformed-zip-file-generated
Feature/malformed zip file generated
2022-06-20 10:19:41 +01:00
Martin Demberger
35336a0827 Suppress nested NonDisposingStream 2022-06-19 22:05:52 +02:00
Martin Demberger
ece7cbfec3 Set skip-marker when stream is skipped 2022-06-18 14:35:14 +02:00
Lars Vahlenberg
a00075ee0d Wrong flags set, we do not expose this in the interface 2022-06-17 15:07:07 +02:00
Lars Vahlenberg
b6c4e28b4d Generated test case, however, don't see any problems 2022-06-16 23:32:46 +02:00
Martin Demberger
8b55cce39a Better handling of uncompressed zip files. 2022-06-15 16:28:14 +02:00
27 changed files with 131 additions and 81 deletions

View File

@@ -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
}
}
}
}
}

View File

@@ -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
}
}
}
}
}

View File

@@ -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
}
}
}
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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
{

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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();
}
}
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);

View File

@@ -2,9 +2,9 @@
<PropertyGroup>
<AssemblyTitle>SharpCompress - Pure C# Decompression/Compression</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>0.32.0</VersionPrefix>
<AssemblyVersion>0.32.0</AssemblyVersion>
<FileVersion>0.32.0</FileVersion>
<VersionPrefix>0.32.1</VersionPrefix>
<AssemblyVersion>0.32.1</AssemblyVersion>
<FileVersion>0.32.1</FileVersion>
<Authors>Adam Hathcock</Authors>
<TargetFrameworks>net461;netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

View File

@@ -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;
}
}
}
}

View File

@@ -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)
{

View File

@@ -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));

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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))
{

View File

@@ -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);
}

View File

@@ -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()
{

View File

@@ -713,5 +713,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())
;
}
}
}
}

View File

@@ -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())

View File

@@ -1,4 +1,4 @@
using System.Text;
using System.Text;
using SharpCompress.Common;
using Xunit;

Binary file not shown.