views:

33

answers:

2

Hi,

I have to load a large xml in flash and I'm trying to send it compressed. To do that I tried to zlib compress the string and send it base64 encoded. In flash I turn the string into a byte array and use its uncompress() method. So far I tried:

ZLIB.NET

byte[] bytData = System.Text.Encoding.UTF8.GetBytes(str);
MemoryStream ms = new MemoryStream();
Stream s = new zlib.ZOutputStream(ms, 3);
s.Write(bytData, 0, bytData.Length);
s.Close();
byte[] compressedData = (byte[])ms.ToArray();
return System.Convert.ToBase64String(compressedData);

Ionic.Zlib (DotNetZip)

return System.Convert.ToBase64String(Ionic.Zlib.GZipStream.CompressBuffer(System.Text.Encoding.UTF8.GetBytes(str)));

ICSharpCode.SharpZipLib (I don't know how to set the compression to zlib)

byte[] a = Encoding.Default.GetBytes(str);
MemoryStream memStreamIn = new MemoryStream(a);
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
ZipEntry newEntry = new ZipEntry("zipEntryName");
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
byte[] byteArrayOut = outputMemStream.ToArray();
return System.Convert.ToBase64String(byteArrayOut);

All produce different results, but flash throws an Error #2058: There was an error decompressing the data.

var decode:ByteArray = Base64.decodeToByteArray(str);
decode.uncompress();
return decode.toString();

Base64 class from here http://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/com/hurlant/util/Base64.as?r=3

So, how can i compress a string in .net and decompress it in flash?

A: 

Flash does not support zip compression in the plugin version. It only does gzip. Only if you compile for AIR you have the option to use zip. So if you are aiming for the browser you will need a gzip encoder on the c# side (I think it's GZipStream)

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html#uncompress%28%29

Quasimondo
Yes, from the documentation: "Flash Player supports only the default algorithm, zlib". That's why I try to zlib in .net. Or am I missing something here?
bfi
P.S. I already tried GZipStream (second example from my post), from the DotNetZip library. I does the same thing as System.IO.Compression, only better.
bfi
Oh I see - I somehow had just looked at your third example and ignored the second one.
Quasimondo
+1  A: 

I got it to work with ZLIB.NET. I just had to set the encoding to ASCII Encoding.ASCII.GetBytes(str);

bfi
As far as performance goes, it turns a 6.5 MB xml into a 43 KB string. I don't have time to test the flash decoding speed, but on my old Core 2 Duo 2 GHz seems very fast.
bfi