Suliman
Suliman

Reputation: 1509

D: how to extract data from archive?

I have very simple code that should extract data from archive:

import std.stdio;
import std.string;
import std.file;
import std.algorithm;
import std.zip;

void main()
{
    string ar = `D:\ftp\s2-imfset_2015\IFPET-150101.zip`;
    auto zip = new ZipArchive(ar.read);
    foreach(ArchiveMember am; zip.directory)
    {
        writeln(am.expandedData);
    }
}

(thanks for explaining about each and map difference). But when I run it it's print [] on console.

Upvotes: 2

Views: 108

Answers (1)

Martin Nowak
Martin Nowak

Reputation: 1497

You need to call zip.expand(am) before the expanded data is available. You can also get the name and size of an archive member before expanding it.

import std.stdio;
import std.string;
import std.file;
import std.algorithm;
import std.zip;

void main()
{
    string ar = `D:\ftp\s2-imfset_2015\IFPET-150101.zip`;
    auto zip = new ZipArchive(ar.read);
    foreach(name, am; zip.directory)
    {
        if(!am.expandedSize) continue; // ignore empty files
        zip.expand(am);
        writeln(am.expandedData);
    }
}

Upvotes: 5

Related Questions