zip压缩字节流 c语言,zip学习笔记 —— 用C编写的简单压缩库

一、简介

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

二、移植

将 miniz.h、 zip.c、 zip.h 加入工程中

29ffe9a85c9c

三、示例

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 日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值