Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on an application which transfers huge data from server (Web API) to client (JavaScript) which takes some time over the network. So we decided to go with compression in server and decompression in client. I have used Zlib for .NET to compress my file. Below is the code which I have used for Compressing.

C#
private void compressFile(string inFile, string outFile)
    {
        System.IO.FileStream outFileStream = new System.IO.FileStream(outFile,      System.IO.FileMode.Create);
        zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream, zlib.zlibConst.Z_BEST_COMPRESSION);
        System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
        try
        {
            CopyStream(inFileStream, outZStream);
        }
        finally
        {
            outZStream.Close();
            outFileStream.Close();
            inFileStream.Close();
        }
    }

    public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
    {
        byte[] buffer = new byte[2000];
        int len;
        while ((len = input.Read(buffer, 0, 2000)) > 0)
        {
            output.Write(buffer, 0, len);
        }
        output.Flush();
    }

It has successfully compressed the data and I'm writing the compressed data to a txt file in my local machine and reading the compressed content of the file and transferring it to the client side.



Please help me resolve this issue or if you could recommend me a different method that would even help!

What I have tried:

<pre>I'm trying to decompress the data in client side using Zlib. Below is the code which I'm using


var inflate = new Zlib.Inflate(response);
var output = inflate.decompress();

But it throws an error stating "unsupported compression method".
Posted
Comments
Richard MacCutchan 27-Mar-17 4:12am    
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)

I am not sure if it is the problem, but the second stream is the zlib.ZOutputStream, so should you not declare it the same?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900