Take the 2-minute tour ×

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);

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

        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


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

        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


        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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer

Your Answer


