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
Hi there,
ReplyDeleteI'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
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.
ReplyDeletesent you an email
ReplyDeleteProblem solved
ReplyDeleteThanks ;)
hii pradeek
ReplyDeletehow can i zip a folder through your code
ok....i got how to zip a folder thanks
ReplyDeleteKind of having the same problem with zip folders
ReplyDeleteHere is an example.
ReplyDeletehttp://files.getdropbox.com/u/1088914/FlexZipCreator.mxml
Hi Pradeek,
ReplyDeleteI 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
@Peter, Here is the code for zipping files along with their folder structure.
ReplyDeletehttp://files.getdropbox.com/u/1088914/FlexZipCreator.mxml
thanks ;)
ReplyDeleteZip 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