lp_opai
2016-12-27 15:34
采纳率: 16.7%
浏览 3.7k

c++怎么解压文件 tar.zip gz格式等

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;
    
    
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2016-12-27 15:50
    评论
    解决 无用
    打赏 举报
  • liupan6889 2016-12-28 01:32

    直接用命令啊,tar xzvf xxx.tar.gz

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题