美文网首页
zlib - compress函数

zlib - compress函数

作者: 微雨旧时歌丶 | 来源:发表于2018-01-05 10:30 被阅读0次
/*
× zlib库compress和uncompress函数的使用,
× 程序可以独立完成简单的文件压缩功能
××××××××××××××××××××××××××××××××××××××××××××××××
× 用法:z_compress 源文件名 目标文件名××××××××××
××××××××××××××××××××××××××××××××××××××××××××××××
× 目标文件:源文件长度+压缩后数据长度+压缩数据××
××××××××××××××××××××××××××××××××××××××××××××××××
*/

#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>

int main(int argc,char* argv[]) {
    FILE* file;
    uLong flen;
    unsigned char* fbuf = NULL;
    uLong clen;
    unsigned char* cbuf = NULL;

    //通过命令行参数将srcfile文件的数据压缩后存放在dstfile文件中
    if(argc<3) {
        printf("Usage: z_compress srcfile dstfile\n");
        return -1;
    }

    if ( (file = fopen(argv[1],"rb")) ==NULL) {
        printf("Can\'t open %s!\n",argv[1]);
        return -1;
    }

    // 装载源文件数据到缓冲区
    fseek(file,0L,SEEK_END); // 跳到文件尾
    flen = ftell(file); // 获取文件长度
    fseek(file,0L,SEEK_SET);

    if ( (fbuf=(unsigned char*)malloc(sizeof(unsigned char)* flen))==NULL) {
        printf("No enough memory!\n");
        fclose(file);
        return -1;
    }
    fread(fbuf, sizeof(unsigned char),flen,file);

    // 压缩数据
    clen = compressBound(flen);
    if ( (cbuf=(unsigned char*)malloc(sizeof(unsigned char)* clen))==NULL) {
        printf("No enough memory!\n");
        fclose(file);
        return -1;
    }
    if (compress(cbuf,&clen,fbuf,flen)!=Z_OK) {
        printf("Compress %s failed!\n",argv[1]);
        return -1;
    }
    fclose(file);

    if ((file = fopen(argv[2],"wb")) ==NULL) {
        printf("Can\'t create %s!\n",argv[2]);
        return -1;
    }

    // 保存压缩后的数据到目标文件
    fwrite(&flen, sizeof(uLong),1,file); // 写入源文件长度
    fwrite(&clen,sizeof(uLong),1,file); //写入目标数据长度
    fwrite(cbuf,sizeof(unsigned char),clen,file);
    fclose(file);

    free(fbuf);
    free(cbuf);

    return 0;
}

相关文章

网友评论

      本文标题:zlib - compress函数

      本文链接:https://www.haomeiwen.com/subject/hzujnxtx.html