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

📄 win32zip.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 2 页
字号:
            if (m == ZE_MISS)              zipwarn("name not matched: ", a);            else              ziperr(m, a);          }          free((zvoid *)a);        }      }      CloseDirScan(d);    }    free((zvoid *)p);  } /* (s.st_mode & S_IFDIR) == 0) */  return ZE_OK;}int procname(n, caseflag)char *n;                /* name to process */int caseflag;           /* true to force case-sensitive match */{    return procname_win32(n, caseflag, INVALID_WIN32_FILE_ATTRIBS);}char *ex2in(x, isdir, pdosflag)char *x;                /* external file name */int isdir;              /* input: x is a directory */int *pdosflag;          /* output: force MSDOS file attributes? *//* Convert the external file name to a zip file name, returning the malloc'ed   string or NULL if not enough memory. */{  char *n;              /* internal file name (malloc'ed) */  char *t;              /* shortened name */  int dosflag;  dosflag = dosify || IsFileSystemOldFAT(x);  if (!dosify && use_longname_ea && (t = GetLongPathEA(x)) != NULL)  {    x = t;    dosflag = 0;  }  /* Find starting point in name before doing malloc */  /* Strip drive specification */  t = *x && isascii((uch)*x) && *(x + 1) == ':' ? x + 2 : x;  /* Strip "//host/share/" part of a UNC name */  if ((!strncmp(x,"//",2) || !strncmp(x,"\\\\",2)) &&      (x[2] != '\0' && x[2] != '/' && x[2] != '\\')) {    n = x + 2;    while (*n != '\0' && *n != '/' && *n != '\\')      INCSTR(n);        /* strip host name */    if (*n != '\0') {      INCSTR(n);      while (*n != '\0' && *n != '/' && *n != '\\')        INCSTR(n);      /* strip `share' name */    }    if (*n != '\0')      t = n + CLEN(n);  }  /* Strip leading "/" to convert an absolute path into a relative path */  while (*t == '/' || *t == '\\')    t++;  /* Strip leading "./" as well as drive letter */  while (*t == '.' && (t[1] == '/' || t[1] == '\\'))    t += 2;  /* Make changes, if any, to the copied name (leave original intact) */  for (n = t; *n; INCSTR(n))    if (*n == '\\')      *n = '/';  if (!pathput)    t = last(t, PATH_END);  /* Malloc space for internal name and copy it */  if ((n = malloc(strlen(t) + 1)) == NULL)    return NULL;  strcpy(n, t);  if (dosify)    msname(n);  /* Returned malloc'ed name */  if (pdosflag)    *pdosflag = dosflag;#ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */  OemToAnsi(n, n);#endif  return n;}char *in2ex(n)char *n;                /* internal file name *//* Convert the zip file name to an external file name, returning the malloc'ed   string or NULL if not enough memory. */{  char *x;              /* external file name */  if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)    return NULL;  strcpy(x, n);#ifdef __RSXNT__ /* RSXNT/EMX C rtl uses OEM charset */  AnsiToOem(x, x);#endif  return x;}ulg filetime(f, a, n, t)char *f;                /* name of file to get info on */ulg *a;                 /* return value: file attributes */long *n;                /* return value: file size */iztimes *t;             /* return value: access, modific. and creation times *//* If file *f does not exist, return 0.  Else, return the file's last   modified date and time as an MSDOS date and time.  The date and   time is returned in a long with the date most significant to allow   unsigned integer comparison of absolute times.  Also, if a is not   a NULL pointer, store the file attributes there, with the high two   bytes being the Unix attributes, and the low byte being a mapping   of that to DOS attributes.  If n is not NULL, store the file size   there.  If t is not NULL, the file's access, modification and creation   times are stored there as UNIX time_t values.   If f is "-", use standard input as the file. If f is a device, return   a file size of -1 */{  struct stat s;        /* results of stat() */  char *name;  unsigned int len = strlen(f);  int isstdin = !strcmp(f, "-");  if (f == label) {    if (a != NULL)      *a = label_mode;    if (n != NULL)      *n = -2L; /* convention for a label name */    if (t != NULL)      t->atime = t->mtime = t->ctime = label_utim;    return label_time;  }  if ((name = malloc(len + 1)) == NULL) {    ZIPERR(ZE_MEM, "filetime");  }  strcpy(name, f);  if (MBSRCHR(name, '/') == (name + len - 1))    name[len - 1] = '\0';  /* not all systems allow stat'ing a file with / appended */  if (isstdin) {    if (fstat(fileno(stdin), &s) != 0) {      free(name);      error("fstat(stdin)");    }    time((time_t *)&s.st_mtime);       /* some fstat()s return time zero */  } else if (LSSTAT(name, &s) != 0) {             /* Accept about any file kind including directories              * (stored with trailing / with -r option)              */    free(name);    return 0;  }  if (a != NULL) {    *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));  }  if (n != NULL)    *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;  if (t != NULL) {    t->atime = s.st_atime;    t->mtime = s.st_mtime;    t->ctime = s.st_ctime;  }  free(name);  return unix2dostime((time_t *)&s.st_mtime);}#ifdef NTSD_EASlocal void GetSD(char *path, char **bufptr, size_t *size,                        char **cbufptr, size_t *csize){  unsigned char stackbuffer[NTSD_BUFFERSIZE];  unsigned long bytes = NTSD_BUFFERSIZE;  unsigned char *buffer = stackbuffer;  unsigned char *DynBuffer = NULL;  long cbytes;  PEF_NTSD_L_HEADER pLocalHeader;  PEF_NTSD_C_HEADER pCentralHeader;  VOLUMECAPS VolumeCaps;  /* check target volume capabilities */  if (!ZipGetVolumeCaps(path, path, &VolumeCaps) ||     !(VolumeCaps.dwFileSystemFlags & FS_PERSISTENT_ACLS)) {    return;  }  VolumeCaps.bUsePrivileges = use_privileges;  VolumeCaps.dwFileAttributes = 0;  /* should set to file attributes, if possible */  if (!SecurityGet(path, &VolumeCaps, buffer, (LPDWORD)&bytes)) {    /* try to malloc the buffer if appropriate */    if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {        DynBuffer = malloc(bytes);        if(DynBuffer == NULL) return;        buffer = DynBuffer; /* switch to the new buffer and try again */        if(!SecurityGet(path, &VolumeCaps, buffer, (LPDWORD)&bytes)) {            free(DynBuffer);            return;        }    } else {        return; /* bail */    }  }  /* # bytes to compress: compress type, CRC, data bytes */  cbytes = (2 + 4 + EB_DEFLAT_EXTRA) + bytes;  /* our two possible failure points.  don't allow trashing of any data     if either fails - notice that *size and *csize don't get updated.     *bufptr leaks if malloc() was used and *cbufptr alloc fails - this     isn't relevant because it's probably indicative of a bigger problem. */  if(*size)    *bufptr = realloc(*bufptr, *size + EF_NTSD_L_LEN + cbytes);  else    *bufptr = malloc(EF_NTSD_L_LEN + cbytes);  if(*csize)    *cbufptr = realloc(*cbufptr, *csize + EF_NTSD_C_LEN);  else    *cbufptr = malloc(EF_NTSD_C_LEN);  if(*bufptr == NULL || *cbufptr == NULL) {    if(DynBuffer) free(DynBuffer);    return;  }  /* local header */  pLocalHeader = (PEF_NTSD_L_HEADER) (*bufptr + *size);  cbytes = memcompress(((char *)pLocalHeader + EF_NTSD_L_LEN), cbytes,                       (char *)buffer, bytes);  *size += EF_NTSD_L_LEN + cbytes;  pLocalHeader->nID = EF_NTSD;  pLocalHeader->nSize = (USHORT)(EF_NTSD_L_LEN - EB_HEADSIZE                                 + cbytes);  pLocalHeader->lSize = bytes; /* uncompressed size */  pLocalHeader->Version = 0;  /* central header */  pCentralHeader = (PEF_NTSD_C_HEADER) (*cbufptr + *csize);  *csize += EF_NTSD_C_LEN;  pCentralHeader->nID = EF_NTSD;  pCentralHeader->nSize = EF_NTSD_C_LEN - EB_HEADSIZE;  /* sbz */  pCentralHeader->lSize = bytes;  if (noisy)    printf(" (%ld bytes security)", bytes);  if(DynBuffer) free(DynBuffer);}#endif /* NTSD_EAS */#ifdef USE_EF_UT_TIME#define EB_L_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(3))#define EB_C_UT_SIZE    (EB_HEADSIZE + EB_UT_LEN(1))local int GetExtraTime(struct zlist far *z, iztimes *z_utim){  char *eb_l_ptr;  char *eb_c_ptr;  ulg ultime;  /* brain-dead IBM compiler defines time_t as "double", so we have to convert   * it into unsigned long integer number...   */#ifdef IZ_CHECK_TZ  if (!zp_tz_is_valid) return ZE_OK;    /* skip silently if no valid TZ info */#endif  if(z->ext)    eb_l_ptr = realloc(z->extra, (z->ext + EB_L_UT_SIZE));  else    eb_l_ptr = malloc(EB_L_UT_SIZE);  if (eb_l_ptr == NULL)    return ZE_MEM;  if(z->cext)    eb_c_ptr = realloc(z->cextra, (z->cext + EB_C_UT_SIZE));  else    eb_c_ptr = malloc(EB_C_UT_SIZE);  if (eb_c_ptr == NULL)    return ZE_MEM;  z->extra = eb_l_ptr;  eb_l_ptr += z->ext;  z->ext += EB_L_UT_SIZE;  eb_l_ptr[0]  = 'U';  eb_l_ptr[1]  = 'T';  eb_l_ptr[2]  = EB_UT_LEN(3);          /* length of data part of e.f. */  eb_l_ptr[3]  = 0;  eb_l_ptr[4]  = EB_UT_FL_MTIME | EB_UT_FL_ATIME | EB_UT_FL_CTIME;  ultime = (ulg)z_utim->mtime;  eb_l_ptr[5]  = (char)(ultime);  eb_l_ptr[6]  = (char)(ultime >> 8);  eb_l_ptr[7]  = (char)(ultime >> 16);  eb_l_ptr[8]  = (char)(ultime >> 24);  ultime = (ulg)z_utim->atime;  eb_l_ptr[9]  = (char)(ultime);  eb_l_ptr[10] = (char)(ultime >> 8);  eb_l_ptr[11] = (char)(ultime >> 16);  eb_l_ptr[12] = (char)(ultime >> 24);  ultime = (ulg)z_utim->ctime;  eb_l_ptr[13] = (char)(ultime);  eb_l_ptr[14] = (char)(ultime >> 8);  eb_l_ptr[15] = (char)(ultime >> 16);  eb_l_ptr[16] = (char)(ultime >> 24);  z->cextra = eb_c_ptr;  eb_c_ptr += z->cext;  z->cext += EB_C_UT_SIZE;  memcpy(eb_c_ptr, eb_l_ptr, EB_C_UT_SIZE);  eb_c_ptr[EB_LEN] = EB_UT_LEN(1);  return ZE_OK;}#endif /* USE_EF_UT_TIME */int set_extra_field(z, z_utim)  struct zlist far *z;  iztimes *z_utim;  /* create extra field and change z->att if desired */{#ifdef NTSD_EAS  if(ZipIsWinNT()) {    /* store SECURITY_DECRIPTOR data in local header,       and size only in central headers */    GetSD(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);  }#endif /* NTSD_EAS */#ifdef USE_EF_UT_TIME  /* store extended time stamps in both headers */  return GetExtraTime(z, z_utim);#else /* !USE_EF_UT_TIME */  return ZE_OK;#endif /* ?USE_EF_UT_TIME */}int deletedir(d)char *d;                /* directory to delete *//* Delete the directory *d if it is empty, do nothing otherwise.   Return the result of rmdir(), delete(), or system().   For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]). */{    return rmdir(d);}#endif /* !UTIL */

⌨️ 快捷键说明

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