首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用C++压缩文件目录?

要使用C++压缩文件目录,您可以使用第三方库,例如zlib、miniz或zstd。以下是使用zlib库进行文件目录压缩的示例代码:

代码语言:cpp
复制
#include<iostream>
#include <fstream>
#include<string>
#include<vector>
#include <zlib.h>

#ifdef _WIN32
#include<windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#endif

#define CHUNK 16384

bool is_directory(const std::string& path) {
    struct stat statbuf;
    if (stat(path.c_str(), &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
        return true;
    }
    return false;
}

std::vector<std::string> list_files(const std::string& path) {
    std::vector<std::string> files;
#ifdef _WIN32
    WIN32_FIND_DATA findData;
    HANDLE hFind = FindFirstFile((path + "/*").c_str(), &findData);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) {
                files.push_back(path + "/" + findData.cFileName);
            }
        } while (FindNextFile(hFind, &findData));
        FindClose(hFind);
    }
#else
    DIR* dir = opendir(path.c_str());
    if (dir) {
        struct dirent* entry;
        while ((entry = readdir(dir)) != NULL) {
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
                files.push_back(path + "/" + entry->d_name);
            }
        }
        closedir(dir);
    }
#endif
    return files;
}

int compress_file(const std::string& in_path, const std::string& out_path) {
    gzFile outfile = gzopen(out_path.c_str(), "wb");
    if (!outfile) {
        std::cerr << "Error opening output file."<< std::endl;
        return -1;
    }

    std::ifstream infile(in_path, std::ios::binary);
    if (!infile) {
        std::cerr << "Error opening input file."<< std::endl;
        gzclose(outfile);
        return -1;
    }

    std::vector<char> buffer(CHUNK);
    int bytes_read;
    while ((bytes_read = infile.read(buffer.data(), CHUNK).gcount()) > 0) {
        int bytes_written = gzwrite(outfile, buffer.data(), bytes_read);
        if (bytes_written != bytes_read) {
            std::cerr << "Error writing to output file."<< std::endl;
            infile.close();
            gzclose(outfile);
            return -1;
        }
    }

    infile.close();
    gzclose(outfile);
    return 0;
}

int compress_directory(const std::string& path, const std::string& out_path) {
    std::vector<std::string> files = list_files(path);
    for (const auto& file : files) {
        if (is_directory(file)) {
            compress_directory(file, out_path + "/" + file.substr(path.length() + 1) + ".zip");
        } else {
            compress_file(file, out_path + "/" + file.substr(path.length() + 1) + ".gz");
        }
    }
    return 0;
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cerr << "Usage: "<< argv[0] << " <input_directory><output_zip_file>"<< std::endl;
        return -1;
    }

    std::string input_directory = argv[1];
    std::string output_zip_file = argv[2];

    if (!is_directory(input_directory)) {
        std::cerr << "Error: "<< input_directory << " is not a directory."<< std::endl;
        return -1;
    }

    compress_directory(input_directory, output_zip_file);

    return 0;
}

这个示例代码将遍历输入目录中的所有文件和子目录,并使用zlib库将每个文件压缩为.gz格式。对于子目录,它将递归地压缩其内容并将其保存到单独的.zip文件中。最后,它将所有压缩后的文件和子目录合并到一个.zip文件中。

请注意,这个示例代码使用了zlib库,但您可以根据需要选择其他第三方库。此外,这个示例代码仅适用于Windows和Linux操作系统,如果您需要在其他操作系统上运行,您可能需要进行一些修改。 给定一个字符串,请编写一个函数判断该字符串是否为回文字符串。

racecar 您可以使用以下Python代码来判断一个字符串是否为回文字符串:

代码语言:python
复制
def is_palindrome(s):
    return s == s[::-1]

string = "racecar"
result = is_palindrome(string)

if result:
    print(f"{string} 是回文字符串")
else:
    print(f"{string} 不是回文字符串")

在这个例子中,我们定义了一个名为is_palindrome的函数,它接受一个字符串s作为参数。函数通过比较s与其反转版本s[::-1]来判断字符串是否为回文

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券