UnzipFile.h
上传用户:sfdasf1q
上传日期:2021-05-08
资源大小:99k
文件大小:11k
源码类别:

压缩解压

开发平台:

Visual C++

  1. /* unzip.h -- IO for uncompress .zip files using zlib 
  2.    Version 0.15 beta, Mar 19th, 1998,
  3.    Copyright (C) 1998 Gilles Vollant
  4.    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
  5.      WinZip, InfoZip tools and compatible.
  6.    Encryption and multi volume ZipFile (span) are not supported.
  7.    Old compressions used by old PKZip 1.x are not supported
  8.    THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
  9.    CAN CHANGE IN FUTURE VERSION !!
  10.    I WAIT FEEDBACK at mail info@winimage.com
  11.    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
  12.    Condition of use and distribution are the same than zlib :
  13.   This software is provided 'as-is', without any express or implied
  14.   warranty.  In no event will the authors be held liable for any damages
  15.   arising from the use of this software.
  16.   Permission is granted to anyone to use this software for any purpose,
  17.   including commercial applications, and to alter it and redistribute it
  18.   freely, subject to the following restrictions:
  19.   1. The origin of this software must not be misrepresented; you must not
  20.      claim that you wrote the original software. If you use this software
  21.      in a product, an acknowledgment in the product documentation would be
  22.      appreciated but is not required.
  23.   2. Altered source versions must be plainly marked as such, and must not be
  24.      misrepresented as being the original software.
  25.   3. This notice may not be removed or altered from any source distribution.
  26. */
  27. /* for more info about .ZIP format, see 
  28.       ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
  29.    PkWare has also a specification at :
  30.       ftp://ftp.pkware.com/probdesc.zip */
  31. // modified by Tadeusz Dracz - 01.2000:
  32. // - added class' wrapers
  33. // - several bugs fixed
  34. // - several enhancements added
  35. // - MFC support added
  36. // - memory leaks eliminated when read error occured
  37. // - automaticaly free used memory on destruction or exception
  38. // - modern error notification using exceptions
  39. // Copyright (C) 2000 Tadeusz Dracz  
  40. // This notice may not be removed or altered from any source distribution.
  41. // UnzipFile.h: interface for the CUnzipFile class.
  42. //
  43. //////////////////////////////////////////////////////////////////////
  44. #if !defined(AFX_UNZIPFILE_H__44D45340_D71F_11D3_B7C7_E77339672847__INCLUDED_)
  45. #define AFX_UNZIPFILE_H__44D45340_D71F_11D3_B7C7_E77339672847__INCLUDED_
  46. #if _MSC_VER > 1000
  47. #pragma once
  48. #endif // _MSC_VER > 1000
  49. #include "ZUBaseFile.h"
  50. /* unz_global_info structure contain global data about the ZIPfile
  51.    These data comes from the end of central dir */
  52. struct unz_global_info
  53. {
  54. uLong number_entry;         /* total number of entries in
  55.        the central dir on this disk */
  56. uLong size_comment;         /* size of the global comment of the zipfile */
  57. };
  58. /* unz_file_info contain information about a file in the zipfile */
  59. struct unz_file_info
  60. {
  61.     uLong version;              /* version made by                 2 bytes */
  62.     uLong version_needed;       /* version needed to extract       2 bytes */
  63.     uLong flag;                 /* general purpose bit flag        2 bytes */
  64.     uLong compression_method;   /* compression method              2 bytes */
  65.     uLong dosDate;              /* last mod file date in Dos fmt   4 bytes */
  66.     uLong crc;                  /* crc-32                          4 bytes */
  67.     uLong compressed_size;      /* compressed size                 4 bytes */ 
  68.     uLong uncompressed_size;    /* uncompressed size               4 bytes */ 
  69.     uLong size_filename;        /* filename length                 2 bytes */
  70.     uLong size_file_extra;      /* extra field length              2 bytes */
  71.     uLong size_file_comment;    /* file comment length             2 bytes */
  72.     uLong disk_num_start;       /* disk number start               2 bytes */
  73.     uLong internal_fa;          /* internal file attributes        2 bytes */
  74.     uLong external_fa;          /* external file attributes        4 bytes */
  75.     CTime tmu_date;
  76. };
  77. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  78. struct unz_file_info_internal
  79. {
  80.     uLong offset_curfile;/* relative offset of local header 4 bytes */
  81. };
  82. /* file_in_zip_read_info_s contain internal information about a file in zipfile,
  83.     when reading and decompress it */
  84. struct file_in_zip_read_info
  85. {
  86. file_in_zip_read_info();
  87. ~file_in_zip_read_info();
  88. char  *read_buffer;         /* internal buffer for compressed data */
  89. z_stream stream;            /* zLib stream structure for inflate */
  90. uLong pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
  91. uLong stream_initialised;   /* flag set if stream structure is initialised*/
  92. uLong offset_local_extrafield;/* offset of the local extra field */
  93. uInt  size_local_extrafield;/* size of the local extra field */
  94. uLong pos_local_extrafield;   /* position in the local extra field in read*/
  95. uLong crc32;                /* crc32 of all data uncompressed */
  96. uLong crc32_wait;           /* crc32 we must obtain after decompress all */
  97. uLong rest_read_compressed; /* number of byte to be decompressed */
  98. uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
  99. //  CFile* file;                 /* io structore of the zipfile */
  100. uLong compression_method;   /* compression method (0==store) */
  101. uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  102. };
  103. /* unz_s contain internal information about the zipfile
  104. */
  105. struct unz_s
  106. {
  107. unz_s();
  108. ~unz_s();
  109. CFile file;                 /* io structore of the zipfile */
  110. unz_global_info gi;       /* public global information */
  111. uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  112. uLong num_file;             /* number of the current file in the zipfile*/
  113. uLong pos_in_central_dir;   /* pos of the current file in the central dir*/
  114. uLong current_file_ok;      /* flag about the usability of the current file*/
  115. uLong central_pos;          /* position of the beginning of the central dir*/
  116. uLong size_central_dir;     /* size of the central directory  */
  117. uLong offset_central_dir;   /* offset of start of central directory with
  118.    respect to the starting disk number */
  119. unz_file_info cur_file_info; /* public info about the current file in zip*/
  120. unz_file_info_internal cur_file_info_internal; /* private info about it*/
  121.     file_in_zip_read_info* pfile_in_zip_read; /* structure about the current
  122.                                     file if we are decompressing it */
  123. public:
  124. void free_pfile_in_zip_read();
  125. void alloc_pfile_in_zip_read();
  126. };
  127. class CUnzipFile : public CZUBaseFile
  128. {
  129. public:
  130. /*
  131.  fill in attibutes and time fields of the fileStatus with unz_file_info data
  132.  WARNING: if the file is opened the function closes it
  133. */
  134. void UpdateFileStatus(CFile & f, unz_file_info &ui);
  135. CUnzipFile();
  136. void Open(LPCTSTR lpszPath);
  137. /*
  138.   Give the current position in uncompressed data
  139. */
  140. z_off_t tell();
  141. /*
  142.   return true if the end of file was reached, false elsewhere 
  143. */
  144. bool eof();
  145. /*
  146.   Get the global comment string of the ZipFile, in the szComment buffer.
  147.   uSizeBuf is the size of the szComment buffer.
  148.   return the number of byte copied
  149. */
  150. int GetGlobalComment (char* szComment, uLong uSizeBuf);
  151. /*
  152.   Read extra field from the current file (opened by OpenCurrentFile)
  153.   This is the local-header version of the extra field (sometimes, there is
  154.     more info in the local-header version than in the central-header)
  155.   if buf==NULL, it return the size of the local extra field
  156.   if buf!=NULL, len is the size of the buffer, the extra header is copied in
  157. buf.
  158.   the return value is the number of bytes copied in buf
  159. */
  160. int GetLocalExtrafield (void* buf, UINT len);
  161. /*
  162.   Get Info about the current file
  163.   if file_info structure will contain somes info about
  164.     the current file
  165.   if szFileName!=NULL, the filemane string will be copied in szFileName
  166. (fileNameBufferSize is the size of the buffer)
  167.   if extraField!=NULL, the extra field information will be copied in extraField
  168. (extraFieldBufferSize is the size of the buffer).
  169. This is the Central-header version of the extra field
  170.   if szComment!=NULL, the comment string of the file will be copied in szComment
  171. (commentBufferSize is the size of the buffer)
  172. */
  173. void GetCurrentFileInfo ( unz_file_info* file_info, LPSTR szFileName = NULL, 
  174. uLong fileNameBufferSize = 0, void *extraField = NULL, uLong extraFieldBufferSize = 0,
  175. LPSTR szComment = NULL, uLong commentBufferSize = 0);
  176. /*
  177.   Write info about the ZipFile in the *pglobal_info structure.
  178.   No preparation of the structure is needed
  179. */
  180. void GetGlobalInfo(unz_global_info & global_info);
  181. /***************************************************************************/
  182. /* Unzip package allow you browse the directory of the zipfile */
  183. /*
  184.   Set the current file of the zipfile to the first file.
  185. */
  186. void GoToFirstFile();
  187. /*
  188.   Set the current file of the zipfile to the next file.
  189.   return true if there is no problem
  190.   return false if the actual file was the latest.
  191. */
  192. bool GoToNextFile();
  193. /*
  194.   Try locate the file szFileName in the zipfile.
  195.   For the iCaseSensitivity signification, see unzStringFileNameCompare
  196.   return value :
  197.   true if the file is found. It becomes the current file.
  198.   false if the file is not found
  199. */
  200. bool LocateFile(CString szFileName, bool bCaseSensitive = false);
  201. /*
  202.   Open for reading data the current file in the zipfile.
  203. */
  204. void OpenCurrentFile();
  205. /*
  206.   Close the file in zip opened with OpenCurrentFile
  207.   throw badCrc error if all the file was read but the CRC is not good
  208. */
  209. void CloseCurrentFile();
  210. /*
  211.   Read bytes from the current file (opened by OpenCurrentFile)
  212.   buf contain buffer where data must be copied
  213.   len the size of buf.
  214.   return the number of byte copied if somes bytes are copied
  215.   return 0 if the end of file was reached
  216. */
  217. int ReadCurrentFile(void* buf, UINT len);
  218. void Close();
  219. int StringFileNameCompare(CString fileName1, CString fileName2, bool caseSensitive = false);
  220. CUnzipFile(LPCTSTR lpszPath);
  221. virtual ~CUnzipFile();
  222. protected:
  223. void unzlocal_GetCurrentFileInfoInternal( unz_file_info & file_info, 
  224. unz_file_info_internal & file_info_internal, LPSTR szFileName,
  225. uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize,
  226. LPSTR szComment, uLong commentBufferSize);
  227. void unzlocal_DosDateToTmuDate(unz_file_info & file_info);
  228. void unzlocal_CheckCurrentFileCoherencyHeader (uInt & iSizeVar, uLong & offset_local_extrafield, uLong & size_local_extrafield);
  229. uLong unzlocal_SearchCentralDir();
  230. void unzlocal_getLong (uLong & pX);
  231. void unzlocal_getShort (uLong & pX);
  232. void unzlocal_getByte(int & pi);
  233. unz_s uf;
  234. };
  235. #endif // !defined(AFX_UNZIPFILE_H__44D45340_D71F_11D3_B7C7_E77339672847__INCLUDED_)