Skip to content

Commit 634e562

Browse files
authored
Merge pull request #935 from Nanook/bugfix/StreamBufferFix
Rewind buffer fix for directory extract.
2 parents d23560a + c789ead commit 634e562

6 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/SharpCompress/Archives/Zip/ZipArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ bool closeStream
330330
protected override IReader CreateReaderForSolidExtraction()
331331
{
332332
var stream = Volumes.Single().Stream;
333-
stream.Position = 0;
333+
((IStreamStack)stream).StackSeek(0);
334334
return ZipReader.Open(stream, ReaderOptions, Entries);
335335
}
336336
}

src/SharpCompress/Common/Zip/StreamingZipFilePart.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ internal BinaryReader FixStreamedFileLocation(ref SharpCompressStream rewindable
5050
if (_decompressionStream is DeflateStream deflateStream)
5151
{
5252
((IStreamStack)rewindableStream).StackSeek(0);
53-
//rewindableStream.Rewind(deflateStream.InputBuffer);
5453
}
5554

5655
Skipped = true;

src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ internal IEnumerable<ZipHeader> ReadStreamHeader(Stream stream)
2222
{
2323
if (stream is not SharpCompressStream) //ensure the stream is already a SharpCompressStream. So the buffer/size will already be set
2424
{
25-
throw new ArgumentException("Stream must be a SharpCompressStream", nameof(stream));
25+
//the original code wrapped this with RewindableStream. Wrap with SharpCompressStream as we can get the buffer size
26+
if (stream is SourceStream src)
27+
{
28+
stream = new SharpCompressStream(
29+
stream,
30+
src.ReaderOptions.LeaveStreamOpen,
31+
bufferSize: src.ReaderOptions.BufferSize
32+
);
33+
}
34+
else
35+
{
36+
throw new ArgumentException("Stream must be a SharpCompressStream", nameof(stream));
37+
}
2638
}
2739
SharpCompressStream rewindableStream = (SharpCompressStream)stream;
2840

tests/SharpCompress.Test/ArchiveTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,25 @@ IEnumerable<string> testArchives
252252
}
253253
}
254254

255+
protected void ArchiveExtractToDirectory(
256+
string testArchive,
257+
ReaderOptions? readerOptions = null
258+
) => ArchiveExtractToDirectory(ArchiveFactory.AutoFactory, testArchive, readerOptions);
259+
260+
protected void ArchiveExtractToDirectory(
261+
IArchiveFactory archiveFactory,
262+
string testArchive,
263+
ReaderOptions? readerOptions = null
264+
)
265+
{
266+
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
267+
using (var archive = archiveFactory.Open(new FileInfo(testArchive), readerOptions))
268+
{
269+
archive.ExtractToDirectory(SCRATCH_FILES_PATH);
270+
}
271+
VerifyFiles();
272+
}
273+
255274
protected void ArchiveFileRead(
256275
IArchiveFactory archiveFactory,
257276
string testArchive,

tests/SharpCompress.Test/ReaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ReaderOptions options
4747
{
4848
UseReader(reader, expectedCompression);
4949
protectedStream.ThrowOnDispose = false;
50-
Assert.False(testStream.IsDisposed, "{nameof(testStream)} prematurely closed");
50+
Assert.False(testStream.IsDisposed, $"{nameof(testStream)} prematurely closed");
5151
}
5252

5353
// Boolean XOR -- If the stream should be left open (true), then the stream should not be diposed (false)

tests/SharpCompress.Test/Zip/ZipArchiveTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public void WinZip26_X_Multi_ArchiveFileRead() =>
8888
[Fact]
8989
public void Zip_Deflate_ArchiveFileRead() => ArchiveFileRead("Zip.deflate.zip");
9090

91+
[Fact]
92+
public void Zip_Deflate_ArchiveExtractToDirectory() =>
93+
ArchiveExtractToDirectory("Zip.deflate.zip");
94+
9195
//will detect and load other files
9296
[Fact]
9397
public void Zip_Deflate_Multi_ArchiveFirstFileRead() =>

0 commit comments

Comments
 (0)