C/C++ 文件压缩解压缩

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


//压缩文件
int compressFile(const char* source, const char* destination) {
    if(source == NULL || destination == NULL)
        return -1;

    FILE* sourceFile = fopen(source, "rb");
    if (!sourceFile) {
        perror("fopen");
        return -1;
    }

    FILE* destFile = fopen(destination, "wb");
    if (!destFile) {
        perror("fopen");
        fclose(sourceFile);
        return -1;
    }

    const int bufferSize = 128 * 1024;
    char buffer[bufferSize] = {0};

    z_stream stream;
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;

    if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
        perror("deflateInit");
        fclose(sourceFile);
        fclose(destFile);
        return -1;
    }

    stream.next_out = buffer;
    stream.avail_out = bufferSize;

    int status;
    do {
        stream.next_in = buffer;
        stream.avail_in = fread(buffer, 1, bufferSize, sourceFile);

        if (ferror(sourceFile)) {
            perror("fread");
            deflateEnd(&stream);
            fclose(sourceFile);
            fclose(destFile);
            return -1;
        }

        status = deflate(&stream, feof(sourceFile) ? Z_FINISH : Z_NO_FLUSH);

        if (fwrite(buffer, 1, bufferSize - stream.avail_out, destFile) != bufferSize - stream.avail_out ||
            ferror(destFile)) {
            perror("fwrite");
            deflateEnd(&stream);
            fclose(sourceFile);
            fclose(destFile);
            return -1;
        }
    } while (status != Z_STREAM_END);

    deflateEnd(&stream);
    fclose(sourceFile);
    fclose(destFile);

    return 0;
}



//解压文件
int decompressFile(const char* source, const char* destination) {
    FILE* sourceFile = fopen(source, "rb");
    if (!sourceFile) {
        perror("fopen");
        return -1;
    }

    FILE* destFile = fopen(destination, "wb");
    if (!destFile) {
        perror("fopen");
        fclose(sourceFile);
        return -1;
    }

    const int bufferSize = 128 * 1024;
    char buffer[bufferSize];

    z_stream stream;
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;

    if (inflateInit(&stream) != Z_OK) {
        perror("inflateInit");
        fclose(sourceFile);
        fclose(destFile);
        return -1;
    }

    stream.next_out = buffer;
    stream.avail_out = bufferSize;

    int status;
    do {
        stream.next_in = buffer;
        stream.avail_in = fread(buffer, 1, bufferSize, sourceFile);

        if (ferror(sourceFile)) {
            perror("fread");
            inflateEnd(&stream);
            fclose(sourceFile);
            fclose(destFile);
            return -1;
        }

        if (stream.avail_in == 0)
            break;

        status = inflate(&stream, Z_NO_FLUSH);

        if (fwrite(buffer, 1, bufferSize - stream.avail_out, destFile) != bufferSize - stream.avail_out ||
            ferror(destFile)) {
            perror("fwrite");
            inflateEnd(&stream);
            fclose(sourceFile);
            fclose(destFile);
            return -1;
        }
    } while (status != Z_STREAM_END);

    inflateEnd(&stream);
    fclose(sourceFile);
    fclose(destFile);

    return 0;
}


int main() {
    const char* sourceFile = "source.txt";
    const char* compressedFile = "compressed.gz";
    const char* decompressedFile = "decompressed.txt";

    // 压缩文件
    if (compressFile(sourceFile, compressedFile) != 0) {
        fprintf(stderr, "压缩文件失败\n");
        return -1;
    }

    // 解压缩文件
    if (decompressFile(compressedFile, decompressedFile) != 0) {
        fprintf(stderr, "解压缩文件失败\n");
        return -1;
    }

    printf("文件压缩和解压缩成功\n");
    return 0;
}

https://blog.csdn.net/qq_41453285/article/details/106688596

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值