2 Replies - 1055 Views - Last Post: 09 July 2019 - 05:12 AM Rate Topic: -----

#1 KC10513   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-October 17

Decompress using gzip

Posted 09 July 2019 - 01:17 AM

Good Day,

I have a server App that receives compressed data from an Ipad and i would like to decompress that data and read it but for some reasons am getting error: "Found invalid data while decoding" here's the function am using to decompress.
 public string Decompress(byte[] data)
        {
            byte[] gZipBuffer = data;
                       
            using (var memoryStream = new MemoryStream())
            {
                int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
                memoryStream.Write(gZipBuffer, 0, gZipBuffer.Length);

                var buffer = new byte[dataLength];

                memoryStream.Position = 0;
                using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    gZipStream.Read(buffer, 0, dataLength);
                }

                return Encoding.UTF8.GetString(buffer);
            }
        }


I also attached the file with sample data (to be converted in byte and pass into the Decompress function)
Any help please;

Attached File(s)

  • Attached File  JobD.txt (1.64K)
    Number of downloads: 7


Is This A Good Question/Topic? 0
  • +

Replies To: Decompress using gzip

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7054
  • View blog
  • Posts: 23,982
  • Joined: 05-May 12

Re: Decompress using gzip

Posted 09 July 2019 - 04:54 AM

It doesn't look like those bytes contain GZip data. Here's the first few byte of that file:
Attached Image

According the RFC 1952, the first two bytes of the GZIP file needs to be 1F 8B, but that doesn't start with those ID bytes. Even assuming that you skipped the first 4 bytes for that dataLength that you seem to be reading on line 13, there are still no ID bytes.

Any which way, if the first 4 bytes is the uncompressed data size, you should probably skip those when try to decompress the rest of the data because those bytes are not part of the gzip format.
Was This Post Helpful? 3
  • +
  • -

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2380
  • View blog
  • Posts: 4,515
  • Joined: 30-May 10

Re: Decompress using gzip

Posted 09 July 2019 - 05:12 AM

> I have a server App that receives compressed data from an Ipad
How?
Are you super sure that the transport is error free?

Your "failure to decompress" suggests you've failed to take into account something on the compression side.

I'm concerned that what you think you're decompressing isn't what you think it should be.
In particular, pretending that binary files are text files risks all sorts of corruption possibilities.
hd /tmp/JobD.txt
00000000  03 0d 0a 33 62 31 0d 0a  1f ef bf bd 08 00 00 00  |...3b1..........|
00000010  00 00 00 13 ef bf bd 5d  53 ef bf bd 40 14 ef bf  |.......][email protected]|
00000020  bd ef bf bd ef bf bd ef  bf bd 4e ef bf bd 40 20  |[email protected] |
00000030  04 ef bf bd ef bf bd 22  7e 54 28 ef bf bd d8 96  |......."~T(.....|
00000040  ef bf bd ef bf bd ef bf  bd 0e 75 1c 67 6c ef bf  |..........u.gl..|
00000050  bd 76 ef bf bd ef bf bd  3a 0e 1d 5b 7f 03 ef bf  |.v......:..[....|
00000060  bd 24 6f ef bf bd 75 ef  bf bd ef bf bd 35 6c 0e  |.$o...u......5l.|
00000070  ef bf bd ef bf bd 3b 2f  12 e6 b0 bb ef bf bd 26  |......;/.......&|
00000080  ef bf bd 7b ef bf bd ec  8f 93 ef bf bd ef bf bd  |...{............|
00000090  3a ef bf bd ef bf bd 0c  5c ef bf bd 6f ef bf bd  |:.......\...o...|
000000a0  29 59 06 5f ef bf bd ef  bf bd 5e 19 ef bf bd 42  |)Y._......^....B|
000000b0  1f ef bf bd ef bf bd 50  ef bf bd ef bf bd 70 76  |.......P......pv|
000000c0  40 5d 3a 16 4f ef bf bd  7c 32 ef bf bd ef bf bd  |@]:.O...|2......|
000000d0  1c ef bf bd 2d ef bf bd  01 7d ef bf bd ef bf bd  |....-....}......|
000000e0  ef bf bd 51 6b ef bf bd  ef bf bd ef bf bd 13 39  |...Qk..........9|
000000f0  62 4d 63 0d 5a 10 ef bf  bd ef bf bd 17 59 5a 0b  |bMc.Z........YZ.|


Compressed data should appear to be 'random', in that all byte values are equally probable.
But your data has a massive excess of just three particular bytes - namely bd, bf and ef.

If you want fidelity, then make a hex-dump of the data, then attach that as your text file.

> int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
For example, all those "0d 0a" look like artefacts of trying to treat binary as text.
Either way, 03 0d 0a 33 in either little-endian or big-endian is one hell of a large number, and doesn't look like a valid length.

Have you run the code in the debugger, put a breakpoint on the above line and actually examined what it is you think you have?
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1