【问题标题】:D: how to add file to zip archive?D:如何将文件添加到 zip 存档中?
【发布时间】:2015-04-06 09:34:42
【问题描述】:

我认为最好将其拆分为两个单独的问题,以帮助新手找到答案。

Previous question 是关于从 zip 存档中提取数据的。但是现在我需要任何示例来展示如何将数据添加到 zip 存档中。

有两种情况。 1.独立文件 2. 测试字符串如:string foo = "qwerty"; 如何发送到 zip?

std.zip 的文档中,我找到了方法addMember(ArchiveMember de);,但我不明白如何将数据传递给它。

【问题讨论】:

    标签: zip d archive


    【解决方案1】:

    显然,新文档没有显示此模块的使用情况。我阅读了源代码并整理出这是这样做的方法:

    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.
    

    Phobos 的下一版本将在文档中提供示例,展示如何读取和写入 zip 文件。

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多