diff --git a/Makefile b/Makefile index c9f1fe414..7860ce1db 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ clean: @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @$(MAKE) -C $(TESTDIR) $@ > $(VOID) @$(MAKE) -C $(ZWRAPDIR) $@ > $(VOID) - @rm -f zstd + @$(RM) zstd @echo Cleaning completed @@ -121,7 +121,7 @@ endif ifneq (,$(filter $(HOST_OS),MSYS POSIX)) cmaketest: cmake --version - rm -rf projects/cmake/build + $(RM) -r projects/cmake/build mkdir projects/cmake/build cd projects/cmake/build ; cmake -DPREFIX:STRING=~/install_test_dir $(CMAKE_PARAMS) .. ; $(MAKE) install ; $(MAKE) uninstall diff --git a/README.md b/README.md index fa59de83f..2c8e707e9 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ For a larger picture including very slow modes, [click on this link](images/DCsp Previous charts provide results applicable to typical file and stream scenarios (several MB). Small data comes with different perspectives. The smaller the amount of data to compress, the more difficult it is to achieve any significant compression. -This problem is common to any compression algorithm. The reason is, compression algorithms learn from past data how to compress future data. But at the beginning of a new file, there is no "past" to build upon. +This problem is common to many compression algorithms. The reason is, compression algorithms learn from past data how to compress future data. But at the beginning of a new file, there is no "past" to build upon. To solve this situation, Zstd offers a __training mode__, which can be used to tune the algorithm for a selected type of data, by providing it with a few samples. The result of the training is stored in a file called "dictionary", which can be loaded before compression and decompression. Using this dictionary, the compression ratio achievable on small data improves dramatically: diff --git a/examples/Makefile b/examples/Makefile index a9d608778..54602dfe0 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -55,20 +55,21 @@ streaming_decompression : streaming_decompression.c clean: @rm -f core *.o tmp* result* *.zst \ simple_compression simple_decompression \ - dictionary_compression dictionary_decompression \ - streaming_compression streaming_decompression + dictionary_compression dictionary_decompression \ + streaming_compression streaming_decompression @echo Cleaning completed test: all cp README.md tmp + cp Makefile tmp2 @echo starting simple compression ./simple_compression tmp ./simple_decompression tmp.zst - ./streaming_decompression tmp.zst + ./streaming_decompression tmp.zst > /dev/null @echo starting streaming compression ./streaming_compression tmp - ./streaming_decompression tmp.zst + ./streaming_decompression tmp.zst > /dev/null @echo starting dictionary compression - ./dictionary_compression tmp README.md - ./dictionary_decompression tmp.zst README.md + ./dictionary_compression tmp2 tmp README.md + ./dictionary_decompression tmp2.zst tmp.zst README.md @echo tests completed diff --git a/examples/README.md b/examples/README.md index 2f4603881..ba132f6c3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,18 +1,31 @@ Zstandard library : usage examples ================================== -- [Simple compression](simple_compression.c) +- [Simple compression](simple_compression.c) : Compress a single file. Introduces usage of : `ZSTD_compress()` -- [Simple decompression](simple_decompression.c) - Decompress a single file compressed by zstd. +- [Simple decompression](simple_decompression.c) : + Decompress a single file. + Only compatible with simple compression. + Result remains in memory. Introduces usage of : `ZSTD_decompress()` -- [Dictionary compression](dictionary_compression.c) +- [Streaming compression](streaming_compression.c) : + Compress a single file. + Introduces usage of : `ZSTD_compressStream()` + +- [Streaming decompression](streaming_decompression.c) : + Decompress a single file compressed by zstd. + Compatible with both simple and streaming compression. + Result is sent to stdout. + Introduces usage of : `ZSTD_decompressStream()` + +- [Dictionary compression](dictionary_compression.c) : Compress multiple files using the same dictionary. Introduces usage of : `ZSTD_createCDict()` and `ZSTD_compress_usingCDict()` -- [Dictionary decompression](dictionary_decompression.c) +- [Dictionary decompression](dictionary_decompression.c) : Decompress multiple files using the same dictionary. + Result remains in memory. Introduces usage of : `ZSTD_createDDict()` and `ZSTD_decompress_usingDDict()` diff --git a/examples/dictionary_compression.c b/examples/dictionary_compression.c index 08d639c03..adcc3b4d5 100644 --- a/examples/dictionary_compression.c +++ b/examples/dictionary_compression.c @@ -73,12 +73,12 @@ static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSi /* createDict() : `dictFileName` is supposed to have been created using `zstd --train` */ -static ZSTD_CDict* createCDict_orDie(const char* dictFileName) +static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel) { size_t dictSize; printf("loading dictionary %s \n", dictFileName); void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize); - ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 3); + ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel); if (!cdict) { fprintf(stderr, "ZSTD_createCDict error \n"); exit(7); @@ -96,6 +96,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi void* const cBuff = malloc_orDie(cBuffSize); ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + if (cctx==NULL) { fprintf(stderr, "ZSTD_createCCtx() error \n"); exit(10); } size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict); if (ZSTD_isError(cSize)) { fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize)); @@ -107,7 +108,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi /* success */ printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); - ZSTD_freeCCtx(cctx); + ZSTD_freeCCtx(cctx); /* never fails */ free(fBuff); free(cBuff); } @@ -127,6 +128,7 @@ static char* createOutFilename_orDie(const char* filename) int main(int argc, const char** argv) { const char* const exeName = argv[0]; + int const cLevel = 3; if (argc<3) { fprintf(stderr, "wrong arguments\n"); @@ -137,7 +139,7 @@ int main(int argc, const char** argv) /* load dictionary only once */ const char* const dictName = argv[argc-1]; - ZSTD_CDict* const dictPtr = createCDict_orDie(dictName); + ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel); int u; for (u=1; u