简体   繁体   中英

Problem with C# Decompression

Have some data in a sybase image type column that I want to use in a C# app. The data has been compressed by Java using the java.util.zip package. I wanted to test that I could decompress the data in C#. So I wrote a test app that pulls it out of the database:

byte[] bytes = (byte[])reader.GetValue(0);  

This gives me a compressed byte[] of 2479 length.
Then I pass this to a seemingly standard C# decompression method:

public static byte[] Decompress(byte[] gzBuffer)
{
    MemoryStream ms = new MemoryStream();
    int msgLength = BitConverter.ToInt32(gzBuffer, 0);
    ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
    byte[] buffer = new byte[msgLength];

    ms.Position = 0;
    GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
    zip.Read(buffer, 0, buffer.Length);

    return buffer;
}  

The value for msgLength is 1503501432 which seems way out of range. The original document should be in the range of 5K -50k. Anyway when I use that value to create "buffer" not surprisingly I get an OutOfMemoryException. What is happening? Jim

The Java compress method is as follows:

public byte[] compress(byte[] bytes) throws Exception {
    byte[] results = new byte[bytes.length];
    Deflater deflator = new Deflater();
    deflater.setInput(bytes);
    deflater.finish();
    int len = deflater.deflate(results);
    byte[] out = new byte[len];
    for(int i=0; i<len; i++) {
        out[i] = results[i];
    }
    return(out);
}  

As I cant see your java code, I can only guess you are compressing your data to a zip file stream. Therefore it will obviously fail if you are trying to decompress that stream with a gzip decompression in c#. Either you change your java code to a gzip compression (Example here at the bottom of the page), or you decompress the zip file stream in c# with an appropriate library (eg SharpZipLib ).

Update

Ok now, I see you are using deflate for the compression in java. So, obviously you have to use the same algorithm in c#: System.IO.Compression.DeflateStream

public static byte[] Decompress(byte[] buffer)
{
    using (MemoryStream ms = new MemoryStream(buffer))
    using (Stream zipStream = new DeflateStream(ms, 
                          CompressionMode.Decompress, true))
    {
        int initialBufferLength = buffer.Length * 2;

        byte[] buffer = new byte[initialBufferLength];
        bool finishedExactly = false;
        int read = 0;
        int chunk;

        while (!finishedExactly && 
              (chunk = zipStream.Read(buffer, read, buffer.Length - read)) > 0)
        {
            read += chunk;

            if (read == buffer.Length)
            {
                int nextByte = zipStream.ReadByte();

                // End of Stream?
                if (nextByte == -1)
                {
                    finishedExactly = true;
                }
                else
                {
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
        }
        if (!finishedExactly)
        {
            byte[] final = new byte[read];
            Array.Copy(buffer, final, read);
            buffer = final;
        }
    }

    return buffer;
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM