2015-04-06 175 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文件。