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.events.FileListEvent;
import flash.filesystem.*;

import nochump.util.zip.*;

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;
stream.open(f,FileMode.READ);
var fileData:ByteArray = new ByteArray();
stream.readBytes(fileData);
var file:Object = new Object();
file.name = f.name;
file.data = fileData;
files.push(file);
}
}

private function createZIP():void
{
zipFile = new ZipOutput();
for(var i:uint = 0; i < files.length; i++)
{
var zipEntry:ZipEntry = new ZipEntry(files[i].name);
zipFile.putNextEntry(zipEntry);
zipFile.write(files[i].data);
zipFile.closeEntry();
}
zipFile.finish();

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

private function onSave(e:Event):void
{
var archiveFile:File = e.target as File;
if(!archiveFile.exists)
{
var stream:FileStream = new FileStream();
stream.open(archiveFile,FileMode.WRITE);
stream.writeBytes(zipFile.byteArray);
stream.close();
}
}

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

12 comments:

  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 nochump.util.zip::ZipOutput/closeEntry()
    at SaveFile_fla::MainTimeline/zipToFile()

    Any idea?
    Regards

    ReplyDelete
  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 @ jpradeek@gmail.com ? Thanks.

    ReplyDelete
  3. hii pradeek

    how can i zip a folder through your code

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

    ReplyDelete
  5. Kind of having the same problem with zip folders

    ReplyDelete
  6. Here is an example.
    http://files.getdropbox.com/u/1088914/FlexZipCreator.mxml

    ReplyDelete
  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:
    http://www.peter-monte.com/temp/

    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,
    Peter

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

    http://files.getdropbox.com/u/1088914/FlexZipCreator.mxml

    ReplyDelete
  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.

    ReplyDelete