1

我正在使用 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 是假的。
有任何想法吗?
谢谢你的前面。

4

1 回答 1

3

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

于 2011-04-06T21:46:27.420 回答