我已將使用7-zip選項壓縮的 zip 檔案上傳到此 GitHub存盤庫add to .zip
中,其中僅包含一個名稱為該檔案的檔案,如何在不將其寫入磁盤的情況下讀取檔案的內容?text.txt
text.txt
我正在使用以下方法將 zip 下載到記憶體中curl
:
#include <curl/curl.h>
static size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) {
size_t realsize = size * nmemb;
auto& mem = *static_cast<std::string*>(userp);
mem.append(static_cast<char*>(contents), realsize);
return realsize;
}
std::string Download(const std::string& url)
{
CURL* curl_handle;
CURLcode res;
std::string chunk;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
// added options that may be required
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); // redirects
curl_easy_setopt(curl_handle, CURLOPT_HTTPPROXYTUNNEL, 1L); // corp. proxies etc.
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); // we want it all
// curl_easy_setopt(curl_handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
res = curl_easy_perform(curl_handle);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << '\n';
} else {
std::cout << chunk.size() << " bytes retrieved\n";
}
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return chunk;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::string link = "https://github.com/R3uan3/test/raw/main/text.zip";
auto data = Download(link);
}
在搜索lib
能夠解壓縮記憶體中的 zip 的程序中,我發現了這個:libzip(歡迎使用任何 lib)。
搜索示例我找到了這個答案,但是他正在從記憶體中加載一個 zipdisk
并閱讀它的內容。
我怎么能讀取字串上的zip
下載內容?curl
data
當我data
在它顯示的除錯器中可視化的內容時PK
,我嘗試將其傳遞給zip *z
,但z
回傳null
//Open the ZIP archive
int err = 0;
zip *z = zip_open(data.c_str(), 0, &err);
//Search for the file of given name
const char *name = "text.txt";
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, name, 0, &st);
//Alloc memory for its uncompressed contents
char *contents = new char[st.size];
//Read the compressed file
zip_file *f = zip_fopen(z, name, 0);
zip_fread(f, contents, st.size);
zip_fclose(f);
uj5u.com熱心網友回復:
我無視一切卷曲在問題中,因為我們已經驗證您已將zip
檔案正確存盤在記憶體中。
我怎樣才能讀取字串資料上的 zip ...?
既然你擁有整個壓縮存盤在記憶體中的檔案,您需要創建一個zip_source
fromchunk.data()
并使用它打開存檔zip_source
- 然后打開存檔中的各個檔案。
下面是方法(沒有錯誤檢查 - 你需要添加它):
{
// ...
zip_error_t ze; // for errors
// create a zip_source from the data you've stored in memory
zip_source_t* zs = zip_source_buffer_create(chunk.data(), chunk.size(), 0, &ze);
// open the archive from the zip_source
zip_t* zip = zip_open_from_source(zs, ZIP_CHECKCONS | ZIP_RDONLY, &ze);
// read how many files you've got in there
zip_int64_t entries = zip_get_num_entries(zip, 0);
std::cout << entries << '\n';
// loop over the entries in the archive
for(zip_int64_t idx = 0; idx < entries; idx) {
std::cout << zip_get_name(zip, idx, ZIP_FL_ENC_STRICT) << '\n';
// open the file at this index
zip_file_t* fp = zip_fopen_index(zip, idx, 0);
// process the file
zip_int64_t len;
char buf[1024];
while((len = zip_fread(fp, buf, sizeof buf)) > 0) {
std::cout << "read " << len << " bytes\n";
// do something with the `len` bytes you have in `buf`
}
zip_fclose(fp); // close this file
}
zip_close(zip); // close the whole archive
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/507050.html
上一篇:使用intellij、kotlin多平臺和gradle時找不到Android平臺
下一篇:如何在容器內安裝curl?