Search code examples
actionscript-3arrayscompressionlzma

How To Decompress a LZMA Compressed File Using ByteArray Method In As3?


This is the code that I'am using to compress/decompress files :

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.utils.ByteArray;

public class Compressor extends Sprite
{
    private var ref:FileReference;

    public function Compressor()
    {
        ref = new FileReference();
        ref.addEventListener(Event.SELECT, load);
        ref.browse([new FileFilter("SWF Files", "*.swf")]);
    }

    private function load(e:Event):void
    {
        ref.addEventListener(Event.COMPLETE, processSWF);
        ref.load();
    }

    private function processSWF(e:Event):void
    {
        var swf:ByteArray;
        switch(ref.data.readMultiByte(3, "us-ascii"))
        {
            case "CWS":
                swf = decompress(ref.data);
                break;
            case "FWS":
                swf = compress(ref.data);
                break;
            default:
                throw Error("Not SWF...");
                break;
        }

        new FileReference().save(swf);
    }

    private function compress(data:ByteArray):ByteArray
    {
        var header:ByteArray = new ByteArray();
        var decompressed:ByteArray = new ByteArray();
        var compressed:ByteArray = new ByteArray();

        header.writeBytes(data, 3, 5); //read the header, excluding the signature
        decompressed.writeBytes(data, 8); //read the rest

        decompressed.compress();

        compressed.writeMultiByte("CWS", "us-ascii"); //mark as compressed
        compressed.writeBytes(header);
        compressed.writeBytes(decompressed);

        return compressed;
    }

    private function decompress(data:ByteArray):ByteArray
    {
        var header:ByteArray = new ByteArray();
        var compressed:ByteArray = new ByteArray();
        var decompressed:ByteArray = new ByteArray();

        header.writeBytes(data, 3, 5); //read the uncompressed header, excluding the signature
        compressed.writeBytes(data, 8); //read the rest, compressed

        compressed.uncompress();

        decompressed.writeMultiByte("FWS", "us-ascii"); //mark as uncompressed
        decompressed.writeBytes(header); //write the header back
        decompressed.writeBytes(compressed); //write the now uncompressed content

        return decompressed;
    }

}
}

My problem : This method doesn't decompress LZMA compressed files :(

Can anyone please tell me how to restructure the above code to achieve LZMA decompression and whether the above compression code is good enough for LZMA-compression?If not, please do give an example of it.

EDIT : After long hours of searching,I got this but I can't quite understand the example code in it :( Some help,anyone?


Solution

  • Found a workaround, Flash only has DEFLATE and ZLIB compression algorithms so I had to wait for LZMA update which may or may not happen or create my own utility so I got Python and after a few neck-breaking hours of trying to figure out how to use it, I was finally able to get what I wanted.

    Places of reference:

    1. The python code to decompress LZMA-format swf files
    2. The pylzma source file