如何在.net c#中压缩字符串并在flash as3中解压缩?

发布于 2024-09-06 21:37:57 字数 1954 浏览 9 评论 0原文

我必须在 flash 中加载一个大的 xml,并且我正在尝试将其压缩发送。为此,我尝试使用 zlib 压缩字符串并将其以 Base64 编码发送。在闪存中,我将字符串转换为字节数组并使用其 uncompress() 方法。到目前为止,我尝试过:

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 (我不知道如何将压缩设置为 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);

所有都会产生不同的结果,但 flash 会抛出错误 #2058: 解压缩时出错数据。

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

来自此处的 Base64 类 http://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/com/hurlant/util/Base64.as?r=3

那么,我怎样才能压缩.net中的字符串并在flash中解压缩它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-09-13 21:37:57

我让它与 ZLIB.NET 一起工作。我只需将编码设置为 ASCII Encoding.ASCII.GetBytes(str);

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

浅浅 2024-09-13 21:37:57

Flash 在插件版本中不支持 zip 压缩。它只进行 gzip 压缩。仅当您针对 AIR 进行编译时,您才可以选择使用 zip。因此,如果您的目标是浏览器,则需要在 c# 端有一个 gzip 编码器(我认为它是 GZipStream)

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html#uncompress%28%29< /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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文