您好,登錄后才能下訂單哦!
在C++中處理ZIP文件,可以使用一些第三方庫,如minizip和zlib。這些庫可以幫助你輕松地讀取、寫入和操作ZIP文件。下面是一些使用這些庫處理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;
}
#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;
}
#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庫壓縮和解壓縮數據。你可以根據自己的需求對這些示例進行修改和擴展。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。