C 5
Deflate.c Guest on 30th July 2020 05:22:51 AM
  1.  
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4. #include "zlib.h"
  5.  
  6. z_stream z;
  7.  
  8. #define DEFAULT_BUFFER_SIZE     32*1024
  9. #define DEFAULT_COMPRESSION     Z_DEFAULT_COMPRESSION;
  10.  
  11. #define MAX(a,b) ((a) >= (b) ? (a) : (b))
  12. #define MIN(a,b) ((a) <= (b) ? (a) : (b))
  13.  
  14. char *input_buffer      = NULL;
  15. char *output_buffer     = NULL;
  16. int  buffer_size        = DEFAULT_BUFFER_SIZE;
  17. int  compression_rate   = DEFAULT_COMPRESSION;
  18.  
  19. FILE * fin = stdin;
  20. FILE * fout = stdout;
  21.  
  22. void HelpScreen (const char *progname)
  23. {
  24.     fprintf(stderr, "\nDeflates using Zlib from stdin to stdout\n");
  25.     fprintf(stderr, "Usage: %s\n", progname ? progname : "deflate");
  26.     fprintf(stderr, "Options:\n");
  27.     fprintf(stderr, "\t-b\tBuffer size (default 32K). This has no effect on compression result\n");
  28.     fprintf(stderr, "\t-c\tCompression rate [1..9] where 1 is compress faster and 9 is compress better (default 6)\n");
  29.     fprintf(stderr, "\t-h\tHelp - this help screen\n");
  30. }
  31.  
  32. int main (int argc, char ** argv)
  33. {
  34.     int arg;
  35.     int status;
  36.     int count;
  37.  
  38.     /* Scan command line for parameters */
  39.     for (arg=1; arg<argc; arg++) {
  40.        
  41.         if (*argv[arg] == '-') {
  42.    
  43.             /* Buffer size */
  44.             if (!strcmp(argv[arg], "-b")) {
  45.                 buffer_size = (arg+1 < argc && *argv[arg+1] != '-') ?
  46.                     atoi(argv[++arg]) : DEFAULT_BUFFER_SIZE;
  47.                 buffer_size = MAX(buffer_size, 256);
  48.  
  49.             /* Compression rate */
  50.             } else if (!strcmp(argv[arg], "-c")) {
  51.                 compression_rate = (arg+1 < argc && *argv[arg+1] != '-') ?
  52.                     atoi(argv[++arg]) : DEFAULT_COMPRESSION;
  53.                 compression_rate = MAX(compression_rate, Z_BEST_SPEED);
  54.                 compression_rate = MIN(compression_rate, Z_BEST_COMPRESSION);
  55.  
  56.             /* Print the help screen and exit */
  57.             } else if (!strcmp(argv[arg], "-h")) {
  58.                 HelpScreen(*argv);
  59.                 exit(0);
  60.  
  61.             } else {
  62.                 HelpScreen(*argv);
  63.                 exit(-1);
  64.             }
  65.         } else {
  66.             HelpScreen(*argv);
  67.             exit(-1);
  68.         }
  69.     }
  70.  
  71.     if ((input_buffer = (char *) calloc(1, buffer_size)) == NULL ||
  72.         (output_buffer = (char *) calloc(1, buffer_size)) == NULL) {
  73.         fprintf(stderr, "Not enough memory\n");
  74.         exit(-1);
  75.     }
  76.  
  77.     deflateInit(&z, compression_rate);
  78.     z.avail_in = 0;
  79.     z.next_out = output_buffer;
  80.     z.avail_out = buffer_size;
  81.     for ( ; ; ) {
  82.         if ( z.avail_in == 0 ) {
  83.             z.next_in = input_buffer;
  84.             z.avail_in = fread( input_buffer, 1, buffer_size, fin );
  85.         }
  86.         if ( z.avail_in == 0 ) {
  87.             status = deflate( &z, Z_FINISH );
  88.             count = buffer_size - z.avail_out;
  89.             if ( count ) fwrite( output_buffer, 1, count, fout );
  90.             break;
  91.         }
  92.         status = deflate( &z, Z_NO_FLUSH );
  93.         count = buffer_size - z.avail_out;
  94.         if ( count ) fwrite( output_buffer, 1, count, fout );
  95.         z.next_out = output_buffer;
  96.         z.avail_out = buffer_size;
  97.     }
  98.     fprintf(stderr, "Compressing data: raw data %lu, compressed %lu, factor %.2f, compression level (default = -1) %d, buffer size %d\n",
  99.             z.total_in, z.total_out,
  100.             z.total_in == 0 ? 0.0 :
  101.             (double)z.total_out / z.total_in,
  102.             compression_rate,
  103.             buffer_size);
  104.     deflateEnd(&z);
  105.     fflush(stdout);
  106.  
  107.     if (input_buffer) free(input_buffer);
  108.     if (output_buffer) free(output_buffer);
  109.     return 0;
  110. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.