2017-02-01 33 views
0

我正在使用標頭zip.hpp(可以在這裏找到https://markand.bitbucket.io/libzip/index.html或這裏http://hg.markand.fr/libzip/file)壓縮一些文件。Cpp強制使用libzip編寫zip

之後要刪除原始文件,我使用remove("myfile.txt")

顯然,zip.hpp在運行結束時將文件壓縮,因此它找不到該文件並且不創建zip文件夾。如果我遺漏了remove("myfile.txt"),一切正常,除了我有幾個文件飛來飛去,我只想在他們的壓縮形式。

您對如何強制libzip寫入zip文件有任何想法嗎? 如果我刪除了archive -instance我所期望的,它應該強制創建,但顯然libzip::Archive -class沒有析構函數(至少我無法找到一個和delete archive拋出許多錯誤)

我基本的代碼如下所示:

#include <fstream> 
#include <zip.h> 
#include "lib/zip.hpp" 


int main() { 

    libzip::Archive archive("output.zip", ZIP_CREATE); 
    std::ofstream outfile ("myfile.txt"); 
    outfile << "Hello World\n"; 
    outfile.close(); 

    archive.add(libzip::source::file("myfile.txt"), "myfile2.txt"); 

    // delete archive; // throws an error... 

    // remove("myfile.txt"); 
    // if left out, output.zip gets created otherwise nothing is created 


    return 0; 

} 

回答

1

libzip::Archive當它超出範圍會寫的內容。因此,您只需在刪除文件之前引入一個附加範圍。

#include <fstream> 
#include <zip.h> 
#include "lib/zip.hpp" 

int main() { 

    { // Additional scope 
     libzip::Archive archive("output.zip", ZIP_CREATE); 
     std::ofstream outfile ("myfile.txt"); 
     outfile << "Hello World\n"; 
     outfile.close(); 
     archive.add(libzip::source::file("myfile.txt"), "myfile2.txt"); 
    } // Archive is written now. 

    remove("myfile.txt"); 

    return 0; 
} 
+0

多數民衆贊成在輝煌,爲什麼我沒有想到的範圍?非常感謝! – David