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