GZIP From JAVA to C# translate

Karol Szymański 101 Reputation points
2021-07-21T13:47:14.337+00:00

I am using Visual Studio 2019, I am communicating with a (publicly available government) service probably written in Java, and I have a "badly packed data" error. I get JAVA code from the developer that works.

Here is the Java code

ByteArrayOutputStream bos = new ByteArrayOutputStream();

 GZIPOutputStream gZIPOutputStream;

 try {

       gZIPOutputStream = new GZIPOutputStream(bos);

 gZIPOutputStream.write(Files.readAllBytes(Paths.get("/Projects/STS/eTW/eTW-1_SIGNED.xml")));

       gZIPOutputStream.finish();

 } catch (IOException e1) {

       // TODO Auto-generated catch block

       e1.printStackTrace();

 }

Code packs xml with gzip, I need to have it in C #

I'm trying this

using (var compressedStream = new MemoryStream())
{
GZip.Compress(new MemoryStream(raw), compressedStream, false);
CompressedsignedxmlDocument = compressedStream.ToArray();
}

Unfortunately, I keep getting incorrectly packed data all the time. How should this be written in C #?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,931 questions
{count} votes

Accepted answer
  1. Karol Szymański 101 Reputation points
    2021-07-22T10:39:38.99+00:00

    byte[] raw = Encoding.UTF8.GetBytes(signedxmlDocument.InnerXml);

    Unfortunately it doesn't help
    API response

    Result: OK
    {
    "eventId": null,
    "referenceNumber": "f278c61c-da34-4169-be7d-d16c7dd24511",
    "etwDocStatus": 402,
    "changeEtwDocStatusTs": 1626950008000,
    "description": "Wystąpił błąd podczas przetwarzania wysyłki.",<---- There was an error processing your shipment.
    "details": "Nieprawidłowo skompresowana treść.", <---- Incorrectly compressed content.
    "upo": null,
    "timestamp": 1626950011000
    }

    This article says GZipStream has a bug, is that true?

    https://gist.github.com/muhammad-naderi/ae58ba0564897c6126c59164269ed428


4 additional answers

Sort by: Most helpful
  1. Karol Szymański 101 Reputation points
    2021-07-26T10:50:57.247+00:00

    Unfortunately, in my case, the empty string is deciphered.

    1 person found this answer helpful.

  2. Michael Taylor 54,321 Reputation points
    2021-07-21T15:07:30.367+00:00

    What is GZip in your C# code? There is no such class in .NET. In .NET you can use GZipStream.

    What is raw? You need to be very careful working with MemoryStream as streams are positional. If you are reading a stream then you need to "rewind" it if you need to read it again, and it is supported by the stream. Additionally you are dealing with stream lifetimes and since the compressed stream owns the output stream before you can get the stream results you need to ensure everything was written.

    Note also that if you're dealing with an XML file (and not XML data in memory) then you can more easily just work with file stream but I'll demo pulling the string from memory.

       //Test data  
       var xml = "your xml string";  
       var dataToCompress = Encoding.ASCII.GetBytes(xml);  
         
       //Compress it  
       byte[] compressedData;  
       using (var input = new MemoryStream(dataToCompress))  
       using (var output = new MemoryStream())  
       using (var gzip = new GZipStream(output, CompressionMode.Compress))  
       {  
           //Flush to ensure it is all written  
           input.CopyTo(gzip);  
           gzip.Flush();  
         
           //Rewind  
           output.Seek(0, SeekOrigin.Begin);  
           compressedData = output.ToArray();  
       };  
         
       //Decompress it  
       string uncompressedData;  
       using (var inputToRead = new MemoryStream(compressedData))  
       using (var gzipReader = new GZipStream(inputToRead, CompressionMode.Decompress))  
       using (var outputResult = new MemoryStream())  
       {  
           //Flush to ensure it is all written  
           gzipReader.CopyTo(outputResult);  
           gzipReader.Flush();  
         
           //Rewind  
           outputResult.Seek(0, SeekOrigin.Begin);  
         
           //Convert back to string  
           uncompressedData = Encoding.ASCII.GetString(outputResult.ToArray());  
       };  
         
       var areEqual = xml == uncompressedData;  
    

  3. Karol Szymański 101 Reputation points
    2021-07-27T13:58:33.28+00:00

    no my problem is not solved.

    var areEqual = xml == uncompressedData;

    Below is a screenshot
    screenshot.png

    Net Framework 4.7.2

    screenshot2.png

    After further attempts, whatever I type in xml, I have the same result for compressedData, and uncompressedData is empty.

    I have used code from this thread, will it be correct?
    https://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp


  4. Viorel 117.4K Reputation points
    2021-07-29T11:33:50.23+00:00

    Try this fix:

    string xml = "your xml string";
    
    // Compress it
    
    var dataToCompress = Encoding.UTF8.GetBytes( xml );
    byte[] compressedData;
    using( var output = new MemoryStream( ) )
    {
       using( var gzip = new GZipStream( output, CompressionMode.Compress ) )
       {
          gzip.Write( dataToCompress, 0, dataToCompress.Length );
       }
       compressedData = output.ToArray( );
    }
    
    // Decompress it
    
    string uncompressedData;
    using( var outputResult = new MemoryStream( ) )
    {
       using( var inputToRead = new MemoryStream( compressedData ) )
       {
          using( var gzipReader = new GZipStream( inputToRead, CompressionMode.Decompress ) )
          {
             gzipReader.CopyTo( outputResult );
          }
       }
       uncompressedData = Encoding.UTF8.GetString( outputResult.GetBuffer( ), 0, (int)outputResult.Length );
    }
    
    bool areEqual = xml == uncompressedData;
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.