2016-02-11 38 views
0

我試圖使用C++和libzip庫解壓縮可執行文件。與上一個類似的問題羅德里戈答案開始,我來此示例代碼:使用libzip將文件解壓縮到磁盤

#include <zip.h> 
int main() 
{ 
    //Open the ZIP archive 
    int err = 0; 
    zip *z = zip_open("foo.zip", 0, &err); 

    //Search for the file of given name 
    const char *name = "file.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, "file.txt", 0); 
    zip_fread(f, contents, st.size); 
    zip_fclose(f); 

    //And close the archive 
    zip_close(z); 
} 

從我個人理解,這個代碼不工作要解壓的文件,但我不知道如何寫文件該磁盤,就像提取一個zip文件一樣,使用像winzip這樣的工具。在內存中解壓縮數據並不能幫助我,但我一直無法弄清楚如何將文件實際存入磁盤。

+0

*您已經得到*出該文件存檔的,現在你只需要它寫入磁盤。 –

回答

0

像這樣的東西應該這樣做:

if(!std::ofstream("file1.txt").write(contents, st.size)) 
{ 
    std::cerr << "Error writing file" << '\n'; 
    return EXIT_FAILURE; 
} 

查找std::ofstream

當然,你應該檢查所有zip文件功能,看看他們是否繼續之前返回的錯誤。

相關問題