ICSharpCode的反向动作... C#中的InflaterInputStream

时间:2011-03-21 10:49:11

标签: c# compression sharpziplib

我正在使用ICSharpCode.SharpZipLib.dll来压缩和解压缩数据 我有以下代码执行数据膨胀:

public static byte[] ZLibDecompress(byte[] zLibCompressedBuffer)
{
    byte[] resBuffer = null;

    MemoryStream mInStream = new MemoryStream(zLibCompressedBuffer);
    MemoryStream mOutStream = new MemoryStream(zLibCompressedBuffer.Length);
    InflaterInputStream infStream = new InflaterInputStream(mInStream);

    mInStream.Position = 0;

    try
    {
        byte[] tmpBuffer = new byte[zLibCompressedBuffer.Length];
        int read = 0;

        do
        {
            read = infStream.Read(tmpBuffer, 0, tmpBuffer.Length);
            if (read > 0)
                mOutStream.Write(tmpBuffer, 0, read);

        } while (read > 0);

        resBuffer = mOutStream.ToArray();
    }
    finally
    {
        infStream.Close();
        mInStream.Close();
        mOutStream.Close();
    }

    return resBuffer;
}

此代码实际上有效,现在我想压缩结果。
所以这是我的代码:

public static byte[] ZLibCompress(byte[] buffer)
{
    byte[] resBuffer = null;

    MemoryStream mOutStream = new MemoryStream(buffer.Length);
    DeflaterOutputStream defStream = new DeflaterOutputStream(mOutStream);

    try
    {
        defStream.Write(buffer, 0, buffer.Length);
        defStream.Flush();
        defStream.Finish();

        resBuffer = mOutStream.ToArray();
    }
    finally
    {
        defStream.Close();
        mOutStream.Close();
    }

    return resBuffer;
}

但两个函数的两个结果数组并不相等:

byte[] unCompBuffer = ZipUtils.ZLibDecompress(zLibBuffer);
byte[] compBuffer = ZipUtils.ZLibCompress(unCompBuffer);
bool eq = compBuffer.SequenceEqual(zLibBuffer);

eq是假的。
任何想法?
谢谢你的提前。

1 个答案:

答案 0 :(得分:3)

压缩缓冲区来自哪里?它是否也被SharpZipLib压缩了?具有相同的压缩级别和选项?如果没有,您不应期望解压缩/重新压缩的缓冲区等于原始缓冲区...