mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-23 23:45:14 +00:00
more fixes?
This commit is contained in:
@@ -35,7 +35,7 @@ public partial class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVo
|
||||
protected override IEnumerable<ZipVolume> LoadVolumes(SourceStream stream)
|
||||
{
|
||||
stream.LoadAllParts();
|
||||
stream.Position = 0;
|
||||
//stream.Position = 0;
|
||||
|
||||
var streams = stream.Streams.ToList();
|
||||
var idx = 0;
|
||||
@@ -156,7 +156,7 @@ public partial class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVo
|
||||
protected override IReader CreateReaderForSolidExtraction()
|
||||
{
|
||||
var stream = Volumes.Single().Stream;
|
||||
((IStreamStack)stream).StackSeek(0);
|
||||
//stream.Position = 0;
|
||||
return ZipReader.OpenReader(stream, ReaderOptions, Entries);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ namespace SharpCompress.Common
|
||||
|
||||
// Use the stream directly without wrapping in BufferedStream
|
||||
// BufferedStream uses synchronous Read internally which doesn't work with async-only streams
|
||||
// SharpCompress uses SharpCompressStream for buffering which supports true async reads
|
||||
_stream = stream;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ public abstract class RarVolume : Volume
|
||||
if (fh.FileName == "CMT")
|
||||
{
|
||||
var buffer = new byte[fh.CompressedSize];
|
||||
fh.PackedStream.NotNull().ReadFully(buffer);
|
||||
await fh.PackedStream.NotNull().ReadFullyAsync(buffer, cancellationToken);
|
||||
Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ public abstract partial class Volume : IVolume, IAsyncDisposable
|
||||
Index = index;
|
||||
ReaderOptions = readerOptions ?? new ReaderOptions();
|
||||
_baseStream = stream;
|
||||
|
||||
if (stream is RewindableStream ss)
|
||||
{
|
||||
ss.Rewind();
|
||||
}
|
||||
if (ReaderOptions.LeaveStreamOpen)
|
||||
{
|
||||
stream = new NonDisposingStream(stream);
|
||||
}
|
||||
|
||||
if (stream is IStreamStack ss)
|
||||
{
|
||||
ss.SetBuffer(ReaderOptions.BufferSize, true);
|
||||
}
|
||||
|
||||
_actualStream = stream;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart
|
||||
return _decompressionStream;
|
||||
}
|
||||
|
||||
internal BinaryReader FixStreamedFileLocation(ref SharpCompressStream rewindableStream)
|
||||
internal BinaryReader FixStreamedFileLocation(ref Stream rewindableStream)
|
||||
{
|
||||
if (Header.IsDirectory)
|
||||
{
|
||||
@@ -49,7 +49,7 @@ internal sealed partial class StreamingZipFilePart : ZipFilePart
|
||||
|
||||
if (_decompressionStream is DeflateStream deflateStream)
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(0);
|
||||
rewindableStream.Position = 0;
|
||||
}
|
||||
|
||||
Skipped = true;
|
||||
|
||||
@@ -594,7 +594,7 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
_stream.Flush();
|
||||
}
|
||||
//rewind the buffer
|
||||
((IStreamStack)this).Rewind(z.AvailableBytesIn); //unused
|
||||
//unused: ((IStreamStack)this).Rewind(z.AvailableBytesIn);
|
||||
z.AvailableBytesIn = 0;
|
||||
}
|
||||
|
||||
@@ -608,7 +608,7 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
await _stream.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
//rewind the buffer
|
||||
((IStreamStack)this).Rewind(z.AvailableBytesIn); //unused
|
||||
//unused: ((IStreamStack)this).Rewind(z.AvailableBytesIn);
|
||||
z.AvailableBytesIn = 0;
|
||||
}
|
||||
|
||||
@@ -984,7 +984,7 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
if (rc == ZlibConstants.Z_STREAM_END && z.AvailableBytesIn != 0 && !_wantCompress)
|
||||
{
|
||||
//rewind the buffer
|
||||
((IStreamStack)this).Rewind(z.AvailableBytesIn); //unused
|
||||
//unused: ((IStreamStack)this).Rewind(z.AvailableBytesIn);
|
||||
z.AvailableBytesIn = 0;
|
||||
}
|
||||
|
||||
@@ -1176,7 +1176,7 @@ internal class ZlibBaseStream : Stream, IStreamStack
|
||||
if (rc == ZlibConstants.Z_STREAM_END && z.AvailableBytesIn != 0 && !_wantCompress)
|
||||
{
|
||||
//rewind the buffer
|
||||
((IStreamStack)this).Rewind(z.AvailableBytesIn); //unused
|
||||
//unused: ((IStreamStack)this).Rewind(z.AvailableBytesIn);
|
||||
z.AvailableBytesIn = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ public sealed partial class LZipStream : Stream, IStreamStack
|
||||
var dSize = 104 * 1024;
|
||||
WriteHeaderSize(stream);
|
||||
|
||||
_countingWritableSubStream = new CountingStream(stream, leaveOpen: true);
|
||||
_countingWritableSubStream = new CountingStream(new NonDisposingStream(stream));
|
||||
_stream = new Crc32Stream(
|
||||
LzmaStream.Create(
|
||||
new LzmaEncoderProperties(true, dSize),
|
||||
|
||||
69
src/SharpCompress/IO/CountingStream.cs
Normal file
69
src/SharpCompress/IO/CountingStream.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCompress.IO;
|
||||
|
||||
/// <summary>
|
||||
/// A simple stream wrapper that counts bytes written without buffering.
|
||||
/// </summary>
|
||||
internal class CountingStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
private readonly bool _leaveOpen;
|
||||
private long _bytesWritten;
|
||||
|
||||
public CountingStream(Stream stream)
|
||||
{
|
||||
_stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of bytes written to this stream.
|
||||
/// </summary>
|
||||
public long BytesWritten => _bytesWritten;
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
|
||||
public override bool CanSeek => _stream.CanSeek;
|
||||
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _stream.Position;
|
||||
set => _stream.Position = value;
|
||||
}
|
||||
|
||||
public override void Flush() => _stream.Flush();
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) =>
|
||||
_stream.Read(buffer, offset, count);
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) =>
|
||||
_stream.Seek(offset, origin);
|
||||
|
||||
public override void SetLength(long value) => _stream.SetLength(value);
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_stream.Write(buffer, offset, count);
|
||||
_bytesWritten += count;
|
||||
}
|
||||
|
||||
public override void WriteByte(byte value)
|
||||
{
|
||||
_stream.WriteByte(value);
|
||||
_bytesWritten++;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && !_leaveOpen)
|
||||
{
|
||||
_stream.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace SharpCompress.IO;
|
||||
/// This is useful when working with compression streams directly and you want
|
||||
/// to keep the base stream open after the compression stream is disposed.
|
||||
/// </summary>
|
||||
internal class NonDisposingStream : Stream
|
||||
internal class NonDisposingStream : Stream, IStreamStack
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
private bool _isDisposed;
|
||||
@@ -214,4 +214,11 @@ internal class NonDisposingStream : Stream
|
||||
throw new ObjectDisposedException(nameof(NonDisposingStream));
|
||||
}
|
||||
}
|
||||
|
||||
public int DefaultBufferSize { get; set; }
|
||||
public Stream BaseStream() => _stream;
|
||||
|
||||
public int BufferSize { get; set; }
|
||||
public int BufferPosition { get; set; }
|
||||
public void SetPosition(long position) => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -57,23 +57,23 @@ public partial class TarReader : AbstractReader<TarEntry, TarVolume>
|
||||
{
|
||||
stream.NotNull(nameof(stream));
|
||||
options = options ?? new ReaderOptions();
|
||||
var rewindableStream = new SharpCompressStream(stream);
|
||||
long pos = ((IStreamStack)rewindableStream).GetPosition();
|
||||
var rewindableStream = RewindableStream.EnsureSeekable(stream);
|
||||
long pos = rewindableStream.Position;
|
||||
if (GZipArchive.IsGZipFile(rewindableStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
var testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
return new TarReader(rewindableStream, options, CompressionType.GZip);
|
||||
}
|
||||
throw new InvalidFormatException("Not a tar file.");
|
||||
}
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
if (BZip2Stream.IsBZip2(rewindableStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
var testStream = BZip2Stream.Create(
|
||||
rewindableStream,
|
||||
CompressionMode.Decompress,
|
||||
@@ -81,36 +81,36 @@ public partial class TarReader : AbstractReader<TarEntry, TarVolume>
|
||||
);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
return new TarReader(rewindableStream, options, CompressionType.BZip2);
|
||||
}
|
||||
throw new InvalidFormatException("Not a tar file.");
|
||||
}
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
if (ZStandardStream.IsZStandard(rewindableStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
var testStream = new ZStandardStream(rewindableStream);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
return new TarReader(rewindableStream, options, CompressionType.ZStandard);
|
||||
}
|
||||
throw new InvalidFormatException("Not a tar file.");
|
||||
}
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
if (LZipStream.IsLZipFile(rewindableStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
var testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
return new TarReader(rewindableStream, options, CompressionType.LZip);
|
||||
}
|
||||
throw new InvalidFormatException("Not a tar file.");
|
||||
}
|
||||
((IStreamStack)rewindableStream).StackSeek(pos);
|
||||
rewindableStream.Position = pos;
|
||||
return new TarReader(rewindableStream, options, CompressionType.None);
|
||||
}
|
||||
|
||||
|
||||
@@ -417,7 +417,7 @@ public partial class ZipWriter : AbstractWriter
|
||||
|
||||
private Stream GetWriteStream(Stream writeStream)
|
||||
{
|
||||
counting = new CountingStream(writeStream, leaveOpen: true);
|
||||
counting = new CountingStream(new NonDisposingStream(writeStream));
|
||||
Stream output = counting;
|
||||
switch (zipCompressionMethod)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user