Saturday, May 30, 2009

Extracting Zip files in Adobe AIR with the nochump library

Today, I'm going to show you how you can use the nochump library to extract zip files with Adobe AIR.

The ZipFile class constructor takes an IDataStream object as the parameter.Here you can use a ByteArray or a FileStream object which contains the data of the zip file. The entries property of the ZipFile object contains all the files in the archive as ZipEntry objects. Here the directory itself(excluding the files inside it) also counts as a ZipEntry object, so make sure to check using the isDirectory property of the ZipEntry object. Finally get the data of a single file using zipFile.getInput() method.

Here's the code :

import flash.filesystem.*;
import flash.net.FileFilter;

import nochump.util.zip.*;

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

private function loadZIP():void
{
var filter:FileFilter = new FileFilter("ZIP","*.zip");
zipInput.browseForOpen("Open ZIP file",[filter]);
zipInput.addEventListener(Event.SELECT, onSelect);
}

private function onSelect(e:Event):void
{
var stream:FileStream = new FileStream();
stream.open(zipInput,FileMode.READ);

zipFile = new ZipFile(stream);
extract.enabled = true;
}

private function extractZIP():void
{
zipOutput.browseForDirectory("Select Directory for extract");
zipOutput.addEventListener(Event.SELECT, onDirSelect);
}

private function onDirSelect(e:Event):void
{
for(var i:uint = 0; i < zipFile.entries.length; i++)
{
var zipEntry:ZipEntry = zipFile.entries[i] as ZipEntry;
// The class considers the folder itself (without the contents) as a ZipEntry.
// So the code creates the subdirectories as expected.
if(!zipEntry.isDirectory())
{
var targetDir:File = e.target as File;
var entryFile:File = new File();
entryFile = targetDir.resolvePath(zipEntry.name);
var entry:FileStream = new FileStream();
entry.open(entryFile, FileMode.WRITE);
entry.writeBytes(zipFile.getInput(zipEntry));
entry.close();
}
}
}

You can download the project archive here. Happy experimenting :D

25 comments:

  1. Have you used the FZip library? How does it compare to nochump?

    http://codeazur.com.br/lab/fzip/

    ReplyDelete
    Replies
    1. It doesn't work for Data Description check my website you can try to use new URLRequest or embed zip as class if you like that?
      http://sourceskyboxer.byethost16.com/dengfziptonochumputilszip

      Delete
  2. @JesterXL I did the same example using FZip. You can download it here . But as you can see in their webpage, you cannot use FZip to extract Zip files with data descriptors, which is a major disadvantage. However, it seems to be faster at extracting simple archives containing text files and images and has helper functions for such file types. But nochump doesn't have problems with data descriptors, so use that for your projects.
    Thanks for the comment.

    ReplyDelete
  3. Hi Pradeek,

    This code seems to be working fine for extracting the small size ZIP file but for the ZIP file with size more than 300Mb it’s a bit slow and taking a lot of time to extract the files…is there any way the performance could be improved or is there any other library that I should look for…please advice?

    ReplyDelete
  4. @Yogesh So far, I have only found only nochump and Fzip for extracting zip files. FZip is supposedly faster but does not support many zip files (with data descriptors). Have you tried Fzip? I have given a similar example in the previous comment.Sorry I couldn't be of more help.
    Thanks for the comment.

    ReplyDelete
  5. @Yogesh Or wait until someone writes a more optimized library using FP10. :D

    ReplyDelete
  6. does Your nochump code works on MAC...fzip does not supports MAC

    ReplyDelete
    Replies
    1. because deng.fzip has not data description....

      Delete
  7. Yes it does. The reason why FZip doesn't work is because FZip can't read ZIPs that make use of Data Descriptors such as those created by the Mac OS X Archiver utility.

    ReplyDelete
  8. Yeah it did work infact ,Thanks for the code.....you have made a brilliant effort,like to see more from you...

    ReplyDelete
  9. When I was in internet on one site I saw interesting tool and after that I forgot it,but some time ago I had problems with zip files and remembered,that tool works with corrupted zip files and more-cannot open zip file,it helped me,tool is free as far as I know,it can also many resources,utiltiy can as well work under all supported versions of this operating system,extremely easy to use, when the program is installed, you can see a file selection dialog, you can choose any file with *.zip extension, including self-extracting archives,program has a powerful engine, that can work with several different algorithms of analysis, it features an easy graphic interface, that will easily guide you through several simple steps of recovery process,tool can work with password-protected files.

    ReplyDelete
  10. How can zip a folder with this library?

    ReplyDelete
  11. @Anonymous Check my other post in the comments

    http://pradeek.blogspot.com/2009/05/creating-zip-files-with-adobe-air-with.html#comments

    ReplyDelete
  12. Yes its working now. Thank you

    ReplyDelete
  13. Hi, i am trying to save using Zinc 3.0 when i try to extract an swf and try to launch it.. it doesn't work... any idea?

    ReplyDelete
    Replies
    1. Zinc i have tested fzip but fzip is crazy to manage with zinc 3.0 but zinc 4.0.22 has mdm.zip - it doesn't work sorry

      I suggest you want build haxe - it is very better than Zinc 4.0

      Delete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Great article,
    works good with Zinc 3.0, here is coe example:
    var fileBA:ByteArray;

    for(var i:uint = 0; i < dwonloadedZipFile.entries.length; i++)
    {
    var zipEntry:ZipEntry = dwonloadedZipFile.entries[i] as ZipEntry;

    if(zipEntry.isDirectory())
    {
    if(!Folder.instance.exists(Path.HANDBRAKE_FOLDER_PATH + zipEntry)) Folder.instance.create(Path.HANDBRAKE_FOLDER_PATH + zipEntry);
    }
    else if(!zipEntry.isDirectory())
    {
    trace(zipEntry.name);
    fileBA = dwonloadedZipFile.getInput(zipEntry);
    // if(!Folder.instance.exists(Path.HANDBRAKE_FOLDER_PATH)) Folder.instance.create(Path.HANDBRAKE_FOLDER_PATH);

    mdm.FileSystem.BinaryFile.setDataBA(fileBA);
    mdm.FileSystem.BinaryFile.writeDataBA(Path.HANDBRAKE_FOLDER_PATH + zipEntry.name);
    }
    }

    ReplyDelete
  16. Nice work! :)

    How do i create zipfile and pack files/folders into zip file?
    I would like to suggest for packing and creating/writing zip file :)

    Thanks :)

    ReplyDelete
  17. Hi Pradeek, Do you know if I need a license to use Nochump swc? If so, what is the procedure ?

    ReplyDelete
  18. Hi Pradeek,
    I used this library in my air project and if the file size of zip is above 600MB,it throws out Error #1000: The system is out of memory in line code "zipFile= new ZipFile(data) "while decompressing.Kindly suggest a workaround for this problem.

    ReplyDelete
  19. Hi Pradeek,
    I am unable to download nochamp library.
    Can you please suggest me direct links
    Please help me

    ReplyDelete
    Replies
    1. Download it from my dropbox - Don't worry you have library now!
      https://www.dropbox.com/s/y410mnzya6ud1nd/nochump.zip?dl=0

      Delete
    2. Download it from my dropbox - Don't worry you have library now!
      https://www.dropbox.com/s/y410mnzya6ud1nd/nochump.zip?dl=0

      Delete
  20. Hey great idea :) I like you build better and i try to move as embed zip from swf - if you don't want many logos or sprites from zip - if hackers check to unzip and steal features.. that is why i have idea as "embed zip into swf" works for Air and Flash Player 20.x Completed!!!
    Please visit my website http://sourceskyboxer.byethost16.com/dengfziptonochumputilszip

    ReplyDelete