程序功能:
1、将指定文件压缩成指定压缩包文件
2、从指定压缩文件读取内容。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <zlib.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
// gzip格式
// (u16)MAGIC + (u8)DEFLATE + (u8)flag + (u32)time + (u8)deflate_flag + (u8)OS_CODE
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("usage : %s src_file dst_file\n", argv[1]);
return -1;
}
#if 0
string out;
//open .gz file
gzFile gzfp = gzopen(argv[1],"rb");
if(!gzfp)
{
return false;
}
//read and add it to out
unsigned char buf[1024];
int have;
while( (have = gzread(gzfp,buf,1024)) > 0)
{
out.append((const char*)buf,have);
}
//close .gz file
gzclose(gzfp);
cout<<out<<endl;
#endif
stringstream out;
ifstream file;
file.open(argv[1]);
if(file.is_open())
{
//file>>out;
out<<file.rdbuf();
}
cout<<out<<endl;
file.close();
string str = out.str();
cout<<str<<endl;
gzFile gzfp = gzopen(argv[2],"wb");
if(!gzfp)
{
return false;
}
//read and add it to out
unsigned char buf[1024];
int have;
if( gzwrite(gzfp,str.c_str(),str.size()) > 0)
{
cout<<"ok"<<endl;
}
//close .gz file
gzclose(gzfp);
return 0;
}