minigzip.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 6k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5. /*
  6.  * minigzip is a minimal implementation of the gzip utility. This is
  7.  * only an example of using zlib and isn't meant to replace the
  8.  * full-featured gzip. No attempt is made to deal with file systems
  9.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  10.  * very limited. So use minigzip only for testing; use gzip for the
  11.  * real thing. On MSDOS, use only on file names without extension
  12.  * or in pipe mode.
  13.  */
  14. /* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */
  15. #include <stdio.h>
  16. #include "zlib.h"
  17. #ifndef __GO32__
  18. extern void exit  OF((int));
  19. #endif
  20. extern int unlink OF((const char *));
  21. #ifdef STDC
  22. #  include <string.h>
  23. #endif
  24. #if defined(MSDOS) || defined(OS2) || defined(WIN32)
  25. #  include <fcntl.h>
  26. #  include <io.h>
  27. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  28. #else
  29. #  define SET_BINARY_MODE(file)
  30. #endif
  31. #ifdef VMS
  32. #  define GZ_SUFFIX "-gz"
  33. #else
  34. #  define GZ_SUFFIX ".gz"
  35. #endif
  36. #define SUFFIX_LEN sizeof(GZ_SUFFIX)
  37. #define BUFLEN 4096
  38. #define MAX_NAME_LEN 1024
  39. #define local static
  40. /* For MSDOS and other systems with limitation on stack size. For Unix,
  41.     #define local
  42.    works also.
  43.  */
  44. char *prog;
  45. void error           OF((char *msg));
  46. void gz_compress     OF((FILE   *in, gzFile out));
  47. void gz_uncompress   OF((gzFile in, FILE   *out));
  48. void file_compress   OF((char  *file));
  49. void file_uncompress OF((char  *file));
  50. int  main            OF((int argc, char *argv[]));
  51. /* ===========================================================================
  52.  * Display error message and exit
  53.  */
  54. void error(msg)
  55.     char *msg;
  56. {
  57.     fprintf(stderr, "%s: %sn", prog, msg);
  58.     exit(1);
  59. }
  60. /* ===========================================================================
  61.  * Compress input to output then close both files.
  62.  */
  63. void gz_compress(in, out)
  64.     FILE   *in;
  65.     gzFile out;
  66. {
  67.     local char buf[BUFLEN];
  68.     int len;
  69.     int err;
  70.     for (;;) {
  71.         len = fread(buf, 1, sizeof(buf), in);
  72.         if (ferror(in)) {
  73.             perror("fread");
  74.             exit(1);
  75.         }
  76.         if (len == 0) break;
  77.         if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
  78.     }
  79.     fclose(in);
  80.     if (gzclose(out) != Z_OK) error("failed gzclose");
  81. }
  82. /* ===========================================================================
  83.  * Uncompress input to output then close both files.
  84.  */
  85. void gz_uncompress(in, out)
  86.     gzFile in;
  87.     FILE   *out;
  88. {
  89.     local char buf[BUFLEN];
  90.     int len;
  91.     int err;
  92.     for (;;) {
  93.         len = gzread(in, buf, sizeof(buf));
  94.         if (len < 0) error (gzerror(in, &err));
  95.         if (len == 0) break;
  96.         if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
  97.     }
  98.     if (fclose(out)) error("failed fclose");
  99.     if (gzclose(in) != Z_OK) error("failed gzclose");
  100. }
  101. /* ===========================================================================
  102.  * Compress the given file: create a corresponding .gz file and remove the
  103.  * original.
  104.  */
  105. void file_compress(file)
  106.     char  *file;
  107. {
  108.     local char outfile[MAX_NAME_LEN];
  109.     FILE  *in;
  110.     gzFile out;
  111.     strcpy(outfile, file);
  112.     strcat(outfile, GZ_SUFFIX);
  113.     in = fopen(file, "rb");
  114.     if (in == NULL) {
  115.         perror(file);
  116.         exit(1);
  117.     }
  118.     out = gzopen(outfile, "wb"); /* use "wb9" for maximal compression */
  119.     if (out == NULL) {
  120.         fprintf(stderr, "%s: can't gzopen %sn", prog, outfile);
  121.         exit(1);
  122.     }
  123.     gz_compress(in, out);
  124.     unlink(file);
  125. }
  126. /* ===========================================================================
  127.  * Uncompress the given file and remove the original.
  128.  */
  129. void file_uncompress(file)
  130.     char  *file;
  131. {
  132.     local char buf[MAX_NAME_LEN];
  133.     char *infile, *outfile;
  134.     FILE  *out;
  135.     gzFile in;
  136.     int len = strlen(file);
  137.     strcpy(buf, file);
  138.     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  139.         infile = file;
  140.         outfile = buf;
  141.         outfile[len-3] = '';
  142.     } else {
  143.         outfile = file;
  144.         infile = buf;
  145.         strcat(infile, GZ_SUFFIX);
  146.     }
  147.     in = gzopen(infile, "rb");
  148.     if (in == NULL) {
  149.         fprintf(stderr, "%s: can't gzopen %sn", prog, infile);
  150.         exit(1);
  151.     }
  152.     out = fopen(outfile, "wb");
  153.     if (out == NULL) {
  154.         perror(file);
  155.         exit(1);
  156.     }
  157.     gz_uncompress(in, out);
  158.     unlink(infile);
  159. }
  160. /* ===========================================================================
  161.  * Usage:  minigzip [-d] [files...]
  162.  */
  163. int main(argc, argv)
  164.     int argc;
  165.     char *argv[];
  166. {
  167.     int uncompr = 0;
  168.     gzFile file;
  169.     prog = argv[0];
  170.     argc--, argv++;
  171.     if (argc > 0) {
  172.         uncompr = (strcmp(*argv, "-d") == 0);
  173.         if (uncompr) {
  174.             argc--, argv++;
  175.         }
  176.     }
  177.     if (argc == 0) {
  178.         SET_BINARY_MODE(stdin);
  179.         SET_BINARY_MODE(stdout);
  180.         if (uncompr) {
  181.             file = gzdopen(fileno(stdin), "rb");
  182.             if (file == NULL) error("can't gzdopen stdin");
  183.             gz_uncompress(file, stdout);
  184.         } else {
  185.             file = gzdopen(fileno(stdout), "wb"); /* "wb9" for max compr. */
  186.             if (file == NULL) error("can't gzdopen stdout");
  187.             gz_compress(stdin, file);
  188.         }
  189.     } else {
  190.         do {
  191.             if (uncompr) {
  192.                 file_uncompress(*argv);
  193.             } else {
  194.                 file_compress(*argv);
  195.             }
  196.         } while (argv++, --argc);
  197.     }
  198.     exit(0);
  199.     return 0; /* to avoid warning */
  200. }