gpt4 book ai didi

c# - 如何在javascript中解压缩在C#中压缩的字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:04 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
ZLIB Decompression - Client Side

我会尽量说清楚,我很抱歉我的英语不好。这是问题:在我的 Web 应用程序中,我收到了一个字符串,表示使用此算法压缩的图像,用 C# 编写:

public static class Compression
{
public static string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}

ms.Position = 0;
MemoryStream outStream = new MemoryStream();

byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}

public static string Decompress(string compressedText)
{

byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

byte[] buffer = new byte[msgLength];

ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}

return Encoding.UTF8.GetString(buffer);
}
}
}

Decompress 方法用于服务器端应用程序。我收到一个 xml 文件,其中包含表示使用 Compress 方法压缩的图像的字符串,我希望能够解压缩我在 Web 应用程序中以 javascript 收到的字符串。有没有办法做到这一点?还有其他解决方案吗?谢谢大家!!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com