使用minizip压缩文件
先决条件:
首先下载minizip 的库文件http://www.winimage.com/zLibDll/minizip.html
其次需要添加头文件支持 #include "../../zip.h" #include "../../unzip.h"
另外,要添加库文件的支持 UnzipUD.lib
几个重要的结构
目标文件结构:zipFile zf;
待压缩文件的结构:zip_fileinfo zi;//zip文件信息
几个重要的函数:
打开目标文件:zf = zipOpen(W2CA(strZipFile),0);
添加新的压缩文件:zipOpenNewFileInZip(...)//添加新的压缩文件
将数据写入目标文件:zipWriteInFileInZip (zf,buf,size_read);//写入目标文件中
关闭目标文件:zipClose(zf,NULL);//关闭目标文件
网络资源:
http://www.codeproject.com/KB/files/zip_utils.aspx
http://www.codeproject.com/KB/stl/zipstream.aspx
http://blog.csdn.net/yyyzlf/archive/2009/11/19/4833667.aspx
http://ty263.spaces.live.com/blog/cns!AEDC97760A4C885F!156.entry
http://www.vckbase.com/english/code/misc/mapizip.shtml.htm
附例:
void CScribbleDoc::OnFileSendZippedMail()
{
ASSERT_VALID(this);
CString strMessage;
CString strZipFile;
int errclose = ZIP_OK;
int err=0;
BOOL bModified = IsModified(); //remember original modified flag.
CString strOldName = m_strPathName;
CString strDir;
#ifndef NO_COLESERVERDOC
ASSERT(m_bRemember);
LPSTORAGE lpOrigStg = m_lpRootStg;
m_lpRootStg = NULL;
#endif
TRY
{
#ifndef NO_COLESERVERDOC
m_bRemember = FALSE;
#endif
CWaitCursor wait;
TCHAR szTempName[_MAX_PATH];
TCHAR szPath[_MAX_PATH];
BOOL bRemoveTemp = FALSE;
CString strFile;
TCHAR szDrive[_MAX_DRIVE],szDir[_MAX_DIR], szName[_MAX_FNAME], szExt[_MAX_EXT];
VERIFY(GetTempPath(_countof(szPath), szPath) != 0);
if (m_strPathName.IsEmpty() || IsModified())
{
// save to temporary path.
VERIFY(GetTempFileName(szPath, _T("afx"), 0, szTempName) != 0);
BOOL bResult = DoSave(szTempName, FALSE);
if (!bResult)
{
//We could not save the temp file. Disk full? Not enough privileges?
AfxFormatString1(strMessage, AFX_IDP_FAILED_IO_ERROR_WRITE,szTempName);
THROW (new CFileException);
}
bRemoveTemp = TRUE;
strFile = m_strTitle;
if (m_strTitle.Find('.') == -1) // no extension.
{
CString strExt;
CDocTemplate* pTemplate = GetDocTemplate();
if (pTemplate != NULL && pTemplate->GetDocString(strExt, CDocTemplate::filterExt))
{
strFile += strExt;
}
}
}
else
{
// use actual file since it isn't modified.
lstrcpyn(szTempName, m_strPathName, _countof(szTempName));
_tsplitpath(m_strPathName,NULL,NULL,szName, szExt);
strFile = CString(szName)+ CString(szExt);
}
zipFile zf;
//We try to create a temporary file in the temp-directory, delete it and create a directory with
//the same name and use that for our newly created ZIP-File:
VERIFY(GetTempFileName(szPath, _T("afx"), 0, strZipFile.GetBufferSetLength(_MAX_PATH+32) ) != 0);
strZipFile.ReleaseBuffer();
CFile::Remove(strZipFile);
if (!CreateDirectory(strZipFile,NULL))
{
//Huh, our process has been preempted and someone else created a file or directory with our temp name?
//Are we not allowed to create this directory? Not enough space to create this directory?
//Get out of this hell!
AfxFormatString1(strMessage, AFX_IDP_FAILED_ACCESS_WRITE,strZipFile);
THROW (new CFileException);
}
strDir = strZipFile; //save the temp directory in a variable so we can delete the directory afterwards
strZipFile+=CString(_T("//"))+strFile;
_tsplitpath(strZipFile,szDrive,szDir,szName, szExt);
_tmakepath(strZipFile.GetBufferSetLength(_MAX_PATH+32),szDrive,szDir,szName, _T("zip"));
strZipFile.ReleaseBuffer();
#ifdef UNICODE
USES_CONVERSION;
zf = zipOpen(W2CA(strZipFile),0);
#else
zf = zipOpen(strZipFile,0);
#endif
FILE * fin;
void* buf=NULL;
int size_buf=0;
size_buf = WRITEBUFFERSIZE;
buf = (void*)malloc(size_buf);
if (!buf)
{
//Not enough memory.
strMessage.LoadString(AFX_IDS_MEMORY_EXCEPTION);
THROW (new CMemoryException);
}
int size_read;
zip_fileinfo zi;
int opt_compress_level=Z_BEST_COMPRESSION;
//Best compression to save bandwidth at a maximum.
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
zi.tmz_date.tm_mday = zi.tmz_date.tm_min = zi.tmz_date.tm_year = 0;
zi.dosDate = 0;
zi.internal_fa = 0;
zi.external_fa = 0;
filetime(szTempName,&zi.dosDate);
err = zipOpenNewFileInZip(zf,
#ifdef UNICODE
W2CA(strFile),
#else
strFile,
#endif
&zi,NULL,0,NULL,0,NULL /* comment*/,(opt_compress_level != 0) ? Z_DEFLATED : 0,opt_compress_level);
ASSERT(err == ZIP_OK);
if (err != ZIP_OK)
{
TRACE1("error in opening %s in zipfile/n",strFile);
AfxFormatString1(strMessage, AFX_IDP_FAILED_IO_ERROR_WRITE,strFile);
THROW (new CFileException); //if we would not return we would use an uninitialized fin variable
}
else
{
fin = _tfopen(szTempName,_T("rb"));
if (fin==NULL)
{
err=ZIP_ERRNO;
TRACE1("error in opening %s for reading/n",szTempName);
strMessage.LoadString(AFX_IDP_FAILED_TO_OPEN_DOC);
THROW (new CFileException);
}
}
do
{
err = ZIP_OK;
size_read = fread(buf,1,size_buf,fin);
if (size_read < size_buf)
if (feof(fin)==0)
{
//Seems like we could not read from the temp name.
AfxFormatString1(strMessage, AFX_IDP_FAILED_IO_ERROR_READ, szTempName);
AfxMessageBox(strMessage);
strMessage.Empty();
err = ZIP_ERRNO;
}
if (err==ZIP_OK && size_read>0)
{
err = zipWriteInFileInZip (zf,buf,size_read);
if (err<0)
{
//We could not write the file in the ZIP-File for whatever reason.
AfxFormatString1(strMessage, AFX_IDP_FAILED_IO_ERROR_WRITE,strZipFile);
AfxMessageBox(strMessage);
strMessage.Empty();
}
}
}
while (err==ZIP_OK && size_read>0);
fclose(fin);
errclose = zipClose(zf,NULL);
if (bRemoveTemp)
CFile::Remove(szTempName);
free (buf);
}
CATCH_ALL(e)
{
SetModifiedFlag(bModified);
m_strPathName = strOldName;
#ifndef NO_COLESERVERDOC
m_lpRootStg = lpOrigStg;
m_bRemember = TRUE;
#endif
if (!strMessage.IsEmpty())
{
AfxMessageBox(strMessage);
return;
}
else
THROW_LAST();
}
END_CATCH_ALL
#ifndef NO_COLESERVERDOC
m_lpRootStg = lpOrigStg;
m_bRemember = TRUE;
#endif
m_strPathName = strZipFile;
if (errclose != ZIP_OK && err==ZIP_OK)
{
//We could not close the ZIP-File
AfxFormatString1(strMessage, AFX_IDP_FAILED_IO_ERROR_WRITE,strZipFile);
AfxMessageBox(strMessage);
}
else if (err==ZIP_OK) //if err!=ZIP_OK, something in creating the zip file went wrong, we already had a message box.
OnFileSendMail(); //yes, everything went the right way up to now. Now invoke the MFC MAPI call OnFileSendMail().
//After we have cheated the MFC OnFileSendMail() call with a ZIP-File instead of a real document,
//we can reset everything to the previously saved values:
SetModifiedFlag(bModified);
m_strPathName = strOldName;
//From now on we don't care if calls succeed. Other apps may have our files or directories open, who knows?
if (_tcslen(strZipFile))
CFile::Remove(strZipFile);
if (_tcslen(strDir))
RemoveDirectory( strDir );
}
使用minizip压缩文件相关推荐
- 用minizip + ZLib 1.2.11 实现压缩文件解压
去ZLIB官网下载最新版本1.2.11,自带minizip,在\contrib\minizip目录下面,由于zlib本身只是对字符串进行了编码压缩(有的人说zlib一次只能压缩一个文件,无法压缩多个文 ...
- Python 创建加密压缩文件
Python 创建加密压缩文件 1. 配置ZLib http://www.winimage.com/zLibDll/minizip.html 下载之后将 zlibwapi.dll 放在 demo/目录 ...
- zlib minizip 压缩与解压缩
本文转自:http://blog.csdn.net/MulinB/article/details/6393139 本文转自:http://blog.csdn.net/tinyhum3d/article ...
- iOS中使用ZipArchive 压缩和解压缩文件
引自 http://www.36duo.com/thread-119-1-1.html 在本教程中,我将演示如何在iOS程序中压缩和解压缩文件.我们将使用一个叫做ZipArchive的第三方库来实现. ...
- 采用ZLIB及MINIZIP进行文件解压缩及加解密
from:http://www.luoriver.com/news-d-i233-s25-m58-b58.html ZLIB开源库采用的是DEFLATE压缩算法,已经不支持加密功能,实际上功能还存在于 ...
- C++ 压缩文件夹(一)
采用zlib与minizip实现压缩文件夹为zip文件,实现效果:
- python 图像压缩后前端解压_Python在后台自动解压各种压缩文件的实现方法
1.需求描述 编写一个 Python 程序,每次下载压缩包形式的文件后,自动将内部文件解压到当前文件夹后将压缩包删除,通过本案例可以学到的知识点: os 模块综合应用 glob 模块综合应用 利用 g ...
- linux 压缩文件夹格式,Linux下常见文件格式的压缩、解压小结
Linux下常见文件格式的压缩.解压小结 .tar 解包: tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ...
- linux下解压缩文件中文乱码问题的解决
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 虽然2005年就有人把这报告为bu ...
最新文章
- virtual hust 2013.6.20 数论基础题目 D - Just the Facts
- 世界级安全技术专家力作——《Linux防火墙》
- 数据蒋堂 | 从一道招聘考题谈起
- zlib和openssl相关库错误的解决
- C++(STL):31 ---关联式容器map源码剖析
- 【clickhouse】ClickHouse中的低基数(LowCardinality)类型
- 【模板】链式前向星+spfa
- 如何保持连接_酒店厨房设备如何保养清洗?
- Softether软件原理分析
- c语言float和char几个字节,C语言当中int,float,double,char这四个有什么区别?
- zip命令 – 压缩文件
- 2018年阿里巴巴重要开源项目汇总
- 延时加载(lazy load)
- android iphone 记事本,手机上用什么记事本软件好?iPhone求推荐一款便签记事本app...
- stm32将flash虚拟成U盘来下载程序
- php ldap 登陆验证,LDAP用户验证功能简介
- adb命令查看手机设备
- 求最小公倍数 java
- 十大股票资讯网站排名 炒股资讯网站排行榜 炒股必看的资讯网站推荐
- 深入理解矩阵的特征值和特征向量
热门文章
- 终于知道怎么看辐射3的地图了
- crontab fastadmin thinkphp 定时任务权限不足
- 高通 android 7.0 插入蓝牙耳机,声音变的最大!
- bluedroid源码分析之ACL包发送和接收(二)
- 公众号如何涨粉,提高阅读量
- 808 Lab虚拟插件:Sample Science 808 Lab for Mac
- 关于显卡的一些参数说明,告诉你如何分辨哪个好一点,哪个烂一点。
- 小盒子可以在大盒子里面移动
- Java中如何实现添加用户信息_如何通过Java客户端在Active Directory中创建新用户并将其添加到现有组...
- 微信自定义分享、二次分享解决方案