Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Unzip using std.zip

Part of StandardLibraryCategory

Description

Shows how to extract a file from a .zip archive.

Instructions

You'll need an archive called "test.zip". You can download a test.zip file that I've used (found on this page).

Example

import std.file;
import std.date;
import std.stream;
import std.zip;
import std.zlib;


int main(char[][] args)
{
    File f;
    byte[] buffer;
    std.zip.ZipArchive zr;
    char[] zipname;


    if (args.length > 1) zipname = args[1];
    else zipname = "test.zip";

    buffer = cast(byte[])std.file.read(zipname);
    zr = new std.zip.ZipArchive(cast(void[])buffer);

    foreach (ArchiveMember de; zr.directory)
    {
        f = new File(de.name, FileMode.Out);

        zr.expand(de);
        f.write(de.expandedData);
    }
    printf("Success!\n");
    return 0;
}

Source

Adapted from Walter's zlib example.