⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipfile.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 4 页
字号:
/*  Copyright (c) 1990-2006 Info-ZIP.  All rights reserved.  See the accompanying file LICENSE, version 2005-Feb-10 or later  (the contents of which are also included in zip.h) for terms of use.  If, for some reason, all these files are missing, the Info-ZIP license  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html*//* *  zipfile.c by Mark Adler. */#define __ZIPFILE_C#include "zip.h"#include "revision.h"#ifdef VMS#  include <rms.h>#  include "vms/vmsmunch.h"#  include "vms/vmsdefs.h"#endif#ifdef __RSXNT__#  include <windows.h>#endif/* * XXX start of zipfile.h */#ifdef THEOS/* Macros cause stack overflow in compiler */ush SH(uch* p) { return ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)); }ulg LG(uch* p) { return ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16)); }#else /* !THEOS *//* Macros for converting integers in little-endian to machine format */#define SH(a) ((ush)(((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8)))#define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))#endif /* ?THEOS *//* Macros for writing machine integers to little-endian format */#define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}#define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}/* -- Structure of a ZIP file -- *//* Signatures for zip file information headers */#define LOCSIG     0x04034b50L#define CENSIG     0x02014b50L#define ENDSIG     0x06054b50L#define EXTLOCSIG  0x08074b50L/* Offsets of values in headers */#define LOCVER  0               /* version needed to extract */#define LOCFLG  2               /* encrypt, deflate flags */#define LOCHOW  4               /* compression method */#define LOCTIM  6               /* last modified file time, DOS format */#define LOCDAT  8               /* last modified file date, DOS format */#define LOCCRC  10              /* uncompressed crc-32 for file */#define LOCSIZ  14              /* compressed size in zip file */#define LOCLEN  18              /* uncompressed size */#define LOCNAM  22              /* length of filename */#define LOCEXT  24              /* length of extra field */#define EXTCRC  0               /* uncompressed crc-32 for file */#define EXTSIZ  4               /* compressed size in zip file */#define EXTLEN  8               /* uncompressed size */#define CENVEM  0               /* version made by */#define CENVER  2               /* version needed to extract */#define CENFLG  4               /* encrypt, deflate flags */#define CENHOW  6               /* compression method */#define CENTIM  8               /* last modified file time, DOS format */#define CENDAT  10              /* last modified file date, DOS format */#define CENCRC  12              /* uncompressed crc-32 for file */#define CENSIZ  16              /* compressed size in zip file */#define CENLEN  20              /* uncompressed size */#define CENNAM  24              /* length of filename */#define CENEXT  26              /* length of extra field */#define CENCOM  28              /* file comment length */#define CENDSK  30              /* disk number start */#define CENATT  32              /* internal file attributes */#define CENATX  34              /* external file attributes */#define CENOFF  38              /* relative offset of local header */#define ENDDSK  0               /* number of this disk */#define ENDBEG  2               /* number of the starting disk */#define ENDSUB  4               /* entries on this disk */#define ENDTOT  6               /* total number of entries */#define ENDSIZ  8               /* size of entire central directory */#define ENDOFF  12              /* offset of central on starting disk */#define ENDCOM  16              /* length of zip file comment *//* Local functions */local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));local int scanzipf_reg OF((FILE *f));#ifndef UTIL   local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *));   local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));   local void zipoddities OF((struct zlist far *));   local int scanzipf_fix OF((FILE *f));#  ifdef USE_EF_UT_TIME     local int ef_scan_ut_time OF((char *ef_buf, extent ef_len, int ef_is_cent,                                   iztimes *z_utim));#  endif /* USE_EF_UT_TIME */   local void cutpath OF((char *p, int delim));#endif /* !UTIL *//* * XXX end of zipfile.h *//* Local data */#ifdef HANDLE_AMIGA_SFX   ulg amiga_sfx_offset;        /* place where size field needs updating */#endiflocal int zqcmp(a, b)ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries *//* Used by qsort() to compare entries in the zfile list. * Compares the internal names z->iname */{  return namecmp((*(struct zlist far **)a)->iname,                 (*(struct zlist far **)b)->iname);}#ifndef UTILlocal int rqcmp(a, b)ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries *//* Used by qsort() to compare entries in the zfile list. * Compare the internal names z->iname, but in reverse order. */{  return namecmp((*(struct zlist far **)b)->iname,                 (*(struct zlist far **)a)->iname);}local int zbcmp(n, z)ZCONST zvoid *n;        /* string to search for */ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry *//* Used by search() to compare a target to an entry in the zfile list. */{  return namecmp((char *)n, ((struct zlist far *)z)->zname);}struct zlist far *zsearch(n)ZCONST char *n;         /* name to find *//* Return a pointer to the entry in zfile with the name n, or NULL if   not found. */{  zvoid far **p;        /* result of search() */  if (zcount &&      (p = search(n, (ZCONST zvoid far **)zsort, zcount, zbcmp)) != NULL)    return *(struct zlist far **)p;  else    return NULL;}#endif /* !UTIL */#ifndef VMS     /* See vms/vms.c for VMS-specific ziptyp(). */#  ifndef PATHCUT#    define PATHCUT '/'#  endifchar *ziptyp(s)char *s;                /* file name to force to zip *//* If the file name *s has a dot (other than the first char), or if   the -A option is used (adjust self-extracting file) then return   the name, otherwise append .zip to the name.  Allocate the space for   the name in either case.  Return a pointer to the new name, or NULL   if malloc() fails. */{  char *q;              /* temporary pointer */  char *t;              /* pointer to malloc'ed string */#ifdef THEOS  char *r;              /* temporary pointer */  char *disk;#endif  if ((t = malloc(strlen(s) + 5)) == NULL)    return NULL;  strcpy(t, s);#ifdef __human68k__  _toslash(t);#endif#ifdef MSDOS  for (q = t; *q; INCSTR(q))    if (*q == '\\')      *q = '/';#endif /* MSDOS */#ifdef __RSXNT__   /* RSXNT/EMX C rtl uses OEM charset */  AnsiToOem(t, t);#endif  if (adjust) return t;#ifndef RISCOS# ifndef QDOS#  ifdef AMIGA  if ((q = MBSRCHR(t, '/')) == NULL)    q = MBSRCHR(t, ':');  if (MBSRCHR((q ? q + 1 : t), '.') == NULL)#  else /* !AMIGA */#    ifdef THEOS  /* the argument expansion add a dot to the end of file names when   * there is no extension and at least one of a argument has wild cards.   * So check for at least one character in the extension if there is a dot   * in file name */  if ((q = MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.')) == NULL    || q[1] == '\0') {#    else /* !THEOS */#      ifdef TANDEM  if (MBSRCHR((q = MBSRCHR(t, '.')) == NULL ? t : q + 1, ' ') == NULL)#      else /* !TANDEM */  if (MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)#      endif /* ?TANDEM */#    endif /* ?THEOS */#  endif /* ?AMIGA */#  ifdef CMS_MVS    if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)#  endif /* CMS_MVS */#  ifdef THEOS    /* insert .zip extension before disk name */    if ((r = MBSRCHR(t, ':')) != NULL) {        /* save disk name */        if ((disk = strdup(r)) == NULL)            return NULL;        strcpy(r[-1] == '.' ? r - 1 : r, ".zip");        strcat(t, disk);        free(disk);    } else {        if (q != NULL && *q == '.')          strcpy(q, ".zip");        else          strcat(t, ".zip");    }  }#  else /* !THEOS */#    ifdef TANDEM     /*  Tandem can't cope with extensions */    strcat(t, " ZIP");#    else /* !TANDEM */    strcat(t, ".zip");#    endif /* ?TANDEM */#  endif /* ?THEOS */# else /* QDOS */  q = LastDir(t);  if(MBSRCHR(q, '_') == NULL && MBSRCHR(q, '.') == NULL)  {      strcat(t, "_zip");  }# endif /* QDOS */#endif /* !RISCOS */  return t;}#endif /* !VMS */#ifndef UTILlocal void zipoddities(z)struct zlist far *z;{    if ((z->vem >> 8) >= NUM_HOSTS)    {        sprintf(errbuf, "made by version %d.%d on system type %d: ",                (ush)(z->vem & 0xff) / (ush)10, (ush)(z->vem & 0xff) % (ush)10,                z->vem >> 8);        zipwarn(errbuf, z->zname);    }    if (z->ver != 10 && z->ver != 11 && z->ver != 20)    {        sprintf(errbuf, "needs unzip %d.%d on system type %d: ",                (ush)(z->ver & 0xff) / (ush)10,                (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8);        zipwarn(errbuf, z->zname);    }    if (z->flg != z->lflg)    {        sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",                z->lflg, z->flg);        zipwarn(errbuf, z->zname);    }    else if (z->flg & ~0xf)    {        sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);        zipwarn(errbuf, z->zname);    }    if (z->how > DEFLATE)    {        sprintf(errbuf, "unknown compression method %u: ", z->how);        zipwarn(errbuf, z->zname);    }    if (z->dsk)    {        sprintf(errbuf, "starts on disk %u: ", z->dsk);        zipwarn(errbuf, z->zname);    }    if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC)    {        sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);        zipwarn(errbuf, z->zname);    }#if 0/* This test is ridiculous, it produces an error message for almost every *//* platform of origin other than MS-DOS, Unix, VMS, and Acorn!  Perhaps   *//* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for?      */    if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL)    {        sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx);        zipwarn(errbuf, z->zname);    }#endif    if (z->ext || z->cext)    {        if (z->ext && z->cext && z->extra != z->cextra)        {          sprintf(errbuf,                  "local extra (%ld bytes) != central extra (%ld bytes): ",                  (ulg)z->ext, (ulg)z->cext);          if (noisy) fprintf(stderr, "\tzip info: %s%s\n", errbuf, z->zname);        }#if (!defined(RISCOS) && !defined(CMS_MVS))        /* in noisy mode, extra field sizes are always reported */        else if (noisy)#else /* RISCOS || CMS_MVS *//* avoid warnings for zipfiles created on the same type of OS system! *//* or, was this warning really intended (eg. OS/2)? */        /* Only give info if extra bytes were added by another system */        else if (noisy && ((z->vem >> 8) != (OS_CODE >> 8)))#endif /* ?(RISCOS || CMS_MVS) */        {            fprintf(stderr, "zip info: %s has %ld bytes of %sextra data\n",                    z->zname, z->ext ? (ulg)z->ext : (ulg)z->cext,                    z->ext ? (z->cext ? "" : "local ") : "central ");        }    }}/* * scanzipf_fix is called with zip -F or zip -FF * read the file from front to back and pick up the pieces * NOTE: there are still checks missing to see if the header *       that was found is *VALID* */local int scanzipf_fix(f)  FILE *f;                      /* zip file *//*   The name of the zip file is pointed to by the global "zipfile".  The globals   zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.   Return an error code in the ZE_ class.*/{    ulg a = 0L;                 /* attributes returned by filetime() */    char b[CENHEAD];            /* buffer for central headers */    ush flg;                    /* general purpose bit flag */    int m;                      /* mismatch flag */    extent n;                   /* length of name */    ulg p;                      /* current file offset */    ulg s;                      /* size of data, start of central */    struct zlist far * far *x;  /* pointer last entry's link */    struct zlist far *z;        /* current zip entry structure */    /* Get any file attribute valid for this OS, to set in the central     * directory when fixing the archive:     */#ifndef UTIL    filetime(zipfile, &a, (long*)&s, NULL);#endif    x = &zfiles;                        /* first link */    p = 0;                              /* starting file offset */#ifdef HANDLE_AMIGA_SFX    amiga_sfx_offset = 0L;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -