c++怎么解压文件 tar.zip gz格式等 ( linux ) 能举个小例子吗 (zlib库?)
3条回答 默认 最新
oyljerry 2016-12-28 02:01关注#include <string> #include <time.h> #include <stdio.h> #include <iostream> #include <string.h> #include "zlib.h" int main() { unsigned long int fileLength; unsigned long int readLength; unsigned long int compressBufLength; unsigned long int uncompressLength; unsigned long int offset; unsigned char *readBuf = new unsigned char[512];//the readbuf of the exist file content unsigned char *compressBuf = new unsigned char[512];//the compress buffer unsigned char *uncompressBuf = new unsigned char[512];//the uncompress content buffer FILE *originalFile = fopen("/lgw150/temp/src/lg4/original.lg4","a+");//the exist file FILE *compressedFile = fopen("/lgw150/temp/src/lg4/compressed.lg4","a+");//compressfile FILE *uncompressFile = fopen("/lgw150/temp/src/lg4/uncompressed.lg4","a+");// fseek(originalFile,0,2); fileLength = ftell(originalFile); offset = 0;// while(offset <fileLength)// { printf("offset=%lu;fileLength=%lu\n",offset,fileLength); memset(readBuf,0,512); memset(compressBuf,0,512); memset(uncompressBuf,0,512); fseek(originalFile,offset,0);// readLength = fread(readBuf,sizeof(char),512,originalFile); offset += readLength;// int compressValue = compress(compressBuf,&compressBufLength,readBuf,readLength); int fwriteValue = fwrite(compressBuf,sizeof(char),compressBufLength,compressedFile);// printf("compressValue = %d;fwriteLength = %d;compressBufLength=%lu;readLength = %lu\n",compressValue,fwriteValue,compressBufLength,readLength); int uncompressValue = uncompress(uncompressBuf,&uncompressLength,compressBuf,compressBufLength);// int fwriteValue2= fwrite(uncompressBuf,sizeof(char),uncompressLength,uncompressFile);// } fseek(originalFile,0,0); fseek(compressedFile,0,0); fseek(uncompressFile,0,0); if(originalFile != NULL) { fclose(originalFile); originalFile = NULL; } if(compressedFile != NULL) { fclose(compressedFile); compressedFile = NULL; } if(uncompressFile != NULL) { fclose(uncompressFile); uncompressFile = NULL; } delete[] readBuf; delete[] compressBuf; delete[] uncompressBuf; return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报