一个 very simple 的 libz Demo

#include <zlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
    gzFile fi = (gzFile)gzopen("file.gz","wb");
    gzwrite(fi,"my decompressed data",strlen("my decompressed data"));
    gzclose(fi);


    return true;
}

gcc -o test test.c -lz




文件压缩

#include <iostream>
#include <string>
#include <zlib.h>
#include <string.h>
#include <stdio.h>

using namespace std;

int compressFile(char *infilename, char *outfilename)
{
        //Opens the needed files
        FILE * infile = fopen(infilename, "rb");
        gzFile outfile = gzopen(outfilename, "wb");
        //Checks if the files are correctly opened
        if (!infile || !outfile) return -1;

        char inbuffer[128];
        int numRead = 0;

        while ((numRead = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0){
                gzwrite(outfile, inbuffer, numRead);

        }
        fclose(infile);
        gzclose(outfile);

}

int main()
{
    gzFile fi = (gzFile)gzopen("file.gz","wb");
    gzwrite(fi,"my decompressed data",strlen("my decompressed data"));
    gzclose(fi);

    compressFile("zip_3.0-8.dsc", "zip_3.0-8.dsc.gz");
    return true;
}





解压zip:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <zip.h>
const char *prg;

static void safe_create_dir(const char *dir)
{
    if (mkdir(dir, 0755) < 0) {
        if (errno != EEXIST) {
            perror(dir);
            exit(1);
        }
    }
}

int main(int argc, char *argv[])
{
    const char *archive;
    struct zip *za;
    struct zip_file *zf;
    struct zip_stat sb;
    char buf[100];
    int err;
    int i, len;
    int fd;
    long long sum;
    prg = argv[0];
    if (argc != 2) {
        fprintf(stderr, "usage: %s archive/n", prg);
        return 1;
    }

    archive = argv[1];
    if ((za = zip_open(archive, 0, &err)) == NULL) {
        zip_error_to_str(buf, sizeof(buf), err, errno);
        fprintf(stderr, "%s: can't open zip archive `%s': %s/n", prg,
            archive, buf);
        return 1;
    }

    for (i = 0; i < zip_get_num_entries(za, 0); i++) {
        if (zip_stat_index(za, i, 0, &sb) == 0) {
            printf("==================/n");
            len = strlen(sb.name);
            printf("Name: [%s], ", sb.name);
            printf("Size: [%llu], ", sb.size);
            printf("mtime: [%u]/n", (unsigned int)sb.mtime);
            if (sb.name[len - 1] == '/') {
                safe_create_dir(sb.name);
            } else {
                zf = zip_fopen_index(za, i, 0);
                if (!zf) {
                    fprintf(stderr, "boese, boese/n");
                    exit(100);
                }

                fd = open(sb.name, O_RDWR | O_TRUNC | O_CREAT, 0644);
                if (fd < 0) {
                    fprintf(stderr, "boese, boese/n");
                    exit(101);
                }

                sum = 0;
                while (sum != sb.size) {
                    len = zip_fread(zf, buf, 100);
                    if (len < 0) {
                        fprintf(stderr, "boese, boese\n");
                        exit(102);
                    }
                    write(fd, buf, len);
                    sum += len;
                }
                close(fd);
                zip_fclose(zf);
            }
        } else {
            printf("File[%s] Line[%d]/n", __FILE__, __LINE__);
        }
    }

    if (zip_close(za) == -1) {
        fprintf(stderr, "%s: can't close zip archive `%s'\n", prg, archive);
        return 1;
    }

    return 0;
}



还有 zpipe.c

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值