Skip to content

Commit

Permalink
Merge pull request #726 from Erior/feature/7ZipDelta
Browse files Browse the repository at this point in the history
Add support for 7ZipDelta decompress
  • Loading branch information
adamhathcock committed Mar 1, 2023
2 parents 9ecf652 + 3ae830a commit 42e8f48
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/SharpCompress/Compressors/Filters/DeltaFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;

namespace SharpCompress.Compressors.Filters
{
internal class DeltaFilter : Filter
{
private const int DISTANCE_MIN = 1;
private const int DISTANCE_MAX = 256;
private const int DISTANCE_MASK = DISTANCE_MAX - 1;

private int _distance;
private byte[] _history;
private int _position;

public DeltaFilter(bool isEncoder, Stream baseStream, byte[] info)
: base(isEncoder, baseStream, 1)
{
_distance = info[0];
_history = new byte[DISTANCE_MAX];
_position = 0;

if (_distance < DISTANCE_MIN)
{
throw new NotSupportedException();
}
}

protected override int Transform(byte[] buffer, int offset, int count)
{
int end = offset + count;

for( int i = offset; i < end; i++ )
{
buffer[i] += _history[(_distance + _position--) & DISTANCE_MASK];
_history[_position & DISTANCE_MASK] = buffer[i];
}

return count;
}
}
}
3 changes: 3 additions & 0 deletions src/SharpCompress/Compressors/LZMA/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SharpCompress.Compressors.LZMA;
internal static class DecoderRegistry
{
private const uint K_COPY = 0x0;
private const uint K_DELTA = 0x3;
private const uint K_LZMA2 = 0x21;
private const uint K_LZMA = 0x030101;
private const uint K_PPMD = 0x030401;
Expand All @@ -37,6 +38,8 @@ long limit
throw new NotSupportedException();
}
return inStreams.Single();
case K_DELTA:
return new DeltaFilter(false, inStreams.Single(), info);
case K_LZMA:
case K_LZMA2:
return new LzmaStream(info, inStreams.Single(), -1, limit);
Expand Down
4 changes: 4 additions & 0 deletions tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ public class SevenZipArchiveTests : ArchiveTests
//"7Zip.BZip2.split.005",
//"7Zip.BZip2.split.006",
//"7Zip.BZip2.split.007"

[Fact]
public void SevenZipArchive_delta_FileRead() =>
ArchiveFileRead("7Zip.delta.7z");
}
Binary file added tests/TestArchives/Archives/7Zip.delta.7z
Binary file not shown.

0 comments on commit 42e8f48

Please sign in to comment.