读书人

C++怎么实现对打包文件进行释放

发布时间: 2012-02-11 09:51:35 作者: rapoo

C++如何实现对打包文件进行释放
用C++实现一个简单的打包文件释放程序,要求具有可移植性,打包文件可以是任何格式(没具体规定)
要求能获知释放完成时间
要求能指定释放路径和指定打包文件路径
释放过程无界面
设计成一个类的形式
请各位大哥帮帮忙呀,第一次写一点都不懂呀........

[解决办法]
直接使用zlib就好了
[解决办法]
www.zlib.org上有完整的帮助
[解决办法]
//用zlib实现压缩与解压缩
//在winxpsp2+devc++4.9.9.2实现
//链接参数中加入-lz
//将zlib1.dll加入程序目录
//kzip功能是压缩文件,kunzip是解压缩文件
//仅限于对单个文件的压缩与解压缩
#i nclude <cstdlib>
#i nclude <iostream>
#i nclude <zlib.h>
#i nclude <stdlib.h>

using namespace std;
void kzip(char *inFile,char *outFile)
{
FILE *FileIn=fopen(inFile, "rb ");
FILE *FileOut=fopen(outFile, "wb ");
fseek(FileIn,0,SEEK_END);
unsigned long FileInSize=ftell(FileIn);
void *RawDataBuff=malloc(FileInSize);
void *CompDataBuff=NULL;
uLongf CompBuffSize=(uLongf)(FileInSize+(FileInSize*0.1)+12);
CompDataBuff=malloc((size_t)(CompBuffSize));
fseek(FileIn,0,SEEK_SET);
fread(RawDataBuff,FileInSize,1,FileIn);
uLongf DestBuffSize;
compress2((Bytef*)CompDataBuff,(uLongf*)&DestBuffSize,(const Bytef*)RawDataBuff,(uLongf)FileInSize,Z_BEST_COMPRESSION);
fwrite(CompDataBuff,DestBuffSize,1,FileOut);
}
void kunzip(char *inFile,char *outFile)
{
//the input file, this is the output file from part one
FILE *FileIn = fopen(inFile, "rb ");

//output file
FILE *FileOut = fopen(outFile, "wb ");

//get the file size of the input file
fseek(FileIn, 0, SEEK_END);
unsigned long FileInSize = ftell(FileIn);

//buffers for the raw and uncompressed data
void *RawDataBuff = malloc(FileInSize);
void *UnCompDataBuff = NULL;

//read in the contents of the file into the source buffer
fseek(FileIn, 0, SEEK_SET);
fread(RawDataBuff, FileInSize, 1, FileIn);
//allocate a buffer big enough to hold the uncompressed data, we can cheat here
//because we know the file size of the original
uLongf UnCompSize = 482000;
UnCompDataBuff = malloc(UnCompSize);


//all data we require is ready so compress it into the source buffer, the exact
//size will be stored in UnCompSize
uncompress((Bytef*)UnCompDataBuff, &UnCompSize, (const Bytef*)RawDataBuff, FileInSize);

//write the decompressed data to disk
fwrite(UnCompDataBuff, UnCompSize, 1, FileOut);
}
int main(int argc, char *argv[])
{
printf( "1、压缩\n2、解压缩\n ");
int n=getchar();
if (n==1)
{
kzip( "in.jpeg ", "out.dat ");
}
else
{
kunzip( "Out.dat ", "in.jpeg ");
}

system( "PAUSE ");
return EXIT_SUCCESS;
}

[解决办法]
【标 题】:实战-在win32环境下进行zlib编程1[原创]
【关键字】:win32,zlib
【来 源】:http://www.cublog.cn/u/15586/showart.php?id=174925
实战-在win32环境下进行zlib编程1[原创]

1、编程环境:winxp sp2+Dev-C++4.9.9.2
2\在devc++中下载更新zlib库
3\新建控制台应用程序.
4\在工程属性中设定其链接参数为-lz
5\编辑源程序如下,假设其工程文件名为ccc:
#i nclude <cstdlib>
#i nclude <iostream>
#i nclude <stdio.h>
#i nclude <zlib.h>

using namespace std;

int main(int argc,char **argv)
{
gzFile zip;
int c;
if (argc <2) return 0;
zip = gzopen(argv[1], "rb ");


while ((c=gzgetc(zip))!=EOF) putchar(c);
gzclose(zip);
return 1;
}
编译这段程序,可执行程序为ccc.exe
6\在这段源程序所实现的功能就是将压缩文档按字节一个个的读取并显示出来.
7\将devc++的bin目录下面的zlib1.dll拷贝到windows\system32目录下面,或是放到应用程序所在的目录.
8\下载一份gzip for windows,我所下载到的是1.3.5版本.
9\随便建立一个文件,如abc.txt
10\在gzip目录下面执行如下命令 gzip -9 <abc.txt> abc.txt.gz,相当于把abc.txt压缩成文件名为abc.txt.gz的文件了.
11\执行ccc abc.txt.gz,会看到已压缩文件abc.txt.gz中的内容了.

[解决办法]
rar是开源的啊
网站上就可以下到源代码
[解决办法]
用zlib的话,先用tar打包成一个文件,再压缩
rar本身支持处理多个文件以及目录

读书人网 >C++

热点推荐