Sunday, May 31, 2009

Creating Zip files with Adobe AIR with the nochump library

Following up on my previous post, here's how to archive a bunch of files and make a zip file.

After getting all the files you want to put in the archive, create a new ZipEntry object for each file and add it to a ZipOutput object using the putNextEntry() of the ZipOutput class. Then pass the ByteArray data of the file to the write() method of the ZipOutput class and close the entry by using the closeEntry() method. Finally, call the finish() method once you have done the same for all the files.

Here's the code snippet :

import flash.filesystem.*;


private var zipInput:File = new File();
private var zipOutput:File = new File();
private var zipFile:ZipOutput;

private var files:Array = new Array();

private function loadFiles():void
zipInput.browseForOpenMultiple("Open ZIP file");
zipInput.addEventListener(FileListEvent.SELECT_MULTIPLE, onSelect);

private function onSelect(e:FileListEvent):void
for(var i:uint = 0;i < e.files.length;i++)
var stream:FileStream = new FileStream();
var f:File = e.files[i] as File;,FileMode.READ);
var fileData:ByteArray = new ByteArray();
var file:Object = new Object(); =; = fileData;

private function createZIP():void
zipFile = new ZipOutput();
for(var i:uint = 0; i < files.length; i++)
var zipEntry:ZipEntry = new ZipEntry(files[i].name);

zipOutput.browseForSave("Select target directory");
zipOutput.addEventListener(Event.SELECT, onSave);

private function onSave(e:Event):void
var archiveFile:File = as File;
var stream:FileStream = new FileStream();,FileMode.WRITE);

As usual you can download the project archive here. Happy experimenting :D


  1. Hi there,

    I'm trying to achieve this result but not possible yet.
    In my situation I read a zip file extract a specific file, zippit and then safe to disk.
    Problem is when I call the method OutPutZip.closeEntry () it launches an error:

    Error: invalid entry compressed size (expected 1488 but got 1425 bytes)
    at SaveFile_fla::MainTimeline/zipToFile()

    Any idea?

  2. There seems to be a problem with the compression of the specified file. Can you post the source code of that particular function or please email me @ ? Thanks.

  3. hii pradeek

    how can i zip a folder through your code

  4. ok....i got how to zip a folder thanks

  5. Kind of having the same problem with zip folders

  6. Here is an example.

  7. Hi Pradeek,

    I thing I understood how to zip a file or files including the full path. What I can't do is to zip an entire folder from root with its files.

    Here is the example:

    There also may be a problem with selecting a folder, which contains all files, and simultaneously select some specific files inside that folder. But still if you try to select a top folder from a zip and try to zip it to the disk nothing will happen since it launches an internal error.

    Best Regards,

  8. @Peter, Here is the code for zipping files along with their folder structure.

  9. Zip files to my mind one of the best important files,because I have on my computer near 2 000 zip files,but not so long approximately 100 zip files was corrupted and in inet I found this tool-crc zip files repair.It helped me surprisingly.Tool is free as far as I know and it can extract needed document from corrupted archive and repair it,moreover utility repair zip files crc error and you'd like to save recovered data.