开发者

How to compress a string in .net c# and decompress it in flash as3?

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.IsStreamO开发者_如何学Cwner = 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜