Skip to content

Commit

Permalink
Merge pull request #807 from TwanVanDongen/master
Browse files Browse the repository at this point in the history
Support for decompressing Zip Shrink (Method:1)
  • Loading branch information
adamhathcock authored Jan 29, 2024
2 parents 8da2499 + c057ffb commit ab5535e
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SharpCompress/Common/CompressionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum CompressionType
LZip,
Xz,
Unknown,
Deflate64
Deflate64,
Shrink
}
1 change: 1 addition & 0 deletions src/SharpCompress/Common/Zip/ZipCompressionMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace SharpCompress.Common.Zip;
internal enum ZipCompressionMethod
{
None = 0,
Shrink = 1,
Deflate = 8,
Deflate64 = 9,
BZip2 = 12,
Expand Down
4 changes: 4 additions & 0 deletions src/SharpCompress/Common/Zip/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public override CompressionType CompressionType
{
return CompressionType.None;
}
case ZipCompressionMethod.Shrink:
{
return CompressionType.Shrink;
}
default:
{
return CompressionType.Unknown;
Expand Down
11 changes: 11 additions & 0 deletions src/SharpCompress/Common/Zip/ZipFilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using SharpCompress.Compressors.Deflate64;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.PPMd;
using SharpCompress.Compressors.Shrink;
using SharpCompress.Compressors.Xz;
using SharpCompress.IO;
using ZstdSharp;
Expand Down Expand Up @@ -79,6 +80,15 @@ protected Stream CreateDecompressionStream(Stream stream, ZipCompressionMethod m

return new DataDescriptorStream(stream);
}
case ZipCompressionMethod.Shrink:
{
return new ShrinkStream(
stream,
CompressionMode.Decompress,
Header.CompressedSize,
Header.UncompressedSize
);
}
case ZipCompressionMethod.Deflate:
{
return new DeflateStream(stream, CompressionMode.Decompress);
Expand Down Expand Up @@ -192,6 +202,7 @@ protected Stream GetCryptoStream(Stream plainStream)
switch (Header.CompressionMethod)
{
case ZipCompressionMethod.None:
case ZipCompressionMethod.Shrink:
case ZipCompressionMethod.Deflate:
case ZipCompressionMethod.Deflate64:
case ZipCompressionMethod.BZip2:
Expand Down
85 changes: 85 additions & 0 deletions src/SharpCompress/Compressors/Shrink/BitStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpCompress.Compressors.Shrink
{
internal class BitStream
{
private byte[] _src;
private int _srcLen;
private int _byteIdx;
private int _bitIdx;
private int _bitsLeft;
private ulong _bitBuffer;
private static uint[] _maskBits = new uint[17]
{
0U,
1U,
3U,
7U,
15U,
31U,
63U,
(uint)sbyte.MaxValue,
(uint)byte.MaxValue,
511U,
1023U,
2047U,
4095U,
8191U,
16383U,
(uint)short.MaxValue,
(uint)ushort.MaxValue
};

public BitStream(byte[] src, int srcLen)
{
_src = src;
_srcLen = srcLen;
_byteIdx = 0;
_bitIdx = 0;
}

public int BytesRead => (_byteIdx << 3) + _bitIdx;

private int NextByte()
{
if (_byteIdx >= _srcLen)
{
return 0;
}

return _src[_byteIdx++];
}

public int NextBits(int nbits)
{
int result = 0;
if (nbits > _bitsLeft)
{
int num;
while (_bitsLeft <= 24 && (num = NextByte()) != 1234)
{
_bitBuffer |= (ulong)num << _bitsLeft;
_bitsLeft += 8;
}
}
result = (int)((long)_bitBuffer & (long)_maskBits[nbits]);
_bitBuffer >>= nbits;
_bitsLeft -= nbits;
return result;
}

public bool Advance(int count)
{
if (_byteIdx > _srcLen)
{
return false;
}
return true;
}
}
}
Loading

0 comments on commit ab5535e

Please sign in to comment.