2

Possible Duplicate:
ZLIB Decompression - Client Side

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);
        }
    }
}

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!!

Community
  • 1
  • 1
user2000769
  • 121
  • 1
  • 2
  • 6
  • 1
    Better if I can replicate the method **Decompress** in a javascript style :) – user2000769 Jan 31 '13 at 16:17
  • 1
    To answer the question about other solutions, you have to specify what it is that you are trying to solve with this approach. – Guffa Jan 31 '13 at 16:17
  • I'm trying to solve this problem: my web app receive from the server an xml file with several infomations. Some of them are images described with a pair of attribute: key and value. The value is a string compressed by the function **Compress** written in C# and I want to be able to decompress that string within my web app written in javascript. – user2000769 Jan 31 '13 at 16:35

2 Answers2

0

The best solution might be to translate the decompression function from C# to Javascript. You could use one that's already available in Javascript such as this one, but you would need to change the source of the image or uncompress-recompress at the server, unless it happens to be compatible with the compression you're using.

Another option would be to convert the image in to .jpg or .png before you use it, again at the server. This would give you more flexibility in the long run, but might put a load on the server depending on traffic and image size.

Community
  • 1
  • 1
xpda
  • 15,585
  • 8
  • 51
  • 82
0

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

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

  • the JSXCompressor library seems not to be useful for my scope or (maybe :)) I don't understand how to use that library. Is there an example of JSXCompressor for my purpose? – user2000769 Jan 31 '13 at 17:23