码迷,mamicode.com
首页 > 编程语言 > 详细

C语言压缩/解压缩

时间:2015-06-24 00:27:02      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

一、简介

Lzlib 压缩库提供了在内存中的 LZMA 压缩和解压算法功能,包括对数据进行完整性检查。压缩格式是 lzip

 

二、安装

http://www.educity.cn/linux/1577732.html

 

三、实例

参考:

 

compression.c

#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: zcdemo 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;
}

decompression.c

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

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

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

    if((file = fopen(argv[1], "rb")) == NULL)
    {
        printf("Can\‘t open %s!\n", argv[1]);
        return -1;
    }
    /* 装载源文件数据到缓冲区 */
    fread(&ulen, sizeof(uLong), 1, file);    /* 获取缓冲区大小 */
    fread(&flen, sizeof(uLong), 1, file);    /* 获取数据流大小 */
    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);
    /* 解压缩数据 */
    if((ubuf = (unsigned char*)malloc(sizeof(unsigned char) * ulen)) == NULL)
    {
        printf("No enough memory!\n");
        fclose(file);
        return -1;
    }
    if(uncompress(ubuf, &ulen, fbuf, flen) != Z_OK)
    {
        printf("Uncompress %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(ubuf, sizeof(unsigned char), ulen, file);
    fclose(file);

    free(fbuf);
    free(ubuf);

    return 0;
}

编译

gcc -g -o compression compression.c -lz
gcc -g -o decompression decompression.c -lz

运行

技术分享

C语言压缩/解压缩

标签:

原文地址:http://www.cnblogs.com/274914765qq/p/4596496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!