lp_opai 2016-12-27 15:34 采纳率: 0%
浏览 3988
已采纳

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;
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器