Unzip and save files using as3?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 01:13:47

There are a few libraries out there that deal with ZIP files in as3, but beware that this is no easy task for a beginner in ActionScript 3.

  • FZip seems to be the most widely used, but it requires that the ZIP archives have Adler32 checksums. Provided with the library there is a Python script that injects the checksum into ZIP files to preprocess the files before unzipping them.

  • As3 port of JZlib, an as3 library to use with Fzip instead of the Python script mentioned above.

  • AS3 Zip Library (the author states that is slower than FZip) that avoids the Addler32 checksum problem.

  • On Adobe Air, you can take a look at a detailed explanation and a working example in this article on the adobe website.

Hope this helps!

To decompress zip files, you can use AS3Commons Zip (formerly know as FZip). It works without the Adler32 checksum requirement mentionned in a previous answer.

Here's an example of how to extract all files in a zip archive. The function below would be called when a URLLoader object has downloaded the zip file and dispatched an Event.COMPLETE event:

import org.as3commons.zip.Zip;
import org.as3commons.zip.ZipFile;

private function _onZipDownloaded(e:Event):void {

    var ba:ByteArray = ByteArray(e.target.data);
    var zip:Zip = new Zip();
    zip.loadBytes(ba);

    for(var i:uint = 0; i < zip.getFileCount(); i++) {

        var zipFile:ZipFile = zip.getFileAt(i);
        var extracted:File = directory.resolvePath(zipFile.filename);

        var fs:FileStream = new FileStream();
        fs.open(extracted, FileMode.WRITE);
        fs.writeBytes(zipFile.content);
        fs.close();

    }

}

Obviously, error checking should be added to the code above but you get the idea...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!