如何在javascript中解压缩字符串,用C#压缩? [英] How to decompress a string in javascript, compressed in C#?

查看:202
本文介绍了如何在javascript中解压缩字符串,用C#压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

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

I'll try to be clear and I'm sorry for my bad english. This is the question: In my web application i received a string that represent an image compressed with this algorithm, written in 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);
        }
    }
}

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

The Decompress method is used in the Server side application. I receive an xml file with the string that represent the image compressed with the Compress method and I want to be able to decompress the string I received in javascript within my web app. Is there a way to do that? Are there other solutions? Thank's to everyone!!

推荐答案

你可以使用 JSXCompressor 库进行解压缩(deflate,unzip)。

You can use JSXCompressor library to do decompression (deflate, unzip).

但是如果您的网络服务器支持http级别的压缩,我认为您可以跳过压缩和解压缩。

But if your web server support compression at http level I think you can skip compression and decompression.

这篇关于如何在javascript中解压缩字符串,用C#压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆