Reputation: 33
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?
Upvotes: 1
Views: 6235
Reputation: 199
I just wanted to add to this regarding the link supplied in your edit in case someone stumbles over it and gets as confused by the formatting as I was, it also has some weird stuff going on with writing a few bytes twice, so here's my heavily modified version of it (file parts require AIR). E.g. I moved away from bitwise operators in favor of ByteArray
read/write functions.
Note: At least nowadays, AS3's ByteArray.decompress()
supports LZMA decompression, so I'm using that, but it doesn't directly support the SWF LZMA format, only LZMA, so there's a bit of tinkering to be done, also note that this code can probably be improved a bit.
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.utils.ByteArray;
function decompressLZMA(idata:ByteArray){
idata.endian = "littleEndian"
var signature:ByteArray = new ByteArray;
//I 0-3 = signature + version
idata.readBytes(signature, 0, 3)
var version:uint = idata.readUnsignedByte()
switch (signature[0]){
case 90: // Z = lzma compressed
var odata:ByteArray = new ByteArray;
odata.endian = "littleEndian"
var i:uint;
//O 0-3 = signature + version
signature[0] = 70 // F = uncompressed
odata.writeBytes(signature)
odata.writeByte(version)
//I 4-7 = swf file length
idata.position = 4
var scriptlen:uint = idata.readUnsignedInt()
//8 = swf header size
scriptlen -= 8
//O 4-7 swf file length
odata.writeUnsignedInt(scriptlen + 8) // Re-add swf header removed earlier
var tdata:ByteArray = new ByteArray;
tdata.endian = "littleEndian"
//I 12 = lzma properties
//I 13-16 = lzma dictionary size
idata.position = 12
//T 0 = lzma properties
//T 1-4 = lzma dictionary size
tdata.writeByte(idata.readByte())
tdata.writeUnsignedInt(idata.readUnsignedInt())
// lzma uncompressed length is stored as LE UI64:
//T 5-8 = uncompressed length
tdata.writeUnsignedInt(scriptlen)
//T 9-12 = uncompressed length continued
tdata.writeUnsignedInt(0)
//16 = LZMA header size
idata.position = 17
//12 = SWF LZMA header size
idata.readBytes(tdata, 13, idata.length-idata.position)
tdata.uncompress(CompressionAlgorithm.LZMA)
odata.writeBytes(tdata)
return odata;
break
}
return idata;
}
var file:File;
var stream:FileStream;
var idata:ByteArray;
var odata:ByteArray;
var path:String
path = File.applicationDirectory.resolvePath('LZMA.swf').nativePath;
file = new File(path);
stream = new FileStream();
stream.open(file, FileMode.READ);
idata = new ByteArray
stream.readBytes(idata)
stream.close()
odata = decompressLZMA(idata)
path = File.applicationDirectory.resolvePath('LZMA_decompressed.swf').nativePath;
file = new File(path);
stream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeBytes(odata)
stream.close()
Upvotes: 1
Reputation: 33
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:
Upvotes: 1