multiple_simple_compression.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include <stdio.h> // printf
  11. #include <stdlib.h> // free
  12. #include <string.h> // memcpy, strlen
  13. #include <zstd.h> // presumes zstd library is installed
  14. #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
  15. typedef struct {
  16. void* fBuffer;
  17. void* cBuffer;
  18. size_t fBufferSize;
  19. size_t cBufferSize;
  20. ZSTD_CCtx* cctx;
  21. } resources;
  22. /*
  23. * allocate memory for buffers big enough to compress all files
  24. * as well as memory for output file name (ofn)
  25. */
  26. static resources createResources_orDie(int argc, const char** argv, char **ofn, size_t* ofnBufferLen)
  27. {
  28. size_t maxFilenameLength=0;
  29. size_t maxFileSize = 0;
  30. int argNb;
  31. for (argNb = 1; argNb < argc; argNb++) {
  32. const char* const filename = argv[argNb];
  33. size_t const filenameLength = strlen(filename);
  34. size_t const fileSize = fsize_orDie(filename);
  35. if (filenameLength > maxFilenameLength) maxFilenameLength = filenameLength;
  36. if (fileSize > maxFileSize) maxFileSize = fileSize;
  37. }
  38. resources ress;
  39. ress.fBufferSize = maxFileSize;
  40. ress.cBufferSize = ZSTD_compressBound(maxFileSize);
  41. *ofnBufferLen = maxFilenameLength + 5;
  42. *ofn = (char*)malloc_orDie(*ofnBufferLen);
  43. ress.fBuffer = malloc_orDie(ress.fBufferSize);
  44. ress.cBuffer = malloc_orDie(ress.cBufferSize);
  45. ress.cctx = ZSTD_createCCtx();
  46. CHECK(ress.cctx != NULL, "ZSTD_createCCtx() failed!");
  47. return ress;
  48. }
  49. static void freeResources(resources ress, char *outFilename)
  50. {
  51. free(ress.fBuffer);
  52. free(ress.cBuffer);
  53. ZSTD_freeCCtx(ress.cctx); /* never fails */
  54. free(outFilename);
  55. }
  56. /* compress with pre-allocated context (ZSTD_CCtx) and input/output buffers*/
  57. static void compressFile_orDie(resources ress, const char* fname, const char* oname)
  58. {
  59. size_t fSize = loadFile_orDie(fname, ress.fBuffer, ress.fBufferSize);
  60. /* Compress using the context.
  61. * If you need more control over parameters, use the advanced API:
  62. * ZSTD_CCtx_setParameter(), and ZSTD_compress2().
  63. */
  64. size_t const cSize = ZSTD_compressCCtx(ress.cctx, ress.cBuffer, ress.cBufferSize, ress.fBuffer, fSize, 1);
  65. CHECK_ZSTD(cSize);
  66. saveFile_orDie(oname, ress.cBuffer, cSize);
  67. /* success */
  68. printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
  69. }
  70. int main(int argc, const char** argv)
  71. {
  72. const char* const exeName = argv[0];
  73. if (argc<2) {
  74. printf("wrong arguments\n");
  75. printf("usage:\n");
  76. printf("%s FILE(s)\n", exeName);
  77. return 1;
  78. }
  79. /* memory allocation for outFilename and resources */
  80. char* outFilename;
  81. size_t outFilenameBufferLen;
  82. resources const ress = createResources_orDie(argc, argv, &outFilename, &outFilenameBufferLen);
  83. /* compress files with shared context, input and output buffers */
  84. int argNb;
  85. for (argNb = 1; argNb < argc; argNb++) {
  86. const char* const inFilename = argv[argNb];
  87. size_t const inFilenameLen = strlen(inFilename);
  88. CHECK(inFilenameLen + 5 <= outFilenameBufferLen, "File name too long!");
  89. memcpy(outFilename, inFilename, inFilenameLen);
  90. memcpy(outFilename+inFilenameLen, ".zst", 5);
  91. compressFile_orDie(ress, inFilename, outFilename);
  92. }
  93. /* free memory */
  94. freeResources(ress,outFilename);
  95. printf("compressed %i files \n", argc-1);
  96. return 0;
  97. }