I come into a trouble implementing a Flex 3.0.0 client that receives compressed HTTP body from server via a socket HTTP library (not class HTTPService
).
First it seems that nginx supports ONLY gzip
compression with gzip on开发者_C百科;
. (Correct me if I'm wrong.) So, add HTTP header of Accept-Encoding: gzip
to request.
Then I get a compressed ByteArray
from HTTP response with header Content-Encoding: gzip
.
The problem here is to decompress the compressed data, namely the HTTP body, correctly.
In later version of Flex, there is a function deflate in ByteArray. But it is absent in Flex 3.0.0. Upgrading to higher version is not an option as it would make existing applications unstable with some new seen and unseen bugs.
Is there an alternative way, code or library, to decompress the gzip
data in ByteArray
?
The ByteArray methods that are needed aren't tied to a particular version of Flex. (The ByteArray class isn't a Flex class -- it's part of the built-in classes in Flash Player and AIR.) So even if your app uses Flex 3, if you can target Flash Player 10+ or AIR 1+ then you should be able to use that library.
If that's absolutely not possible, there is another possibility but it will take some work.
This ActionScript library is created for working with .zip files: http://nochump.com/blog/archives/15
However, it doesn't use the built-in ByteArray compression but instead includes an ActionScript implementation of the flate algorithm. So you could (theoretically) use the Inflater class from that library in place of the call to ByteArray.uncompress() in the GZip library mentioned by Chubbard
From chubbard and probertson's answer. I have worked out the solution.
http://probertson.com/projects/gzipencoder/
http://nochump.com/blog/archives/15
Add GzipEncoder and Zip Library to src
In GzipBytesEncoder.as
of GzipEncoder
(com.probertson.utils
),
replace
srcBytes.deflate();
with
var outBuffer:ByteArray = new ByteArray;
var deflater:Deflater = new Deflater();
deflater.setInput(srcBytes);
deflater.deflate(outBuffer)
srcBytes = outBuffer;
And replace
data.inflate();
with
var outBuffer:ByteArray = new ByteArray;
var inflater:Inflater = new Inflater();
inflater.setInput(data);
inflater.inflate(outBuffer)
data = outBuffer;
Give this a try:
http://probertson.com/projects/gzipencoder/
精彩评论