您好,登錄后才能下訂單哦!
在C++中,解壓庫通常指的是用于處理壓縮文件的庫,如zlib、libzip、minizip等。以下是這些庫的配置指南:
vcpkg install zlib
sudo apt-get install zlib1g-dev
在C++項目中使用zlib,首先需要包含頭文件:
#include <zlib.h>
解壓示例代碼:
#include <iostream>
#include <fstream>
#include <vector>
#include <zlib.h>
int main() {
std::ifstream file("compressed.gz", std::ios::binary);
if (!file) {
std::cerr << "Cannot open compressed file" << std::endl;
return 1;
}
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
inflateInit(&zs);
std::vector<char> buffer(1024);
std::streamsize total = 0;
while (!file.eof()) {
zs.avail_in = file.readsome(buffer.data(), buffer.size());
if (zs.avail_in == 0) break;
zs.next_in = reinterpret_cast<Bytef*>(buffer.data());
do {
zs.avail_out = buffer.size();
zs.next_out = reinterpret_cast<Bytef*>(buffer.data());
inflate(&zs, Z_NO_FLUSH);
total += zs.avail_out;
} while (zs.avail_out == 0);
file.seekg(zs.total_out - zs.avail_out, std::ios::beg);
}
inflateEnd(&zs);
file.close();
std::cout << "Decompressed size: " << total << std::endl;
return 0;
}
vcpkg install libzip
sudo apt-get install libzip-dev
在C++項目中使用libzip,首先需要包含頭文件:
#include <zip.h>
解壓示例代碼:
#include <iostream>
#include <fstream>
#include <vector>
#include <zip.h>
int main() {
zip_t *zip = zip_open("compressed.zip", 0);
if (!zip) {
std::cerr << "Cannot open zip file" << std::endl;
return 1;
}
int num_entries = zip_get_num_entries(zip, 0);
for (int i = 0; i < num_entries; ++i) {
const char *entry_name = zip_get_name(zip, i, 0);
zip_file_t *file = zip_fopen(zip, entry_name, 0);
if (!file) {
std::cerr << "Cannot open entry: " << entry_name << std::endl;
continue;
}
std::vector<char> buffer(1024);
zip_int64_t size = zip_fread(file, buffer.data(), buffer.size());
if (size > 0) {
std::ofstream out(entry_name, std::ios::binary);
out.write(buffer.data(), size);
out.close();
}
zip_fclose(file);
}
zip_close(zip);
return 0;
}
vcpkg install minizip
sudo apt-get install libminizip-dev
在C++項目中使用minizip,首先需要包含頭文件:
#include "unzip.h"
解壓示例代碼:
#include <iostream>
#include <fstream>
#include <vector>
#include "unzip.h"
int main() {
unzFile uf = unzOpen("compressed.zip");
if (!uf) {
std::cerr << "Cannot open zip file" << std::endl;
return 1;
}
unz_global_info gi;
if (unzGetGlobalInfo(uf, &gi) != UNZ_OK) {
std::cerr << "Cannot get global info" << std::endl;
unzClose(uf);
return 1;
}
for (unsigned int i = 0; i < gi.number_of_files; ++i) {
unz_file_info fi;
if (unzGetCurrentFileInfo(uf, &fi, nullptr, 0, nullptr, 0, nullptr, 0) != UNZ_OK) {
std::cerr << "Cannot get file info" << std::endl;
continue;
}
std::vector<char> buffer(fi.uncompressed_size);
if (unzReadCurrentFile(uf, buffer.data(), fi.uncompressed_size) != UNZ_OK) {
std::cerr << "Cannot read file" << std::endl;
continue;
}
std::ofstream out(fi.filename, std::ios::binary);
out.write(buffer.data(), fi.uncompressed_size);
out.close();
}
unzClose(uf);
return 0;
}
以上是zlib、libzip和minizip三個解壓庫的安裝和使用示例。選擇哪個庫取決于你的具體需求和環境。希望這些指南對你有所幫助!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。