minigzip.c
上传用户:shengde
上传日期:2021-02-21
资源大小:638k
文件大小:11k
源码类别:

压缩解压

开发平台:

Visual C++

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995-2006, 2010 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$ */
  15. #include "zlib.h"
  16. #include <stdio.h>
  17. #ifdef STDC
  18. #  include <string.h>
  19. #  include <stdlib.h>
  20. #endif
  21. #ifdef USE_MMAP
  22. #  include <sys/types.h>
  23. #  include <sys/mman.h>
  24. #  include <sys/stat.h>
  25. #endif
  26. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  27. #  include <fcntl.h>
  28. #  include <io.h>
  29. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  30. #else
  31. #  define SET_BINARY_MODE(file)
  32. #endif
  33. #ifdef VMS
  34. #  define unlink delete
  35. #  define GZ_SUFFIX "-gz"
  36. #endif
  37. #ifdef RISCOS
  38. #  define unlink remove
  39. #  define GZ_SUFFIX "-gz"
  40. #  define fileno(file) file->__file
  41. #endif
  42. #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  43. #  include <unix.h> /* for fileno */
  44. #endif
  45. #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
  46.   extern int unlink OF((const char *));
  47. #endif
  48. #if defined(UNDER_CE) && defined(NO_ERRNO_H)
  49. #  include <windows.h>
  50. #  define perror(s) pwinerror(s)
  51. /* Map the Windows error number in ERROR to a locale-dependent error
  52.    message string and return a pointer to it.  Typically, the values
  53.    for ERROR come from GetLastError.
  54.    The string pointed to shall not be modified by the application,
  55.    but may be overwritten by a subsequent call to strwinerror
  56.    The strwinerror function does not change the current setting
  57.    of GetLastError.  */
  58. static char *strwinerror (error)
  59.      DWORD error;
  60. {
  61.     static char buf[1024];
  62.     wchar_t *msgbuf;
  63.     DWORD lasterr = GetLastError();
  64.     DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
  65.         | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  66.         NULL,
  67.         error,
  68.         0, /* Default language */
  69.         (LPVOID)&msgbuf,
  70.         0,
  71.         NULL);
  72.     if (chars != 0) {
  73.         /* If there is an rn appended, zap it.  */
  74.         if (chars >= 2
  75.             && msgbuf[chars - 2] == 'r' && msgbuf[chars - 1] == 'n') {
  76.             chars -= 2;
  77.             msgbuf[chars] = 0;
  78.         }
  79.         if (chars > sizeof (buf) - 1) {
  80.             chars = sizeof (buf) - 1;
  81.             msgbuf[chars] = 0;
  82.         }
  83.         wcstombs(buf, msgbuf, chars + 1);
  84.         LocalFree(msgbuf);
  85.     }
  86.     else {
  87.         sprintf(buf, "unknown win32 error (%ld)", error);
  88.     }
  89.     SetLastError(lasterr);
  90.     return buf;
  91. }
  92. static void pwinerror (s)
  93.     const char *s;
  94. {
  95.     if (s && *s)
  96.         fprintf(stderr, "%s: %sn", s, strwinerror(GetLastError ()));
  97.     else
  98.         fprintf(stderr, "%sn", strwinerror(GetLastError ()));
  99. }
  100. #endif /* UNDER_CE && NO_ERRNO_H */
  101. #ifndef GZ_SUFFIX
  102. #  define GZ_SUFFIX ".gz"
  103. #endif
  104. #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
  105. #define BUFLEN      16384
  106. #define MAX_NAME_LEN 1024
  107. #ifdef MAXSEG_64K
  108. #  define local static
  109.    /* Needed for systems with limitation on stack size. */
  110. #else
  111. #  define local
  112. #endif
  113. char *prog;
  114. void error            OF((const char *msg));
  115. void gz_compress      OF((FILE   *in, gzFile out));
  116. #ifdef USE_MMAP
  117. int  gz_compress_mmap OF((FILE   *in, gzFile out));
  118. #endif
  119. void gz_uncompress    OF((gzFile in, FILE   *out));
  120. void file_compress    OF((char  *file, char *mode));
  121. void file_uncompress  OF((char  *file));
  122. int  main             OF((int argc, char *argv[]));
  123. /* ===========================================================================
  124.  * Display error message and exit
  125.  */
  126. void error(msg)
  127.     const char *msg;
  128. {
  129.     fprintf(stderr, "%s: %sn", prog, msg);
  130.     exit(1);
  131. }
  132. /* ===========================================================================
  133.  * Compress input to output then close both files.
  134.  */
  135. void gz_compress(in, out)
  136.     FILE   *in;
  137.     gzFile out;
  138. {
  139.     local char buf[BUFLEN];
  140.     int len;
  141.     int err;
  142. #ifdef USE_MMAP
  143.     /* Try first compressing with mmap. If mmap fails (minigzip used in a
  144.      * pipe), use the normal fread loop.
  145.      */
  146.     if (gz_compress_mmap(in, out) == Z_OK) return;
  147. #endif
  148.     for (;;) {
  149.         len = (int)fread(buf, 1, sizeof(buf), in);
  150.         if (ferror(in)) {
  151.             perror("fread");
  152.             exit(1);
  153.         }
  154.         if (len == 0) break;
  155.         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  156.     }
  157.     fclose(in);
  158.     if (gzclose(out) != Z_OK) error("failed gzclose");
  159. }
  160. #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
  161. /* Try compressing the input file at once using mmap. Return Z_OK if
  162.  * if success, Z_ERRNO otherwise.
  163.  */
  164. int gz_compress_mmap(in, out)
  165.     FILE   *in;
  166.     gzFile out;
  167. {
  168.     int len;
  169.     int err;
  170.     int ifd = fileno(in);
  171.     caddr_t buf;    /* mmap'ed buffer for the entire input file */
  172.     off_t buf_len;  /* length of the input file */
  173.     struct stat sb;
  174.     /* Determine the size of the file, needed for mmap: */
  175.     if (fstat(ifd, &sb) < 0) return Z_ERRNO;
  176.     buf_len = sb.st_size;
  177.     if (buf_len <= 0) return Z_ERRNO;
  178.     /* Now do the actual mmap: */
  179.     buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
  180.     if (buf == (caddr_t)(-1)) return Z_ERRNO;
  181.     /* Compress the whole file at once: */
  182.     len = gzwrite(out, (char *)buf, (unsigned)buf_len);
  183.     if (len != (int)buf_len) error(gzerror(out, &err));
  184.     munmap(buf, buf_len);
  185.     fclose(in);
  186.     if (gzclose(out) != Z_OK) error("failed gzclose");
  187.     return Z_OK;
  188. }
  189. #endif /* USE_MMAP */
  190. /* ===========================================================================
  191.  * Uncompress input to output then close both files.
  192.  */
  193. void gz_uncompress(in, out)
  194.     gzFile in;
  195.     FILE   *out;
  196. {
  197.     local char buf[BUFLEN];
  198.     int len;
  199.     int err;
  200.     for (;;) {
  201.         len = gzread(in, buf, sizeof(buf));
  202.         if (len < 0) error (gzerror(in, &err));
  203.         if (len == 0) break;
  204.         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  205.             error("failed fwrite");
  206.         }
  207.     }
  208.     if (fclose(out)) error("failed fclose");
  209.     if (gzclose(in) != Z_OK) error("failed gzclose");
  210. }
  211. /* ===========================================================================
  212.  * Compress the given file: create a corresponding .gz file and remove the
  213.  * original.
  214.  */
  215. void file_compress(file, mode)
  216.     char  *file;
  217.     char  *mode;
  218. {
  219.     local char outfile[MAX_NAME_LEN];
  220.     FILE  *in;
  221.     gzFile out;
  222.     if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
  223.         fprintf(stderr, "%s: filename too longn", prog);
  224.         exit(1);
  225.     }
  226.     strcpy(outfile, file);
  227.     strcat(outfile, GZ_SUFFIX);
  228.     in = fopen(file, "rb");
  229.     if (in == NULL) {
  230.         perror(file);
  231.         exit(1);
  232.     }
  233.     out = gzopen(outfile, mode);
  234.     if (out == NULL) {
  235.         fprintf(stderr, "%s: can't gzopen %sn", prog, outfile);
  236.         exit(1);
  237.     }
  238.     gz_compress(in, out);
  239.     unlink(file);
  240. }
  241. /* ===========================================================================
  242.  * Uncompress the given file and remove the original.
  243.  */
  244. void file_uncompress(file)
  245.     char  *file;
  246. {
  247.     local char buf[MAX_NAME_LEN];
  248.     char *infile, *outfile;
  249.     FILE  *out;
  250.     gzFile in;
  251.     size_t len = strlen(file);
  252.     if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
  253.         fprintf(stderr, "%s: filename too longn", prog);
  254.         exit(1);
  255.     }
  256.     strcpy(buf, file);
  257.     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  258.         infile = file;
  259.         outfile = buf;
  260.         outfile[len-3] = '';
  261.     } else {
  262.         outfile = file;
  263.         infile = buf;
  264.         strcat(infile, GZ_SUFFIX);
  265.     }
  266.     in = gzopen(infile, "rb");
  267.     if (in == NULL) {
  268.         fprintf(stderr, "%s: can't gzopen %sn", prog, infile);
  269.         exit(1);
  270.     }
  271.     out = fopen(outfile, "wb");
  272.     if (out == NULL) {
  273.         perror(file);
  274.         exit(1);
  275.     }
  276.     gz_uncompress(in, out);
  277.     unlink(infile);
  278. }
  279. /* ===========================================================================
  280.  * Usage:  minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
  281.  *   -c : write to standard output
  282.  *   -d : decompress
  283.  *   -f : compress with Z_FILTERED
  284.  *   -h : compress with Z_HUFFMAN_ONLY
  285.  *   -r : compress with Z_RLE
  286.  *   -1 to -9 : compression level
  287.  */
  288. int main(argc, argv)
  289.     int argc;
  290.     char *argv[];
  291. {
  292.     int copyout = 0;
  293.     int uncompr = 0;
  294.     gzFile file;
  295.     char *bname, outmode[20];
  296.     strcpy(outmode, "wb6 ");
  297.     prog = argv[0];
  298.     bname = strrchr(argv[0], '/');
  299.     if (bname)
  300.       bname++;
  301.     else
  302.       bname = argv[0];
  303.     argc--, argv++;
  304.     if (!strcmp(bname, "gunzip"))
  305.       uncompr = 1;
  306.     else if (!strcmp(bname, "zcat"))
  307.       copyout = uncompr = 1;
  308.     while (argc > 0) {
  309.       if (strcmp(*argv, "-c") == 0)
  310.         copyout = 1;
  311.       else if (strcmp(*argv, "-d") == 0)
  312.         uncompr = 1;
  313.       else if (strcmp(*argv, "-f") == 0)
  314.         outmode[3] = 'f';
  315.       else if (strcmp(*argv, "-h") == 0)
  316.         outmode[3] = 'h';
  317.       else if (strcmp(*argv, "-r") == 0)
  318.         outmode[3] = 'R';
  319.       else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
  320.                (*argv)[2] == 0)
  321.         outmode[2] = (*argv)[1];
  322.       else
  323.         break;
  324.       argc--, argv++;
  325.     }
  326.     if (outmode[3] == ' ')
  327.         outmode[3] = 0;
  328.     if (argc == 0) {
  329.         SET_BINARY_MODE(stdin);
  330.         SET_BINARY_MODE(stdout);
  331.         if (uncompr) {
  332.             file = gzdopen(fileno(stdin), "rb");
  333.             if (file == NULL) error("can't gzdopen stdin");
  334.             gz_uncompress(file, stdout);
  335.         } else {
  336.             file = gzdopen(fileno(stdout), outmode);
  337.             if (file == NULL) error("can't gzdopen stdout");
  338.             gz_compress(stdin, file);
  339.         }
  340.     } else {
  341.         if (copyout) {
  342.             SET_BINARY_MODE(stdout);
  343.         }
  344.         do {
  345.             if (uncompr) {
  346.                 if (copyout) {
  347.                     file = gzopen(*argv, "rb");
  348.                     if (file == NULL)
  349.                         fprintf(stderr, "%s: can't gzopen %sn", prog, *argv);
  350.                     else
  351.                         gz_uncompress(file, stdout);
  352.                 } else {
  353.                     file_uncompress(*argv);
  354.                 }
  355.             } else {
  356.                 if (copyout) {
  357.                     FILE * in = fopen(*argv, "rb");
  358.                     if (in == NULL) {
  359.                         perror(*argv);
  360.                     } else {
  361.                         file = gzdopen(fileno(stdout), outmode);
  362.                         if (file == NULL) error("can't gzdopen stdout");
  363.                         gz_compress(in, file);
  364.                     }
  365.                 } else {
  366.                     file_compress(*argv, outmode);
  367.                 }
  368.             }
  369.         } while (argv++, --argc);
  370.     }
  371.     return 0;
  372. }