Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add managed Deflate64 support to ZipArchive #11264

Merged
merged 4 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/System.IO.Compression/src/System.IO.Compression.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@
<Compile Include="$(SharedOpenSourcePath)System\IO\Compression\ZipCustomStreams.cs" />
<Compile Include="$(SharedOpenSourcePath)System\IO\Compression\ZipHelper.cs" />
<Compile Include="$(SharedOpenSourcePath)System\IO\Compression\ZipVersion.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\BlockType.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\CopyEncoder.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\DeflateInput.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\DeflaterManaged.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\DeflateManagedStream.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\FastEncoder.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\FastEncoderStatus.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\FastEncoderWindow.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\FileFormats.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\HuffmanTree.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\InflaterManaged.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\InflaterState.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\InputBuffer.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\Match.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\MatchState.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\OutputBuffer.cs" />
<Compile Include="System\IO\Compression\DeflateManaged\OutputWindow.cs" />
<Compile Include="$(CommonPath)\System\IO\PathInternal.cs">
<Link>Common\System\IO\PathInternal.cs</Link>
</Compile>
Expand All @@ -39,12 +56,12 @@
<ItemGroup Condition="'$(TargetGroup)' != 'net46'">
<Compile Include="System\IO\Compression\CompressionLevel.cs" />
<Compile Include="System\IO\Compression\CompressionMode.cs" />
<Compile Include="System\IO\Compression\Deflater.cs" />
<Compile Include="System\IO\Compression\DeflateStream.cs" />
<Compile Include="System\IO\Compression\GZipStream.cs" />
<Compile Include="System\IO\Compression\Inflater.cs" />
<Compile Include="System\IO\Compression\ZLibException.cs" />
<Compile Include="System\IO\Compression\ZLibNative.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\Deflater.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\DeflateStream.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\Inflater.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\ZLibException.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\ZLibNative.cs" />
</ItemGroup>
<!-- Windows specific files common to both net46 and Core -->
<ItemGroup Condition=" '$(TargetsWindows)' == 'true'">
Expand All @@ -55,15 +72,15 @@
</ItemGroup>
<!-- Windows specific files exclusive to Core -->
<ItemGroup Condition=" '$(TargetsWindows)' == 'true' And '$(TargetGroup)' != 'net46'">
<Compile Include="System\IO\Compression\ZLibNative.Windows.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\ZLibNative.Windows.cs" />
<Compile Include="Interop\Interop.zlib.Windows.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<!-- Unix specific files -->
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
<Compile Include="System\IO\Compression\ZLibNative.Unix.cs" />
<Compile Include="System\IO\Compression\DeflateZLib\ZLibNative.Unix.cs" />
<Compile Include="System\IO\Compression\ZipArchiveEntry.Unix.cs" />
<Compile Include="Interop\Interop.zlib.Unix.cs" />
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.IO.Compression
{
internal enum BlockType
{
Uncompressed = 0,
Static = 1,
Dynamic = 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.IO.Compression
{
public enum CompressionMode
{
Decompress = 0,
Compress = 1
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

namespace System.IO.Compression
{
internal class CopyEncoder
{
// padding for copy encoder formatting
// - 1 byte for header
// - 4 bytes for len, nlen
private const int PaddingSize = 5;

// max uncompressed deflate block size is 64K.
private const int MaxUncompressedBlockSize = 65536;


// null input means write an empty payload with formatting info. This is needed for the final block.
public void GetBlock(DeflateInput input, OutputBuffer output, bool isFinal)
{
Debug.Assert(output != null);
Debug.Assert(output.FreeBytes >= PaddingSize);

// determine number of bytes to write
int count = 0;
if (input != null)
{
// allow space for padding and bits not yet flushed to buffer
count = Math.Min(input.Count, output.FreeBytes - PaddingSize - output.BitsInBuffer);

// we don't expect the output buffer to ever be this big (currently 4K), but we'll check this
// just in case that changes.
if (count > MaxUncompressedBlockSize - PaddingSize)
{
count = MaxUncompressedBlockSize - PaddingSize;
}
}

// write header and flush bits
if (isFinal)
{
output.WriteBits(FastEncoderStatics.BFinalNoCompressionHeaderBitCount,
FastEncoderStatics.BFinalNoCompressionHeader);
}
else
{
output.WriteBits(FastEncoderStatics.NoCompressionHeaderBitCount,
FastEncoderStatics.NoCompressionHeader);
}

// now we're aligned
output.FlushBits();

// write len, nlen
WriteLenNLen((ushort)count, output);

// write uncompressed bytes
if (input != null && count > 0)
{
output.WriteBytes(input.Buffer, input.StartIndex, count);
input.ConsumeBytes(count);
}
}

private void WriteLenNLen(ushort len, OutputBuffer output)
{
// len
output.WriteUInt16(len);

// nlen
ushort onesComp = (ushort)(~(ushort)len);
output.WriteUInt16(onesComp);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

namespace System.IO.Compression
{
internal class DeflateInput
{
private byte[] _buffer;
private int _count;
private int _startIndex;

internal byte[] Buffer
{
get
{
return _buffer;
}
set
{
_buffer = value;
}
}

internal int Count
{
get
{
return _count;
}
set
{
_count = value;
}
}

internal int StartIndex
{
get
{
return _startIndex;
}
set
{
_startIndex = value;
}
}

internal void ConsumeBytes(int n)
{
Debug.Assert(n <= _count, "Should use more bytes than what we have in the buffer");
_startIndex += n;
_count -= n;
Debug.Assert(_startIndex + _count <= _buffer.Length, "Input buffer is in invalid state!");
}

internal InputState DumpState()
{
InputState savedState;
savedState.count = _count;
savedState.startIndex = _startIndex;
return savedState;
}

internal void RestoreState(InputState state)
{
_count = state.count;
_startIndex = state.startIndex;
}

internal struct InputState
{
internal int count;
internal int startIndex;
}
}
}
Loading