📄 unzip.c
字号:
unz_file_info_internal * pfile_info_internal, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize){ unz_s *s; unz_file_info file_info; unz_file_info_internal file_info_internal; int err = UNZ_OK; uLong uMagic; long lSeek = 0; if (file == NULL) return UNZ_PARAMERROR; s = (unz_s *) file; if (fseek (s->file, s->pos_in_central_dir + s->byte_before_the_zipfile, SEEK_SET) != 0) err = UNZ_ERRNO; /* we check the magic */ if (err == UNZ_OK) { if (unzlocal_getLong (s->file, &uMagic) != UNZ_OK) err = UNZ_ERRNO; else if (uMagic != 0x02014b50) err = UNZ_BADZIPFILE; } if (unzlocal_getShort (s->file, &file_info.version) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.version_needed) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.flag) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.compression_method) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &file_info.dosDate) != UNZ_OK) err = UNZ_ERRNO; unzlocal_DosDateToTmuDate (file_info.dosDate, &file_info.tmu_date); if (unzlocal_getLong (s->file, &file_info.crc) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &file_info.compressed_size) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &file_info.uncompressed_size) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.size_filename) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.size_file_extra) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.size_file_comment) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.disk_num_start) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &file_info.internal_fa) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &file_info.external_fa) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &file_info_internal.offset_curfile) != UNZ_OK) err = UNZ_ERRNO; lSeek += file_info.size_filename; if ((err == UNZ_OK) && (szFileName != NULL)) { uLong uSizeRead; if (file_info.size_filename < fileNameBufferSize) { *(szFileName + file_info.size_filename) = '\0'; uSizeRead = file_info.size_filename; } else uSizeRead = fileNameBufferSize; if ((file_info.size_filename > 0) && (fileNameBufferSize > 0)) if (fread (szFileName, (uInt) uSizeRead, 1, s->file) != 1) err = UNZ_ERRNO; lSeek -= uSizeRead; } if ((err == UNZ_OK) && (extraField != NULL)) { uLong uSizeRead; if (file_info.size_file_extra < extraFieldBufferSize) uSizeRead = file_info.size_file_extra; else uSizeRead = extraFieldBufferSize; if (lSeek != 0) { if (fseek (s->file, lSeek, SEEK_CUR) == 0) lSeek = 0; else err = UNZ_ERRNO; } if ((file_info.size_file_extra > 0) && (extraFieldBufferSize > 0)) if (fread (extraField, (uInt) uSizeRead, 1, s->file) != 1) err = UNZ_ERRNO; lSeek += file_info.size_file_extra - uSizeRead; } else lSeek += file_info.size_file_extra; if ((err == UNZ_OK) && (szComment != NULL)) { uLong uSizeRead; if (file_info.size_file_comment < commentBufferSize) { *(szComment + file_info.size_file_comment) = '\0'; uSizeRead = file_info.size_file_comment; } else uSizeRead = commentBufferSize; if (lSeek != 0) { if (fseek (s->file, lSeek, SEEK_CUR) == 0) lSeek = 0; else err = UNZ_ERRNO; } if ((file_info.size_file_comment > 0) && (commentBufferSize > 0)) if (fread (szComment, (uInt) uSizeRead, 1, s->file) != 1) err = UNZ_ERRNO; lSeek += file_info.size_file_comment - uSizeRead; } else lSeek += file_info.size_file_comment; if ((err == UNZ_OK) && (pfile_info != NULL)) *pfile_info = file_info; if ((err == UNZ_OK) && (pfile_info_internal != NULL)) *pfile_info_internal = file_info_internal; return err;}/* * Write info about the ZipFile in the *pglobal_info structure. No * preparation of the structure is needed return UNZ_OK if there is no * problem. */extern int ZEXPORT unzGetCurrentFileInfo ( unzFile file, unz_file_info * pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize){ return unzlocal_GetCurrentFileInfoInternal (file, pfile_info, NULL, szFileName, fileNameBufferSize, extraField, extraFieldBufferSize, szComment, commentBufferSize);}/* * Set the current file of the zipfile to the first file. return UNZ_OK if * there is no problem */extern int ZEXPORT unzGoToFirstFile (unzFile file){ int err = UNZ_OK; unz_s *s; if (file == NULL) return UNZ_PARAMERROR; s = (unz_s *) file; s->pos_in_central_dir = s->offset_central_dir; s->num_file = 0; err = unzlocal_GetCurrentFileInfoInternal (file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0); s->current_file_ok = (err == UNZ_OK); return err;}/* * Set the current file of the zipfile to the next file. return UNZ_OK if * there is no problem return UNZ_END_OF_LIST_OF_FILE if the actual file was * the latest. */extern int ZEXPORT unzGoToNextFile (unzFile file){ unz_s *s; int err; if (file == NULL) return UNZ_PARAMERROR; s = (unz_s *) file; if (!s->current_file_ok) return UNZ_END_OF_LIST_OF_FILE; if (s->num_file + 1 == s->gi.number_entry) return UNZ_END_OF_LIST_OF_FILE; s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment; s->num_file++; err = unzlocal_GetCurrentFileInfoInternal (file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0); s->current_file_ok = (err == UNZ_OK); return err;}/* * Try locate the file szFileName in the zipfile. For the iCaseSensitivity * signification, see unzipStringFileNameCompare * * return value : UNZ_OK if the file is found. It becomes the current file. * UNZ_END_OF_LIST_OF_FILE if the file is not found */extern int ZEXPORT unzLocateFile ( unzFile file, const char *szFileName, int iCaseSensitivity){ unz_s *s; int err; uLong num_fileSaved; uLong pos_in_central_dirSaved; if (file == NULL) return UNZ_PARAMERROR; if (strlen (szFileName) >= UNZ_MAXFILENAMEINZIP) return UNZ_PARAMERROR; s = (unz_s *) file; if (!s->current_file_ok) return UNZ_END_OF_LIST_OF_FILE; num_fileSaved = s->num_file; pos_in_central_dirSaved = s->pos_in_central_dir; err = unzGoToFirstFile (file); while (err == UNZ_OK) { char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1]; unzGetCurrentFileInfo (file, NULL, szCurrentFileName, sizeof (szCurrentFileName) - 1, NULL, 0, NULL, 0); if (unzStringFileNameCompare (szCurrentFileName, szFileName, iCaseSensitivity) == 0) return UNZ_OK; err = unzGoToNextFile (file); } s->num_file = num_fileSaved; s->pos_in_central_dir = pos_in_central_dirSaved; return err;}/* * Read the local header of the current zipfile Check the coherency of the * local header and info in the end of central directory about this file * store in *piSizeVar the size of extra info in local header (filename and * size of extra field data) */local int unzlocal_CheckCurrentFileCoherencyHeader ( unz_s * s, uInt * piSizeVar, uLong * poffset_local_extrafield, uInt * psize_local_extrafield){ uLong uMagic, uData, uFlags; uLong size_filename; uLong size_extra_field; int err = UNZ_OK; *piSizeVar = 0; *poffset_local_extrafield = 0; *psize_local_extrafield = 0; if (fseek (s->file, s->cur_file_info_internal.offset_curfile + s->byte_before_the_zipfile, SEEK_SET) != 0) return UNZ_ERRNO; if (err == UNZ_OK) { if (unzlocal_getLong (s->file, &uMagic) != UNZ_OK) err = UNZ_ERRNO; else if (uMagic != 0x04034b50) err = UNZ_BADZIPFILE; } if (unzlocal_getShort (s->file, &uData) != UNZ_OK) err = UNZ_ERRNO; /* * else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) * err=UNZ_BADZIPFILE; */ if (unzlocal_getShort (s->file, &uFlags) != UNZ_OK) err = UNZ_ERRNO; if (unzlocal_getShort (s->file, &uData) != UNZ_OK) err = UNZ_ERRNO; else if ((err == UNZ_OK) && (uData != s->cur_file_info.compression_method)) err = UNZ_BADZIPFILE; if ((err == UNZ_OK) && s->cur_file_info.compression_method > Z_DEFLATED) err = UNZ_BADZIPFILE; if (unzlocal_getLong (s->file, &uData) != UNZ_OK) /* date/time */ err = UNZ_ERRNO; if (unzlocal_getLong (s->file, &uData) != UNZ_OK) /* crc */ err = UNZ_ERRNO; else if ((err == UNZ_OK) && (uData != s->cur_file_info.crc) && ((uFlags & 8) == 0)) err = UNZ_BADZIPFILE; if (unzlocal_getLong (s->file, &uData) != UNZ_OK) /* size compr */ err = UNZ_ERRNO; else if ((err == UNZ_OK) && (uData != s->cur_file_info.compressed_size) && ((uFlags & 8) == 0)) err = UNZ_BADZIPFILE; if (unzlocal_getLong (s->file, &uData) != UNZ_OK) /* size uncompr */ err = UNZ_ERRNO; else if ((err == UNZ_OK) && (uData != s->cur_file_info.uncompressed_size) && ((uFlags & 8) == 0)) err = UNZ_BADZIPFILE; if (unzlocal_getShort (s->file, &size_filename) != UNZ_OK) err = UNZ_ERRNO; else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename)) err = UNZ_BADZIPFILE; *piSizeVar += (uInt) size_filename; if (unzlocal_getShort (s->file, &size_extra_field) != UNZ_OK) err = UNZ_ERRNO; *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_filename; *psize_local_extrafield = (uInt) size_extra_field; *piSizeVar += (uInt) size_extra_field; return err;}/* * Open for reading data the current file in the zipfile. If there is no * error and the file is opened, the return value is UNZ_OK. */extern int ZEXPORT unzOpenCurrentFile (unzFile file){ int err = UNZ_OK; uInt iSizeVar; unz_s *s; file_in_zip_read_info_s *pfile_in_zip_read_info; uLong offset_local_extrafield; /* offset of the local extra * field */ uInt size_local_extrafield; /* size of the local extra * field */ if (file == NULL) return UNZ_PARAMERROR; s = (unz_s *) file; if (!s->current_file_ok) return UNZ_PARAMERROR; if (s->pfile_in_zip_read != NULL) unzCloseCurrentFile (file); if (unzlocal_CheckCurrentFileCoherencyHeader (s, &iSizeVar, &offset_local_extrafield, &size_local_extrafield) != UNZ_OK) return UNZ_BADZIPFILE; pfile_in_zip_read_info = (file_in_zip_read_info_s *) ALLOC (sizeof (file_in_zip_read_info_s)); if (pfile_in_zip_read_info == NULL) return UNZ_INTERNALERROR; pfile_in_zip_read_info->read_buffer = (char *) ALLOC (UNZ_BUFSIZE); pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -