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

#include 
#include 
#include 
#include 


//压缩文件
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

你可能感兴趣的:(c语言,c++,java)