Decompress the Gzip json data in .Net Controller

522 Views Asked by At

I'm currently trying to decompress the json data that being passed in from the server side in the controller but keep hitting below error.

System.IO.InvalidDataException: 'The archive entry was compressed using an unsupported compression method.'

Below is the ajax code that pass in the json data to the controller.

function SaveQuestion(jsonData, formId, parentId, message, flatJsonData, isSaveAndCreate) {
    $('#overlay2').show();
    // Compress the JSON data using gzip
    var compressedJsonData = pako.gzip(jsonData, { to: 'string' });

    // Base64 encode the compressed data
    var encodedCompressedJsonData = btoa(compressedJsonData);

    $.ajax({
        url: "/IntelCollection/SaveDynamicForm",
        type: "POST",
        data: { formId: formId, jsonData: encodedCompressedJsonData, flatJsonData: flatJsonData, parentId: parentId },
        success: function (result) {
            if (isTest) {
                isTest = false;
                $('#tree').jstree(true).refresh();
                goForwardToCard3();
            }

            if (!isSaveAndCreate) {
                if (!isGoBack) {
                    refreshNodeId = result;
                }
                else {
                    isGoBack = false;
                }
            }
            else {
                refreshNodeId = 0;
            }

            $('#overlay2').hide();
            $("#tree").jstree(true).refresh();
            ReloadPreView();
            GrowlMessage(message, 'success');
        },
        error: function (request, status, error) {
            console.log(request.responseText);
        }
    });`
}

And below is the controller that received the data

public async Task<string> SaveDynamicForm(int formId, string jsonData, string flatJsonData, string parentId = "", string optionId = "")
{
         // Call the Decompress method to get the decompressed JSON data
         string decompressedJson = Decompress(jsonData);

         var treeModel = JsonConvert.DeserializeObject<TreeModel>(decompressedJson);
}

and this is the function that handle the compressed data

public static string Decompress(string value)
{
      byte[] compressedBytes = Convert.FromBase64String(value);
      string decompressedJson = "";

      using (MemoryStream compressedStream = new MemoryStream(compressedBytes))
      using (DeflateStream decompressionStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
      using (MemoryStream decompressedStream = new MemoryStream())
      {
          // Decompress the data
          decompressionStream.CopyTo(decompressedStream);

          // Convert the decompressed bytes to a string
          decompressedJson = Encoding.UTF8.GetString(decompressedStream.ToArray());

      }
      return decompressedJson;
}

I did try to use different type of decompress method such as GzipStream but still got similar issue. It seems no issue when encode and decode the data. So, i assuming the decompress function is not a right one. Do you guys have any insight on this?

1

There are 1 best solutions below

1
Felipe Gabriel Souza Martins On

Basically the error says that the server cannot handle the gzip method, what I can recommend you to do is: On the server side you use the decompression method that properly handles gzip. An example would be the GZipStream class.

public ActionResult GenericMethod(string formId, string jsonData, string flatJsonData, string parentId)
{
    byte[] compressedData = Convert.FromBase64String(jsonData);
    
    using (var compressedStream = new MemoryStream(compressedData))
    using (var decompressedStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    using (var reader = new StreamReader(decompressedStream))
    {
        string decompressedJsonData = reader.ReadToEnd();
        // Treat how to best find json data
    }
}