zip学习笔记 —— 用C编写的简单压缩库

一、简介

基于 miniz 的用C语言编写的可移植的、简单的 zip 库。Miniz 是一个无损的、高性能的数据压缩库,位于一个源文件中。只需要简单的接口来附加缓冲区或文件到当前的 zip 条目。

二、移植

项目地址:https://github.com/kuba–/zip

miniz.hzip.czip.h 加入工程中

三、示例

3.1 创建一个具有默认压缩级别的新压缩存档

struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
{
    zip_entry_open(zip, "foo-1.txt");
    {
        const char *buf = "Some data here...\0";
        zip_entry_write(zip, buf, strlen(buf));
    }
    zip_entry_close(zip);

    zip_entry_open(zip, "foo-2.txt");
    {
        // merge 3 files into one entry and compress them on-the-fly.
        zip_entry_fwrite(zip, "foo-2.1.txt");
        zip_entry_fwrite(zip, "foo-2.2.txt");
        zip_entry_fwrite(zip, "foo-2.3.txt");
    }
    zip_entry_close(zip);
}
zip_close(zip);

3.2 解压缩到文件夹中

int on_extract_entry(const char *filename, void *arg) {
    static int i = 0;
    int n = *(int *)arg;
    printf("Extracted: %s (%d of %d)\n", filename, ++i, n);

    return 0;
}

int arg = 2;
zip_extract("foo.zip", "/tmp", on_extract_entry, &arg);

3.3 将压缩条目解压缩到内存中

void *buf = NULL;
size_t bufsize;

struct zip_t *zip = zip_open("foo.zip", 0, 'r');
{
    zip_entry_open(zip, "foo-1.txt");
    {
        zip_entry_read(zip, &buf, &bufsize);
    }
    zip_entry_close(zip);
}
zip_close(zip);

free(buf);

• 由 Leung 写于 2020 年 9 月 21 日

• 参考:https://github.com/kuba–/zip

要使用7-Zip中的lzma压缩大文件,您需要遵循以下步骤: 1. 下载7-Zip SDK并解压缩到您的计算机。 2. 使用以下代码包含必要的头文件: ```c #include "lzma/LzmaEnc.h" #include "lzma/LzmaDec.h" ``` 3. 创建一个LzmaEncHandle和LzmaEncProps结构体来设置压缩选项: ```c LzmaEncHandle enc = LzmaEnc_Create(&g_Alloc); LzmaEncProps props; LzmaEncProps_Init(&props); props.level = 5; // 压缩级别 props.dictSize = 1 << 26; // 字典大小 props.reduceSize = (1 << 30) - 1; // 压缩后文件大小 props.lp = 0; // 用于匹配模式的扩展字节数 props.pb = 2; // 用于匹配模式的字节数 props.fb = 128; // 用于匹配模式的字节数 props.numThreads = 2; // 使用的线程数 ``` 4. 使用LzmaEnc_SetProps函数将压缩选项应用于LzmaEncHandle: ```c LzmaEnc_SetProps(enc, &props); ``` 5. 打开要压缩的大文件,并为输出文件创建一个文件句柄: ```c FILE* inFile = fopen("input_file", "rb"); FILE* outFile = fopen("output_file.lzma", "wb"); ``` 6. 编写循环,将文件分段读取并压缩: ```c int inSize = 1 << 16; // 每次读取的输入文件大小 int outSize = 1 << 16; // 每次写入的输出文件大小 unsigned char* inBuf = (unsigned char*)malloc(inSize); unsigned char* outBuf = (unsigned char*)malloc(outSize); while (true) { // 读取输入文件 size_t bytesRead = fread(inBuf, 1, inSize, inFile); if (bytesRead == 0) { break; } // 压缩数据 size_t outPos = 0; size_t outLen = outSize; LzmaEnc_Encode(enc, outBuf, &outLen, inBuf, bytesRead, &g_Alloc, &g_Alloc); // 写入输出文件 fwrite(outBuf, 1, outLen, outFile); } ``` 7. 关闭文件句柄和LzmaEncHandle,并释放内存: ```c fclose(inFile); fclose(outFile); LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc); free(inBuf); free(outBuf); ``` 这样,您就可以使用7-Zip中的lzma压缩大文件了。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leung_ManWah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值