• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LinuxHunter
原创+技巧=无坚不摧
博客园    首页    新随笔    联系   管理    订阅  订阅
使用zzip和minizip解压缩文件
使用zzip和minizip解压缩文件
#include <zzip/zzip.h>
#include
<zlib.h>
#include
<zip.h>
#include
<unzip.h>

#include
<string>
#include
<map>

#ifdef _DEBUG
#pragma comment( lib, "zlib_d.lib")
#pragma comment( lib, "zzip_d.lib")
#pragma comment( lib, "minizip_d.lib")
#else
#pragma comment( lib, "zlib.lib" )
#pragma comment( lib, "zzip.lib" )
#pragma comment( lib, "minizip.lib" )
#endif

#define SAFE_DELETEARRAY(p) if(p != NULL) { delete[] (p); (p) = NULL; };

void UnzipAndZip( const char* pScrFileName, const char* pDstFileName )
{
struct FILE_DESC
{
unsigned
char* pData;
size_t DataSize;
};

std::map
<std::string, FILE_DESC> mapFileStreams;

ZZIP_DIR
* pZipDir = zzip_opendir( pScrFileName );
ZZIP_DIRENT
* pZipDirent = NULL;
while( pZipDirent = zzip_readdir( pZipDir ) )
{
size_t length
= strlen(pZipDirent->d_name);
if( length > 0 )
{
if( pZipDirent->d_name[length - 1] != '\\' &&
pZipDirent
->d_name[length - 1] != '/' )
{
ZZIP_FILE
* pZipFile = zzip_file_open( pZipDir, pZipDirent->d_name, ZZIP_CASELESS );
if( pZipFile != NULL )
{
ZZIP_STAT sz;
memset(
&sz, 0, sizeof(sz) );
zzip_file_stat( pZipFile,
&sz );
if( sz.st_size > 0 )
{
unsigned
char* pBuffer = new unsigned char[sz.st_size];
zzip_file_read( pZipFile, pBuffer, sz.st_size );
FILE_DESC data
= { pBuffer, sz.st_size };
mapFileStreams[pZipDirent
->d_name] = data;
}
zzip_file_close( pZipFile );
}
}
}
}
if( pZipDir )
zzip_closedir( pZipDir );

zip_fileinfo ZipFileInfo;
zipFile ZipFile
= zipOpen( pDstFileName, 0 );

std::map
<std::string, FILE_DESC>::iterator iter =
mapFileStreams.begin();

while( iter != mapFileStreams.end() )
{
int err = zipOpenNewFileInZip( ZipFile, iter->first.c_str(),
&ZipFileInfo, NULL, 0, NULL, 0, NULL, 0, 0 );
zipWriteInFileInZip( ZipFile, iter
->second.pData, iter->second.DataSize );
SAFE_DELETEARRAY( iter
->second.pData );
++iter;
}
zipClose( ZipFile, NULL );
}

int main(int argc, char* argv[])
{
UnzipAndZip(
"test.zip", "dst.zip" );
return 0;
};
posted on 2011-04-20 15:15  LinuxHunter  阅读(15640)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3