forked from OctopusDeploy/Octodiff
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grzegorz Blok
committed
Dec 5, 2019
1 parent
8e01736
commit 82e10e8
Showing
11 changed files
with
252 additions
and
62 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> | ||
<metadata> | ||
<id>FastRsyncNet.Compression</id> | ||
<version>1.0.0</version> | ||
<authors>Grzegorz Blok</authors> | ||
<owners>Grzegorz Blok</owners> | ||
<license type="expression">Apache-2.0</license> | ||
<projectUrl>https://github.com/GrzegorzBlok/FastRsyncNet</projectUrl> | ||
<requireLicenseAcceptance>false</requireLicenseAcceptance> | ||
<tags>sync rsync synchronization compression</tags> | ||
<description>.NET library for file compression compatible with Rsync algorithm.</description> | ||
<dependencies> | ||
<group targetFramework=".NETFramework4.5"> | ||
<dependency id="DotNetZip" version="1.13.4" exclude="Build,Analyzers" /> | ||
</group> | ||
|
||
<group targetFramework=".NETStandard2.0"> | ||
<dependency id="DotNetZip" version="1.13.4" exclude="Build,Analyzers" /> | ||
</group> | ||
</dependencies> | ||
</metadata> | ||
<files> | ||
<file src="..\source\FastRsync\bin\Release\net45\FastRsync.dll" target="lib\net45\FastRsyncCompression.dll" /> | ||
<file src="..\source\FastRsync\bin\Release\netstandard2.0\FastRsync.dll" target="lib\netstandard2.0\FastRsyncCompression.dll" /> | ||
</files> | ||
</package> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Authors>Grzegorz Blok</Authors> | ||
<Company /> | ||
<PackageId>FastRsyncNet.Compression</PackageId> | ||
<Product></Product> | ||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> | ||
<Copyright>Copyright © 2019</Copyright> | ||
<Description>.NET library for file compression compatible with Rsync algorithm.</Description> | ||
<PackageProjectUrl>https://github.com/GrzegorzBlok/FastRsyncNet</PackageProjectUrl> | ||
<PackageTags>sync rsync synchronization compression</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DotNetZip" Version="1.13.4" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// this implementation is influenced by pigz tool by Mark Adler: https://github.com/madler/pigz/blob/master/pigz.c | ||
// pigz license: | ||
/* | ||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the author be held liable for any damages | ||
arising from the use of this software. | ||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. | ||
Mark Adler | ||
madler@alumni.caltech.edu | ||
*/ | ||
|
||
using System; | ||
using System.IO; | ||
using Ionic.Zlib; | ||
|
||
namespace FastRsync.Compression | ||
{ | ||
public class GZip | ||
{ | ||
const int RSYNCBITS = 12; | ||
const uint RSYNCMASK = ((1U << RSYNCBITS) - (uint)1); | ||
const uint RSYNCHIT = (RSYNCMASK >> 1); | ||
|
||
const int BUFFER_SIZE = 32 * 1024; | ||
|
||
public static void Compress(Stream sourceStream, Stream destStream) | ||
{ | ||
var buffer = new byte[BUFFER_SIZE]; | ||
|
||
using (var compressor = new GZipStream(destStream, Ionic.Zlib.CompressionMode.Compress, | ||
CompressionLevel.BestSpeed, true)) | ||
{ | ||
compressor.LastModified = new DateTime(1970, 1, 1); | ||
uint hash = RSYNCHIT; | ||
int n; | ||
while ((n = sourceStream.Read(buffer, 0, BUFFER_SIZE)) > 0) | ||
{ | ||
for (int i = 0, j = 0; i < n; i++) | ||
{ | ||
hash = ((hash << 1) ^ buffer[i]) & RSYNCMASK; | ||
|
||
if (hash == RSYNCHIT) | ||
{ | ||
compressor.FlushMode = FlushType.Sync; | ||
compressor.Write(buffer, j, i + 1 - j); | ||
j = i + 1; | ||
} | ||
else if (i + 1 == n) | ||
{ | ||
compressor.FlushMode = FlushType.None; | ||
compressor.Write(buffer, j, i + 1 - j); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using FastRsync.Compression; | ||
using FastRsync.Core; | ||
using FastRsync.Delta; | ||
using FastRsync.Signature; | ||
using NUnit.Framework; | ||
|
||
namespace FastRsync.Tests | ||
{ | ||
[TestFixture] | ||
public class GZipTests | ||
{ | ||
[Test] | ||
[TestCase(120)] | ||
[TestCase(5 * 1024 * 1024)] | ||
public void GZip_CompressData(int dataLength) | ||
{ | ||
// Arrange | ||
var data = new byte[dataLength]; | ||
new Random().NextBytes(data); | ||
var srcStream = new MemoryStream(data); | ||
var destStream = new MemoryStream(); | ||
|
||
// Act | ||
GZip.Compress(srcStream, destStream); | ||
|
||
// Assert | ||
destStream.Seek(0, SeekOrigin.Begin); | ||
var decompressedStream = new MemoryStream(); | ||
using (var gz = new GZipStream(destStream, CompressionMode.Decompress)) | ||
{ | ||
gz.CopyTo(decompressedStream); | ||
} | ||
|
||
var dataOutput = decompressedStream.ToArray(); | ||
Assert.AreEqual(data, dataOutput); | ||
} | ||
|
||
[Theory] | ||
[TestCase(120)] | ||
[TestCase(5 * 1024 * 1024)] | ||
public void GZip_CompressData_RsyncSignatureAndPatch(int dataLength) | ||
{ | ||
// Arrange | ||
var dataBasis = new byte[dataLength]; | ||
new Random().NextBytes(dataBasis); | ||
var basisStream = new MemoryStream(dataBasis); | ||
var basisStreamCompressed = new MemoryStream(); | ||
var basisStreamCompressedSignature = new MemoryStream(); | ||
|
||
var newFileStream = new MemoryStream(); | ||
newFileStream.Write(dataBasis, 10, dataLength * 4 / 5); | ||
var newRandomData = new byte[dataLength * 2 / 5]; | ||
new Random().NextBytes(newRandomData); | ||
newFileStream.Write(newRandomData, 0, newRandomData.Length); | ||
newFileStream.Seek(0, SeekOrigin.Begin); | ||
|
||
var newFileStreamCompressed = new MemoryStream(); | ||
var deltaStream = new MemoryStream(); | ||
var patchedCompressedStream = new MemoryStream(); | ||
|
||
// Act | ||
GZip.Compress(basisStream, basisStreamCompressed); | ||
basisStreamCompressed.Seek(0, SeekOrigin.Begin); | ||
|
||
var signatureBuilder = new SignatureBuilder(); | ||
signatureBuilder.Build(basisStreamCompressed, new SignatureWriter(basisStreamCompressedSignature)); | ||
basisStreamCompressedSignature.Seek(0, SeekOrigin.Begin); | ||
|
||
GZip.Compress(newFileStream, newFileStreamCompressed); | ||
newFileStreamCompressed.Seek(0, SeekOrigin.Begin); | ||
|
||
var deltaBuilder = new DeltaBuilder(); | ||
deltaBuilder.BuildDelta(newFileStreamCompressed, new SignatureReader(basisStreamCompressedSignature, null), | ||
new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); | ||
deltaStream.Seek(0, SeekOrigin.Begin); | ||
|
||
var deltaApplier = new DeltaApplier | ||
{ | ||
SkipHashCheck = true | ||
}; | ||
var deltaReader = new BinaryDeltaReader(deltaStream, null); | ||
deltaApplier.Apply(basisStreamCompressed, deltaReader, patchedCompressedStream); | ||
deltaApplier.HashCheck(deltaReader, patchedCompressedStream); | ||
|
||
// Assert | ||
Assert.AreEqual(newFileStreamCompressed.ToArray(), patchedCompressedStream.ToArray()); | ||
|
||
patchedCompressedStream.Seek(0, SeekOrigin.Begin); | ||
var decompressedStream = new MemoryStream(); | ||
using (var gz = new GZipStream(patchedCompressedStream, CompressionMode.Decompress)) | ||
{ | ||
gz.CopyTo(decompressedStream); | ||
} | ||
|
||
var dataOutput = decompressedStream.ToArray(); | ||
Assert.AreEqual(newFileStream.ToArray(), dataOutput); | ||
} | ||
} | ||
} |
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
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.