2015-04-06 230 views
0

我認爲最好將它分成兩個單獨的問題,以幫助新手找到答案。D:如何將文件添加到zip壓縮文件?

Previous question是關於從zip檔案中提取數據。但現在我需要任何示例來展示如何將數據添加到zip歸檔文件。

有2種情況。 1.獨立文件 2.測試字符串像:string foo = "qwerty"; 如何發送它的zip?

std.zip docx我找到了方法addMember(ArchiveMember de);,但我不明白如何將數據傳遞給它。

回答

0

顯然,新文檔沒有顯示此模塊的用法。我讀的來源和整理出來,這是爲了做到這一點:

import std.file: write; 
import std.string: representation; 
import std.zip: ArchiveMember, ZipArchive; 

void main() 
{ 
    char[] contents = "This is my test data.\n".dup; 

    // Create the archive. 
    auto zip = new ZipArchive(); 

    // Create the archive entry 
    ArchiveMember ae = new ArchiveMember(); 
    ae.name = "test.txt"; 
    ae.expandedData(contents.representation); 

    // Add to the entry to the archive. 
    zip.addMember(ae); 

    // Build the archive. 
    void[] compressed_data = zip.build(); 

    // Write the archive data to a file. 
    write("test.zip", compressed_data); 
} 

而且它的工作原理:

$ ./addzip 
$ unzip test.zip 
Archive: test.zip 
extracting: test.txt     
$ cat test.txt 
This is my test data. 

火衛一的下一個版本將具有文檔中的例子,說明如何閱讀和編寫zip文件。

相關問題