Use https://www.nuget.org/packages/StreamingZipReader instead of this! @bjornharrtell is kindly maintaining it, and has already implemented ZIP64 support which is missing in the proof of concept below.
Last active
May 26, 2024 16:01
-
-
Save jnm2/31bdf08357a44c91d01736ad43b9c447 to your computer and use it in GitHub Desktop.
StreamingZipReader proof of concept for https://github.com/dotnet/runtime/issues/59027
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2021 Joseph Musser | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Buffers.Binary; | |
using System.Diagnostics; | |
using System.Text; | |
[DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")] | |
internal ref struct SpanReader | |
{ | |
private ReadOnlySpan<byte> span; | |
public SpanReader(ReadOnlySpan<byte> span) | |
{ | |
this.span = span; | |
} | |
public int RemainingByteCount => span.Length; | |
public void Skip(int byteCount) | |
{ | |
span = span[byteCount..]; | |
} | |
public bool ReadFixedValue(ReadOnlySpan<byte> value) | |
{ | |
if (!span.StartsWith(value)) return false; | |
span = span[value.Length..]; | |
return true; | |
} | |
public ushort ReadUInt16LittleEndian() | |
{ | |
var value = BinaryPrimitives.ReadUInt16LittleEndian(span); | |
span = span[sizeof(ushort)..]; | |
return value; | |
} | |
public uint ReadUInt32LittleEndian() | |
{ | |
var value = BinaryPrimitives.ReadUInt32LittleEndian(span); | |
span = span[sizeof(uint)..]; | |
return value; | |
} | |
public ReadOnlySpan<byte> ReadBytes(int byteCount) | |
{ | |
var value = span[..byteCount]; | |
span = span[byteCount..]; | |
return value; | |
} | |
private string GetDebuggerDisplay() | |
{ | |
const int maxDisplayedByteCount = 32; | |
var builder = new StringBuilder(); | |
builder.Append(span.Length); | |
builder.Append(" bytes left"); | |
if (span.Length != 0) | |
{ | |
builder.Append(": "); | |
foreach (var value in span.Length > maxDisplayedByteCount ? span[..maxDisplayedByteCount] : span) | |
builder.Append(value.ToString("X2")); | |
if (span.Length > maxDisplayedByteCount) | |
builder.Append('…'); | |
} | |
return builder.ToString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class StreamingZipEntry | |
{ | |
public StreamingZipEntry(string name, uint crc32, uint compressedLength, uint length) | |
{ | |
Name = name; | |
Crc32 = crc32; | |
CompressedLength = compressedLength; | |
Length = length; | |
} | |
public string Name { get; } | |
public uint Crc32 { get; } | |
public uint CompressedLength { get; } | |
public uint Length { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Buffers; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public sealed partial class StreamingZipReader : IAsyncDisposable | |
{ | |
private static readonly byte[] LocalFileHeader = { (byte)'P', (byte)'K', 3, 4 }; | |
private static readonly byte[] CentralDirectoryHeader = { (byte)'P', (byte)'K', 1, 2 }; | |
private readonly bool leaveOpen; | |
private Stream? stream; | |
private StreamingZipEntry? currentEntry; | |
private SubStream? currentSubStream; | |
private DeflateStream? currentDeflateStream; | |
public StreamingZipReader(Stream stream, bool leaveOpen = false) | |
{ | |
if (!stream.CanRead) | |
throw new ArgumentException("The stream must be readable.", nameof(stream)); | |
this.stream = stream; | |
this.leaveOpen = leaveOpen; | |
} | |
public ValueTask DisposeAsync() | |
{ | |
return leaveOpen || stream is null ? ValueTask.CompletedTask : stream.DisposeAsync(); | |
} | |
public async ValueTask<bool> MoveToNextEntryAsync(bool skipDirectories, CancellationToken cancellationToken) | |
{ | |
if (stream is null) | |
return false; | |
var remainingLength = 0L; | |
if (currentSubStream is not null) | |
{ | |
currentSubStream.Detach(); | |
remainingLength = currentSubStream.Length - currentSubStream.Position; | |
currentSubStream = null; | |
currentDeflateStream = null; | |
} | |
else if (currentEntry is not null) | |
{ | |
remainingLength = currentEntry.CompressedLength; | |
} | |
if (remainingLength != 0) | |
{ | |
if (stream.CanSeek) | |
{ | |
stream.Seek(remainingLength, SeekOrigin.Current); | |
} | |
else | |
{ | |
var skipArray = ArrayPool<byte>.Shared.Rent(checked((int)remainingLength)); | |
await ReadBlockAsync(stream, skipArray.AsMemory(0, (int)remainingLength), cancellationToken).ConfigureAwait(false); | |
ArrayPool<byte>.Shared.Return(skipArray); | |
} | |
} | |
readEntry: | |
var array = ArrayPool<byte>.Shared.Rent(30); | |
var buffer = array.AsMemory(0, 30); | |
var bytesRead = await ReadBlockAsync(stream, buffer, cancellationToken).ConfigureAwait(false); | |
try | |
{ | |
if (bytesRead == 0) | |
return false; | |
else if (bytesRead < buffer.Length) | |
throw new InvalidDataException("The stream ended unexpectedly or is not a .zip archive having no bytes around file entries."); | |
if (buffer.Span.StartsWith(CentralDirectoryHeader)) | |
{ | |
if (!leaveOpen) await stream.DisposeAsync().ConfigureAwait(false); | |
stream = null; | |
currentEntry = null; | |
return false; | |
} | |
} | |
finally | |
{ | |
ArrayPool<byte>.Shared.Return(array); | |
} | |
var (crc32, compressedSize, uncompressedSize, fileNameLength, extraFieldLength) = ParseMinimumLocalFileHeader(buffer.Span); | |
array = ArrayPool<byte>.Shared.Rent(fileNameLength + extraFieldLength); | |
buffer = array.AsMemory(0, fileNameLength + extraFieldLength); | |
bytesRead = await ReadBlockAsync(stream, buffer, cancellationToken).ConfigureAwait(false); | |
try | |
{ | |
if (bytesRead < buffer.Length) | |
throw new InvalidDataException("The stream ended unexpectedly."); | |
if (skipDirectories && compressedSize == 0 && buffer.Span[fileNameLength - 1] == '/') | |
goto readEntry; | |
var fileName = Encoding.ASCII.GetString(buffer.Span[..fileNameLength]); | |
currentEntry = new(fileName, crc32, compressedSize, uncompressedSize); | |
} | |
finally | |
{ | |
ArrayPool<byte>.Shared.Return(array); | |
} | |
return true; | |
} | |
private static ( | |
uint Crc32, | |
uint CompressedSize, | |
uint UncompressedSize, | |
ushort FileNameLength, | |
ushort ExtraFieldLength) ParseMinimumLocalFileHeader(ReadOnlySpan<byte> span) | |
{ | |
var reader = new SpanReader(span); | |
if (!reader.ReadFixedValue(LocalFileHeader)) | |
throw new InvalidDataException("The stream is not a .zip archive having no bytes around file entries."); | |
var version = reader.ReadUInt16LittleEndian(); | |
if (version > 20) | |
throw new NotSupportedException("Zip format versions greater than 2.0 have not been tested."); | |
var flags = reader.ReadUInt16LittleEndian(); | |
if (flags != 0) | |
throw new NotImplementedException("Unknown flags"); | |
var compressionMethod = reader.ReadUInt16LittleEndian(); | |
// Last modified time and date (use 0x5455 "extended timestamp" extra field extension if possible first) | |
reader.Skip(4); | |
var crc32 = reader.ReadUInt32LittleEndian(); | |
var compressedSize = reader.ReadUInt32LittleEndian(); | |
var uncompressedSize = reader.ReadUInt32LittleEndian(); | |
var fileNameLength = reader.ReadUInt16LittleEndian(); | |
var extraFieldLength = reader.ReadUInt16LittleEndian(); | |
if (compressedSize > 0 && compressionMethod != 8) | |
throw new NotSupportedException("Unsupported compression method"); | |
return (crc32, compressedSize, uncompressedSize, fileNameLength, extraFieldLength); | |
} | |
private static async ValueTask<int> ReadBlockAsync(Stream stream, Memory<byte> buffer, CancellationToken cancellationToken) | |
{ | |
var totalBytesRead = 0; | |
while (true) | |
{ | |
var bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); | |
if (bytesRead == 0) break; | |
totalBytesRead += bytesRead; | |
if (bytesRead == buffer.Length) break; | |
buffer = buffer[bytesRead..]; | |
} | |
return totalBytesRead; | |
} | |
public StreamingZipEntry CurrentEntry => currentEntry | |
?? throw new InvalidOperationException($"{nameof(CurrentEntry)} can only be used after {nameof(MoveToNextEntryAsync)} returns true."); | |
public Stream GetCurrentEntryStream() | |
{ | |
if (currentEntry is null) | |
throw new InvalidOperationException($"{nameof(GetCurrentEntryStream)} can only be used after {nameof(MoveToNextEntryAsync)} returns true."); | |
if (currentDeflateStream is null) | |
{ | |
currentSubStream = new SubStream(stream!, currentEntry.CompressedLength); | |
currentDeflateStream = new DeflateStream(currentSubStream, CompressionMode.Decompress); | |
} | |
return currentDeflateStream; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
partial class StreamingZipReader | |
{ | |
private sealed class SubStream : Stream | |
{ | |
private Stream? stream; | |
private long position; | |
public SubStream(Stream stream, long length) | |
{ | |
this.stream = stream; | |
Length = length; | |
} | |
public void Detach() => stream = null; | |
private static Exception CreateDetachedStreamException() | |
{ | |
return new InvalidOperationException($"A stream returned from {nameof(StreamingZipReader)}.{nameof(GetCurrentEntryStream)} may not be used after calling {nameof(StreamingZipReader)}.{nameof(MoveToNextEntryAsync)} again."); | |
} | |
public override int Read(byte[] buffer, int offset, int count) | |
{ | |
return Read(buffer.AsSpan(offset, count)); | |
} | |
public override int Read(Span<byte> buffer) | |
{ | |
var remainingLength = Length - Position; | |
if (remainingLength == 0) | |
{ | |
// This stream instance is at the end even though the inner stream may not be. | |
return 0; | |
} | |
if (buffer.Length > remainingLength) | |
buffer = buffer[..(int)remainingLength]; | |
if (stream is null) throw CreateDetachedStreamException(); | |
var byteCount = stream.Read(buffer); | |
position += byteCount; | |
return byteCount; | |
} | |
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | |
{ | |
return ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); | |
} | |
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | |
{ | |
var remainingLength = Length - Position; | |
if (remainingLength == 0) | |
{ | |
// This stream instance is at the end even though the inner stream may not be. | |
return 0; | |
} | |
if (buffer.Length > remainingLength) | |
buffer = buffer[..(int)remainingLength]; | |
if (stream is null) throw CreateDetachedStreamException(); | |
var byteCount = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); | |
position += byteCount; | |
return byteCount; | |
} | |
public override bool CanRead => true; | |
public override bool CanSeek => false; | |
public override bool CanWrite => false; | |
public override long Length { get; } | |
public override long Position { get => position; set => throw new NotSupportedException(); } | |
public override void Flush() => throw new NotSupportedException(); | |
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | |
public override void SetLength(long value) => throw new NotSupportedException(); | |
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done! https://www.nuget.org/packages/StreamingZipReader