简体   繁体   English

C#解压缩的问题

[英]Problem with C# Decompression

Have some data in a sybase image type column that I want to use in a C# app. 在sybase图像类型列中包含一些我想在C#应用程序中使用的数据。 The data has been compressed by Java using the java.util.zip package. Java使用java.util.zip包压缩了数据。 I wanted to test that I could decompress the data in C#. 我想测试我可以解压缩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. 这给了我一个2479长度的压缩字节[]。
Then I pass this to a seemingly standard C# decompression method: 然后我把它传递给一个看似标准的C#解压缩方法:

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. msgLength的值是1503501432,这似乎超出了范围。 The original document should be in the range of 5K -50k. 原始文件应在5K-50k的范围内。 Anyway when I use that value to create "buffer" not surprisingly I get an OutOfMemoryException. 无论如何,当我使用该值创建“缓冲区”时,我得到一个OutOfMemoryException。 What is happening? 怎么了? Jim 吉姆

The Java compress method is as follows: Java压缩方法如下:

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. 由于我无法看到您的java代码,我只能猜测您正在将数据压缩为zip文件流。 Therefore it will obviously fail if you are trying to decompress that stream with a gzip decompression in c#. 因此,如果您尝试使用c#中的gzip解压缩来解压缩该流,则显然会失败。 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 ). 您可以将Java代码更改为gzip压缩( 此处位于页面底部),或者使用适当的库(例如SharpZipLib )解压缩c#中的zip文件流。

Update 更新

Ok now, I see you are using deflate for the compression in java. 好了,我看到你在java中使用deflate进行压缩。 So, obviously you have to use the same algorithm in c#: System.IO.Compression.DeflateStream 所以,显然你必须在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;
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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