如何用VC++来调用zlib库压缩文件夹?

我想把多级文件夹压缩到一个文件中,并且扩展名为pkg的资源文件。voidCZlibTestDlg::OnButton1(){constunsignedcharstrSrc... 我想把多级文件夹压缩到一个文件中,并且扩展名为pkg的资源文件。
void CZlibTestDlg::OnButton1()
{
const unsigned char strSrc[]="hello world!\n\
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试\
aaaaa bbbbb ccccc ddddd aaaaa bbbbb ccccc ddddd中文测试 中文测试";

unsigned char buff[1024]={0},strDst[1024]={0};
unsigned long srcLen=sizeof(strSrc),bufLen=sizeof(buff),dstLen=sizeof(strDst);
CString strc,strc2,strc3;
strc.Format("Src string:%s\nLength:%d\n===================\n",strSrc,srcLen);
//压缩
compress(buff,&bufLen,strSrc,srcLen);
strc2.Format("\nAfter Compressed Length:%d\nCompressed String:%s\n==============\n",bufLen,buff);

如何修改导入文字hello world!\n\
aaaaa bbbbb ....改成导入多级文件夹
压缩后输出为一个文件?
展开
 我来答
ziyang64
2011-03-15 · TA获得超过962个赞
知道小有建树答主
回答量:529
采纳率:0%
帮助的人:803万
展开全部
就是用findfirst, findnext找到所有目录下的文件,逐个压缩就是了。我把这两步分开来写的。
现在的输出格式是:
文件名长,文件名,压缩后的长度,压缩的内容。
仅是示例而已,根据自己的需要修改吧。

#include <stdio.h>
#include <tchar.h>

#include "zlib.h"

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

#include <windows.h>
#include <io.h>

using namespace std;

void recursiveGetFileNames( vector<string>& names, const char* path )
{
char savepath[ MAX_PATH ];
GetCurrentDirectory( sizeof( savepath ), savepath );

SetCurrentDirectory( path );
char fullpath[ MAX_PATH ];
GetCurrentDirectory( sizeof( fullpath ), fullpath );

_finddata_t fileinfo;
long handle = _findfirst( "*.*", &fileinfo );
if (handle == -1L)
return;
do
{
if ( fileinfo.attrib & _A_SUBDIR )
{
if ( strcmp( fileinfo.name, "." )
&& strcmp( fileinfo.name, ".." ) )
{
recursiveGetFileNames( names, fileinfo.name );
}
}
else
{
string str( fullpath );
str += "\\";
str += fileinfo.name;
names.push_back( str );
}
} while (::_findnext(handle, &fileinfo) == 0);
_findclose(handle);

SetCurrentDirectory( savepath );
}

class CompFile
{
public:
CompFile( ofstream& f )
: fout( f )
{
}

void operator()( string file);

private:
ofstream& fout;
};

void CompFile::operator()( string file )
{
size_t nlen = file.length();
fout.write( (char*)&nlen, 4 );
fout.write( file.c_str(), nlen );

ifstream fin( file.c_str(), ios::binary );
fin.seekg( 0, ios::end );
uLongf len = fin.tellg();
char* buf = new char[ len*2 ];

fin.seekg( 0, ios::beg );
fin.read( buf, len );

uLongf bufLen = len;
char* dest = buf+len;
compress( (Bytef*)dest, &bufLen, (Bytef*)buf, len );

fout.write( (char*)&bufLen, 4 );
fout.write( dest, bufLen );

delete[] buf;
}

int main(int argc, char* argv[])
{
if ( argc < 3 )
{
return 0;
}
vector<string> names;

recursiveGetFileNames( names, argv[2] );

ofstream fout( argv[1], ios::binary );

for_each( names.begin(), names.end(), CompFile(fout) );
}
追问
把这个代码放进那个文件里调试?我想吧多级文件压缩的,一个一个文件压缩不合适。
追答
建个console工程,把这个代码放进去,编译成exe后,要带命令行参数启动(也可以在VC中设),
第一个参数是压缩文件包的名称,第二个是待压缩的文件路径。recursiveGetFileNames就是把那个路径下多级目录的全部文件找出来,然后一个一个的压缩。你说一个一个压缩不合适,可以把几个文件都读到缓冲区后一起压缩,只是太麻烦,而且如果是大文件,一次压一个文件也是不可能的,只能分成小块压缩。
匿名用户
推荐于2016-03-09
展开全部
文件名,压缩后的长度,压缩的内容。
仅是示例而已,根据自己的需要修改
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
翟郁骆灵珊
2019-09-13 · TA获得超过1061个赞
知道小有建树答主
回答量:1199
采纳率:100%
帮助的人:5.1万
展开全部
就是用findfirst,
findnext找到所有目录下的文件,逐个压缩就是了。我把这两步分开来写的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式