linux

Linux readdir如何实现文件压缩与解压

小樊
51
2025-05-18 19:45:53
栏目: 智能运维

在Linux中,readdir函数用于读取目录中的文件和子目录。它本身并不直接支持文件的压缩与解压。但是,你可以结合其他库和工具来实现这一功能。

以下是一个简单的示例,展示如何使用C语言和zlib库来实现文件的压缩与解压。首先确保已经安装了zlib库。

压缩文件

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

int compress_file(const char *input_filename, const char *output_filename) {
    FILE *input_file = fopen(input_filename, "rb");
    if (!input_file) {
        perror("无法打开输入文件");
        return -1;
    }

    FILE *output_file = fopen(output_filename, "wb");
    if (!output_file) {
        perror("无法打开输出文件");
        fclose(input_file);
        return -1;
    }

    int ret, flush;
    unsigned have;
    z_stream strm;
    unsigned char in[4096];
    unsigned char out[4096];

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
    if (ret != Z_OK) {
        fprintf(stderr, "deflateInit错误 %d\n", ret);
        fclose(input_file);
        fclose(output_file);
        return -1;
    }

    do {
        strm.avail_in = fread(in, 1, sizeof(in), input_file);
        if (ferror(input_file)) {
            (void)deflateEnd(&strm);
            fclose(input_file);
            fclose(output_file);
            return -1;
        }
        flush = feof(input_file) ? Z_FINISH : Z_NO_FLUSH;
        strm.next_in = in;

        do {
            strm.avail_out = sizeof(out);
            strm.next_out = out;
            ret = deflate(&strm, flush);
            have = sizeof(out) - strm.avail_out;
            if (fwrite(out, 1, have, output_file) != have || ferror(output_file)) {
                (void)deflateEnd(&strm);
                fclose(input_file);
                fclose(output_file);
                return -1;
            }
        } while (strm.avail_out == 0);
    } while (flush != Z_FINISH);

    (void)deflateEnd(&strm);
    fclose(input_file);
    fclose(output_file);
    return 0;
}

解压文件

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

int decompress_file(const char *input_filename, const char *output_filename) {
    FILE *input_file = fopen(input_filename, "rb");
    if (!input_file) {
        perror("无法打开输入文件");
        return -1;
    }

    FILE *output_file = fopen(output_filename, "wb");
    if (!output_file) {
        perror("无法打开输出文件");
        fclose(input_file);
        return -1;
    }

    int ret, flush;
    unsigned have;
    z_stream strm;
    unsigned char in[4096];
    unsigned char out[4096];

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    ret = inflateInit(&strm);
    if (ret != Z_OK) {
        fprintf(stderr, "inflateInit错误 %d\n", ret);
        fclose(input_file);
        fclose(output_file);
        return -1;
    }

    do {
        strm.avail_in = fread(in, 1, sizeof(in), input_file);
        if (ferror(input_file)) {
            (void)inflateEnd(&strm);
            fclose(input_file);
            fclose(output_file);
            return -1;
        }
        flush = feof(input_file) ? Z_FINISH : Z_NO_FLUSH;
        strm.next_in = in;

        do {
            strm.avail_out = sizeof(out);
            strm.next_out = out;
            ret = inflate(&strm, flush);
            have = sizeof(out) - strm.avail_out;
            if (fwrite(out, 1, have, output_file) != have || ferror(output_file)) {
                (void)inflateEnd(&strm);
                fclose(input_file);
                fclose(output_file);
                return -1;
            }
        } while (strm.avail_out == 0);
    } while (flush != Z_FINISH);

    (void)inflateEnd(&strm);
    fclose(input_file);
    fclose(output_file);
    return 0;
}

这两个函数分别用于压缩和解压文件。你可以根据需要调用它们。注意,这里没有涉及到readdir函数,因为它是用于读取目录内容的,而不是处理文件压缩和解压。如果你需要在遍历目录的同时压缩或解压文件,可以在遍历过程中调用这两个函数。

0
看了该问题的人还看了