'How to Compress JSON data in angular 4 app using gzip compression & send it to server using Http post?

I am using pako library for compression:

 var binaryString = pako.gzip(data);

At server side (.net core) i am using middleware to decomopress the zipped string.

 try
 {
     context.Request.Body = new GZipStream(context.Request.Body, CompressionMode.Decompress);
 }

But i am getting error as the archive entry was compressed using an unsupported compression method



Solution 1:[1]

 compress(objectYouWantToCompress: any) {
 var data = JSON.stringify(objectYouWantToCompress);
 var binaryString = this.pako.gzip(data);
 return btoa(String.fromCharCode(...new Uint8Array(binaryString)));
}

this can decompress on .net to your required object

on dotnet side:

public string Unzip(string str)
{
     byte[] bytes=Convert.FromBase64String(str);
     using (var msi = new MemoryStream(bytes))
     using (var mso = new MemoryStream())
     {
         using (var gs = new GZipStream(msi, CompressionMode.Decompress))
           {
                    CopyTo(gs, mso);
           }

                return Encoding.UTF8.GetString(mso.ToArray());
       }
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hari G