AS3 port of JZLib
Last month I was working on a project that used FZip to decompress some zip files in flash.
One tricky thing about FZip is that when running in Flash Player it requires your zip files to have an adler32 checksum for each file in order to work. This is normally fixed with a work around python script provided with FZip.
The python script is easy and all, but why not figure out how to do it in pure AS3 with more standard zip files?
The checksum is needed because AS3’s ByteArray only supports ZLib when running in Flash Player. In AIR it supports deflate which is what zip files use by default. I would be really curious to hear from Adobe why they chose not to support deflate since you need deflate for Zlib to work anyway.
I decided to implement inflate in as3 but I didn’t want to do it with new code so I looked for FOSS projects to port. JZlib was a good choice because Java is similar to AS3 and it didn’t rely on any external system calls.
This port supports everything in JZlib so you can use it for any inflate or deflate operations you might need.
To use with FZip
I used this library in FZip so it no longer requires that zip files be converted before use. It is tested to be working with the OSX cli zip command. It doesn’t work with OSX finder zip compression because of another issue.
- Download Modified FZip Source
- Download as3zlib
- Google Code Home for as3zlib
- View FZip decompress example
In FZipFile.as:
protected function parseContent(data:IDataInput):void { if(_compressionMethod === COMPRESSION_DEFLATED && !_encrypted) { if(HAS_INFLATE) { // Adobe Air supports inflate decompression. // If we got here, this is an Air application // and we can decompress without using the Adler32 hack // so we just write out the raw deflate compressed file data.readBytes(_content, 0, _sizeCompressed); } else if(_hasAdler32) { // Add zlib header // CMF (compression method and info) _content.writeByte(0x78); // FLG (compression level, preset dict, checkbits) var flg:uint = (~_deflateSpeedOption << 6) & 0xc0; flg += 31 - (((0x78 << 8) | flg) % 31); _content.writeByte(flg); // Add raw deflate-compressed file data.readBytes(_content, 2, _sizeCompressed); // Add adler32 checksum _content.position = _content.length; _content.writeUnsignedInt(_adler32); } else { data.readBytes(_content, 0, _sizeCompressed); //throw new Error("Adler32 checksum not found."); } isCompressed = true; } else if(_compressionMethod == COMPRESSION_NONE) { data.readBytes(_content, 0, _sizeCompressed); isCompressed = false; } else { throw new Error("Compression method " + _compressionMethod + " is not supported."); } _content.position = 0; } protected function uncompress():void { if(isCompressed && _content.length > 0) { _content.position = 0; if(HAS_INFLATE) { _content.uncompress.apply(_content, ["deflate"]); } else if(_hasAdler32) { _content.uncompress(); } else { var uncompr:ByteArray = new ByteArray(); var d_stream:ZStream=new ZStream(); d_stream.next_in = _content; d_stream.next_in_index = 0; d_stream.next_out = uncompr; d_stream.next_out_index = 0; var err:int = d_stream.inflateInitWithNoWrap(true); while(d_stream.total_out != 1 && d_stream.total_in < _content.length && i<=_content.length*4) { d_stream.avail_in=d_stream.avail_out=10; err=d_stream.inflate(JZlib.Z_NO_FLUSH); if(err == JZlib.Z_STREAM_END) { System.println("decompress success:" + this.filename); break; } else if (err == JZlib.Z_STREAM_ERROR) { System.println("stream error:" + this.filename + " " + d_stream.msg); break; } else if (err == JZlib.Z_DATA_ERROR) { System.println("data error:" + this.filename + " " + d_stream.msg); break; //} else { // System.println("status:" + this.filename + " " + err); } } err=d_stream.inflateEnd(); _content = uncompr; } _content.position = 0; isCompressed = false; } }

June 21st, 2009 at 11:05 am
Hi Tea,
I would really like to use your Fzip class, but unfortunatelly the source files use a class that I can’t find in the source…: com.wirelust.util.Cast;
could you please add it to the repository, or somewhere so I can check the usage of your class?
Thanks,
Stan
June 21st, 2009 at 4:52 pm
Oops. sorry I forgot to check that in. You can get it here:
http://code.google.com/p/as3zlib/source/browse/trunk/src/com/wirelust/util/Cast.as
I’ll update the zip archive later on.
July 1st, 2009 at 9:59 pm
I made some modifications to this library to fix some flex3 compilation warnings as well as some corruption that occurs when using Z_SYNC_FLUSH. I attempted to send you an email but have no idea if it was the correct address, let me know if you would like me to send you a diff, and thanks!
September 11th, 2009 at 11:12 am
Hey Tea,
I love this library, it works like a charm.. well zipping up data does.. when you use
var zip:FZip = new Zip();
// load a file
// add listeners
var file:FZipFile = zip.getFileAt(FILE_INDEX);
…
Does matter if its an image or in my case I tried \utf-8\ text.. It chops off alittle of the end.. Why is that?? Thanks
September 20th, 2009 at 2:06 pm
Are you sure this really works ?
I am trying to build a swc in flex containing this code, but i get language errors
- the \in\ variables, in as3, \in\ – is keyword
- the throws at the end of each method, in java this is possible, in as3 i doubt
Please, make it work
September 22nd, 2009 at 3:38 pm
how to use the fzip-prepare.py? pls reaply me via this email
January 21st, 2010 at 6:26 am
Hello,
I use your classe, it works well, but for texts files (in my case xml files) the getContentAsString cut the end of the string….
If do you plan to fix it, it would be very kind of you !!
Thanks
Thierry
January 21st, 2010 at 7:54 am
Please email me an example project where this is happening and I will look into fixing it. Unfortunately I don’t have time to investigate without knowing the exact nature of the problem.