Júlio Almeida
Júlio Almeida

Reputation: 1869

Deflate with Pako and inflate with asp net core

I'm trying to deflate a .xlsx file on the front end and inflate it in the server side, in a asp net core 2.2 server.

I tried everything and i have this now:

//JS code
handleSaveFile = (file) => {
    var compressedFile = pako.deflate(JSON.stringify(file), { to: 'string' });
    this.setState({ file: compressedFile });
  } 

Completely straight forward, pako.deflate is enough to do the trick.

Now on the back-end i tried everything but according to the documentation ends up like this: enter image description here

I tried also GZipStream, but the result is the same. I cant find anything regarding compress/decompress but there is plenty of info regarding the other way around.

Upvotes: 1

Views: 2028

Answers (1)

Haryono
Haryono

Reputation: 2822

Please take a look this, hope will help:

Client:

let output = pako.gzip(JSON.stringify(obj));

Server:

public static string Decompress(byte[] data)
{
    // Read the last 4 bytes to get the length
    byte[] lengthBuffer = new byte[4];
    Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
    int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);

    var buffer = new byte[uncompressedSize];
    using (var ms = new MemoryStream(data))
    {
        using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
        {
            gzip.Read(buffer, 0, uncompressedSize);
        }
    }
    string json = Encoding.UTF8.GetString(buffer); 
    return json;
}

detail in : https://stackoverflow.com/a/66825721/1979406

Upvotes: 2

Related Questions