Thursday, April 1, 2010

Flex Directory Compression

Here is an example for compression and decompression of files and directories using the DEFLATE compression algorithm. 

The structure I have created for the file structure in the compressed file is arbitrary.  for each file it describes the name and data witch is a byte array of the file content. Directories do not have a data attribute, but children that recursively describe files with in.  


Download the source code here

Download the example AIR application here

 
When running the example, drag and drop a file or directory into the upper box, this will catch the link and create a compressed version of the file in the application storage directory (i.e. /AppData/Roaming/Deflator/Local Store)

To extract the file back, click on the compressed file in the list and click the extract button. This will recreate the file or directory on your desktop.




Please note: that if the original file is still on the desktop when you extract, you will have an error. so if you deflated something from the desktop, make sure you rename it before you extract

Good luck.

10 comments:

  1. Hi Gilad and thanks for sharing this.

    Your app works great. However, I'm trying to decompress a folder that was compressed using your class on a linux server. I tried using gunzip amongst others but I keep getting an error.

    Any ideas how I would go about doing that?
    Cheers,
    Nick.

    ReplyDelete
  2. Hi Nick,
    to have the compression work as a GZIP, you will have to make some adjustments. per file that is added to the archive you need to have:
    * a 10-byte header, containing a magic number, a version number and a timestamp
    * optional extra headers, such as the original file name,
    * a body, containing a DEFLATE-compressed payload
    * an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data

    you can find all this at: http://en.wikipedia.org/wiki/Gzip

    for my particular needs, the file entries contain the file name and the payload. in case its a directory, instead of the binary payload i have a list of children.

    mind you, i found a bug today in the distribute function:
    line 95 should read: resStream.writeBytes(d,0,d.bytesAvailable);
    instead of the writeObject currently there.

    hope this helps..

    ReplyDelete
  3. link for dropbox is broken

    ReplyDelete
  4. i try to download the file but i cant get that. anyone can help me to get ?

    ReplyDelete
  5. Anyone have any luck finding the source files?

    ReplyDelete
  6. Arghh where is the source :-(

    ReplyDelete
  7. HI Gilad Manor source is missing from dropBox. Can u fix it.
    suvrajit nayak
    suvrajitn@riaxe.com

    ReplyDelete
  8. found it: https://github.com/giladmanor/JunkYard/tree/master/actionscript
    sorry guys

    ReplyDelete

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.