format.zip: Basic usage

NOTE: The following example applies to targets that have access to the computer file system for file loading/saving (neko/cpp/php etc). For flash usage, have a look here: http://haxe.org/com/libs/format/zip/flash

Creating a zip file

Let's say that we want to create a zip file with one text file items inside: textfile.txt with the content of "Hello zip!".

1. Creating a zip file entry


Each of the zip file items is represented by a format.zip.Data.Entry, so we start with creating that entry out of the "Hello zip!" string in the following way:
var string = "Hello zip!"; 
// Convert the string to bytes
var bytes = Bytes.ofString(string); 
// Create a zip entry for the bytes:
var entry:format.zip.Data.Entry =  
{
    fileName : "path/to/textfile.txt", // <- This is the internal zip file folder structure
    fileSize : bytes.length,
    fileTime : Date.now(),
    compressed : false,
    dataSize : 0,
    data : bytes,
    crc32 : format.tools.CRC32.encode(bytes),
    extraFields : new List()
}

Please note that we can freely create the internal directory structure of our zip file by defining the desired path to the content file.

2. Creating a list of zip entries


Now we have create a single file zip entry item. We can create as many entries as we like. The entries should then be added to a list of entries:
// Create a list of entries
var entries:List<format.zip.Data.Entry> = new List();
// Add our text data entry:
entries.add(entry);
// Add as many entries as you like...
// ...

3. Saving the zip file


Now we can save it all to a zip file by using a format.zip.Writer to write to a BytesOutput stream:
// Write our entries to a BytesOutput stream using format.zip.Writer            
var bytesOutput = new BytesOutput();
var writer = new format.zip.Writer(bytesOutput);
writer.writeData(entries);
// Grab the zipped file from the output stream
var zipfileBytes = bytesOutput.getBytes();
// Save the zipped file to disc
var file = File.write("test.zip", true);
file.write(zipfileBytes);
file.close();

Reading a zip file


Here's how we can read the content from our newly created test.zip.

4. Grabbing the list of entries out of the zip file


We start by reading the zip file and let the format.zip.reader get our entries out of it:
// Read our zip file and get the bytes into a BytesInput stream
var zipfileBytes = File.getBytes("test.zip");
var bytesInput = new BytesInput(zipfileBytes);
// use a format.zip.Reader to grab the zip entries
var reader = new format.zip.Reader(bytesInput);
var entries:List<format.zip.Data.Entry> = reader.read();        

As a result we have the same kind of list of entries as we saw in step 2 above.

5. Grabbing the data out of the zip entry


Now we can simply iterate our list of entries to search for the entry filename that we want to extract.
In step 1. above, we saved our string with the internal entry filename path/to/textfile.txt, so we have to search for this one among the entry filenames.
Here's a simplistic way of doing it:
var searchEntryFileName = "path/to/textfile.txt";
for (entry in entries ) {
    if (entry.fileName == searchEntryFileName) {
        // Get the bytes out of the entry and uncompress them
        var bytes:Bytes = format.zip.Tools.uncompress(entry.data);
        // Convert the bytes to a string
        var string = bytes.toString();
        // Should trace "Hello zip!"
        trace(string);
    }
}

version #15316, modified 2012-08-11 10:55:39 by ncannasse