温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++处理ZIP文件技巧

发布时间:2024-10-30 10:42:37 来源:亿速云 阅读:79 作者:小樊 栏目:编程语言

在C++中处理ZIP文件,可以使用一些第三方库,如minizipzlib。这些库可以帮助你轻松地读取、写入和操作ZIP文件。下面是一些使用这些库处理ZIP文件的技巧:

  1. 使用minizip库读取ZIP文件:
#include <iostream>
#include <zip.h>
#include <unzip.h>
#include <vector>

int main() {
    unzFile uf = unzOpen("example.zip");
    if (uf == NULL) {
        std::cerr << "Error opening ZIP file" << std::endl;
        return 1;
    }

    unz_global_info gi;
    if (unzGetGlobalInfo(uf, &gi) != UNZ_OK) {
        std::cerr << "Error getting global info" << std::endl;
        unzClose(uf);
        return 1;
    }

    std::vector<char> buf(gi.total_out);
    unz_file_info fi;
    int ret;

    for (unsigned int i = 0; i < gi.num_files; ++i) {
        ret = unzGetCurrentFileInfo(uf, &fi, NULL, 0, NULL, 0, NULL, 0);
        if (ret != UNZ_OK) {
            std::cerr << "Error getting file info" << std::endl;
            continue;
        }

        std::vector<char> filename(fi.filename, fi.filename + fi.filename_length);
        std::vector<char> file_content(buf.size());

        ret = unzOpenCurrentFile(uf, &file_content[0]);
        if (ret != UNZ_OK) {
            std::cerr << "Error opening current file" << std::endl;
            continue;
        }

        unzReadCurrentFile(uf, &file_content[0], file_content.size());
        unzCloseCurrentFile(uf);

        std::cout << "Filename: " << filename << std::endl;
        std::cout << "Size: " << fi.uncompressed_size << std::endl;
        std::cout << "Content: " << std::string(file_content.begin(), file_content.end()) << std::endl;
    }

    unzClose(uf);
    return 0;
}
  1. 使用minizip库向ZIP文件添加文件:
#include <iostream>
#include <zip.h>
#include <unzip.h>
#include <vector>

int main() {
    unzFile uf = unzOpen("example.zip");
    if (uf == NULL) {
        std::cerr << "Error opening ZIP file" << std::endl;
        return 1;
    }

    int ret = unzGoToFirstFile(uf);
    if (ret != UNZ_OK) {
        std::cerr << "Error going to first file" << std::endl;
        unzClose(uf);
        return 1;
    }

    const char* filename = "newfile.txt";
    const char* content = "This is the content of the new file.";
    size_t content_size = strlen(content);

    unz_file_info fi;
    ret = unzGetCurrentFileInfo(uf, &fi, NULL, 0, NULL, 0, NULL, 0);
    if (ret != UNZ_OK) {
        std::cerr << "Error getting current file info" << std::endl;
        unzClose(uf);
        return 1;
    }

    unz_file_info new_fi;
    ret = unzOpenCurrentFile(uf, &new_fi);
    if (ret != UNZ_OK) {
        std::cerr << "Error opening current file" << std::endl;
        unzClose(uf);
        return 1;
    }

    ret = unzWriteCurrentFile(uf, content, content_size);
    if (ret != UNZ_OK) {
        std::cerr << "Error writing current file" << std::endl;
        unzCloseCurrentFile(uf);
        unzClose(uf);
        return 1;
    }

    unzCloseCurrentFile(uf);
    unzClose(uf);
    return 0;
}
  1. 使用zlib库压缩和解压缩数据:
#include <iostream>
#include <vector>
#include <zlib.h>

std::vector<char> compressData(const std::vector<char>& data) {
    z_stream zs;
    zs.zalloc = Z_NULL;
    zs.zfree = Z_NULL;
    zs.opaque = Z_NULL;
    zs.avail_in = data.size();
    zs.next_in = reinterpret_cast<Bytef*>(data.data());

    int ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
    if (ret != Z_OK) {
        std::cerr << "Error initializing compression" << std::endl;
        return {};
    }

    std::vector<char> compressed_data;
    compressed_data.reserve(data.size() * 2);

    do {
        zs.avail_out = compressed_data.size();
        zs.next_out = reinterpret_cast<Bytef*>(compressed_data.data() + compressed_data.size());

        ret = deflate(&zs, Z_FINISH);
        if (ret != Z_STREAM_END && ret != Z_OK) {
            std::cerr << "Error compressing data" << std::endl;
            deflateEnd(&zs);
            return {};
        }

        compressed_data.resize(compressed_data.size() + zs.avail_out);
    } while (zs.avail_out == 0);

    deflateEnd(&zs);
    return compressed_data;
}

std::vector<char> decompressData(const std::vector<char>& data) {
    z_stream zs;
    zs.zalloc = Z_NULL;
    zs.zfree = Z_NULL;
    zs.opaque = Z_NULL;
    zs.avail_in = data.size();
    zs.next_in = reinterpret_cast<Bytef*>(data.data());

    int ret = inflateInit(&zs);
    if (ret != Z_OK) {
        std::cerr << "Error initializing decompression" << std::endl;
        return {};
    }

    std::vector<char> decompressed_data;
    decompressed_data.reserve(data.size() * 2);

    do {
        zs.avail_out = decompressed_data.size();
        zs.next_out = reinterpret_cast<Bytef*>(decompressed_data.data() + decompressed_data.size());

        ret = inflate(&zs, Z_NO_FLUSH);
        if (ret != Z_STREAM_END && ret != Z_OK) {
            std::cerr << "Error decompressing data" << std::endl;
            inflateEnd(&zs);
            return {};
        }

        decompressed_data.resize(decompressed_data.size() + zs.avail_out);
    } while (zs.avail_out == 0);

    inflateEnd(&zs);
    return decompressed_data;
}

这些示例展示了如何使用minizip库读取ZIP文件、向ZIP文件添加文件以及使用zlib库压缩和解压缩数据。你可以根据自己的需求对这些示例进行修改和扩展。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI