-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for delta encoding, compatible with the --patch-from option that was introduced in zstd reference v1.4.5: https://github.com/facebook/zstd/wiki/Zstandard-as-a-patching-engine
- Loading branch information
Showing
10 changed files
with
151 additions
and
34 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
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 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,46 @@ | ||
package zstd_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
) | ||
|
||
func ExampleWithEncoderDictRaw() { | ||
// "Raw" dictionaries can be used for compressed delta encoding. | ||
|
||
source := []byte(` | ||
This is the source file. Compression of the target file with | ||
the source file as the dictionary will produce a compressed | ||
delta encoding of the target file.`) | ||
target := []byte(` | ||
This is the target file. Decompression of the delta encoding with | ||
the source file as the dictionary will produce this file.`) | ||
|
||
// The dictionary id is arbitrary. We use zero for compatibility | ||
// with zstd --patch-from, but applications can use any id | ||
// not in the range [32768, 1<<31). | ||
const id = 0 | ||
|
||
bestLevel := zstd.WithEncoderLevel(zstd.SpeedBestCompression) | ||
|
||
w, _ := zstd.NewWriter(nil, bestLevel, | ||
zstd.WithEncoderDictRaw(id, source)) | ||
delta := w.EncodeAll(target, nil) | ||
|
||
r, _ := zstd.NewReader(nil, zstd.WithDecoderDictRaw(id, source)) | ||
out, err := r.DecodeAll(delta, nil) | ||
if err != nil || !bytes.Equal(out, target) { | ||
panic("decoding error") | ||
} | ||
|
||
// Ordinary compression, for reference. | ||
w, _ = zstd.NewWriter(nil, bestLevel) | ||
compressed := w.EncodeAll(target, nil) | ||
|
||
// Check that the delta is at most half as big as the compressed file. | ||
fmt.Println(len(delta) < len(compressed)/2) | ||
// Output: | ||
// true | ||
} |
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,5 @@ | ||
0000000000000000 | ||
|
||
This file is to be used as the dictionary for compressing target.txt: | ||
|
||
zstd -19 --patch-from=source.txt target.txt |
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,5 @@ | ||
0000000000000000 | ||
|
||
This file is to be compressed with source.txt as the dictionary: | ||
|
||
zstd -19 --patch-from=source.txt target.txt |
Binary file not shown.
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