XUnzip 源码分析
kelvin 发布于 2024-08-10

duilib项目中引用了XUnzip进行zip压缩文件的解压缩读取,看了介绍,这个源码是参考修改gzip的源码,不算注释等无用的代码,大概四千行左右的代码量,

有下面函数

1、HZIP OpenZipU(void *z,unsigned int len,DWORD flags)
2、ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze)
3、ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *zew)
4、ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
5、ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *zew)
6、ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags)
7、ZRESULT CloseZipU(HZIP hz)
8、bool IsZipHandleU(HZIP hz)
9、ZRESULT TUnzip::Unzip(int index,void *dst,unsigned int len,DWORD flags)
10、void EnsureDirectory(const TCHAR *rootdir, const TCHAR *dir)
11、ZRESULT TUnzip::Find(const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
12、ZRESULT TUnzip::Get(int index,ZIPENTRY *ze)
13、ZRESULT TUnzip::Open(void *z,unsigned int len,DWORD flags)
14、int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
15、int unzCloseCurrentFile (unzFile file)
16、int unzGetLocalExtrafield (unzFile file,voidp buf,unsigned len)
17、int unzReadCurrentFile  (unzFile file, voidp buf, unsigned len)
18、unzFile unzOpenInternal(LUFILE *fin)
19、nt unzClose (unzFile file)
20、int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
21、void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
22、int unzlocal_GetCurrentFileInfoInternal (unzFile file, unz_file_info *pfile_info, unz_file_info_internal *pfile_info_internal, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
23、int unzGetCurrentFileInfo (unzFile file, unz_file_info *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
24、int unzGoToFirstFile (unzFile file)
25、int unzGoToNextFile (unzFile file)
26、int unzLocateFile (unzFile file, const TCHAR *szFileName, int iCaseSensitivity)
27、int unzlocal_CheckCurrentFileCoherencyHeader (unz_s *s,uInt *piSizeVar, uLong *poffset_local_extrafield, uInt  *psize_local_extrafield)
28、int unzOpenCurrentFile (unzFile file)
29、int unzReadCurrentFile  (unzFile file, voidp buf, unsigned len)
30、z_off_t unztell (unzFile file)
31、int unzGetLocalExtrafield (unzFile file,voidp buf,unsigned len)
32、int unzCloseCurrentFile (unzFile file)
33、int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)

在duilib找的一段解压缩读取文件的参考代码

 sFile += CPaintManagerUI::GetResourceZip();
 HZIP hz = NULL;
 if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
 else hz = OpenZip((void*)sFile.GetData(), 0, 2);
 if( hz == NULL ) return _Failed(_T("Error opening zip file"));
 ZIPENTRY ze; 
 int i; 
 if( FindZipItem(hz, pstrFilename, true, &i, &ze) != 0 ) return _Failed(_T("Could not find ziped file"));
 DWORD dwSize = ze.unc_size;
 if( dwSize == 0 ) return _Failed(_T("File is empty"));
 if ( dwSize > 4096*1024 ) return _Failed(_T("File too large"));
 BYTE* pByte = new BYTE[ dwSize ];
 int res = UnzipItem(hz, i, pByte, dwSize, 3);
 if( res != 0x00000000 && res != 0x00000600) {
     delete[] pByte;
     if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
     return _Failed(_T("Could not unzip file"));
 }
 if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);

 

 

kelvin
关注 私信
文章
92
关注
0
粉丝
0