C# SharpZipLib不压缩内存流

C# SharpZipLib不压缩内存流,c#,zip,zipfile,C#,Zip,Zipfile,我有一个要压缩的内存流: public static MemoryStream ZipChunk(MemoryStream unZippedChunk) { MemoryStream zippedChunk = new MemoryStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(zippedChunk); zipOutputStream.SetLevel(3);

我有一个要压缩的内存流:

public static MemoryStream ZipChunk(MemoryStream unZippedChunk) {

        MemoryStream zippedChunk = new MemoryStream();

        ZipOutputStream zipOutputStream = new ZipOutputStream(zippedChunk);
        zipOutputStream.SetLevel(3);

        ZipEntry entry = new ZipEntry("name");
        zipOutputStream.PutNextEntry(entry);

        Utils.StreamCopy(unZippedChunk, zippedChunk, new byte[4096]);
        zipOutputStream.CloseEntry();

        zipOutputStream.IsStreamOwner = false;
        zipOutputStream.Close();
        zippedChunk.Close();

        return zippedChunk;
    }

public static void StreamCopy(Stream source, Stream destination, byte[] buffer, bool bFlush = true) {
        bool flag = true;
        while (flag) {

            int num = source.Read(buffer, 0, buffer.Length);
            if (num > 0) {                    
                destination.Write(buffer, 0, num);
            }

            else {

                if (bFlush) {                        
                    destination.Flush();
                }

                flag = false;
            }
        }           
    }
这应该很简单。您为它提供了一个要压缩的流。这些方法压缩流并返回它。太好了

然而,我没有得到压缩流回来。我得到的是在开始和结束时添加了大约20ish字节的流,这似乎与zip库有关。但是中间的数据是完全未压缩的(256字节具有相同值等的范围)。我试着把等级提高到9级,但没有任何改变


为什么我的流没有压缩?

您可以通过以下方式将原始流复制到输出流中:

Utils.StreamCopy(unZippedChunk, zippedChunk, new byte[4096]);
您应该复制到
zipOutputStream

StreamCopy(unZippedChunk, zipOutputStream, new byte[4096]);
旁注:不要使用自定义复制流方法-使用默认方法:

unZippedChunk.CopyTo(zipOutputStream);

我知道我错过了一些愚蠢的东西,我就是看不见。谢谢知道了。将修复它。@AlexeiLevenkov在本例中返回的流被释放(通过
zippedChunk.Close();
),因此未存储的位置不是最大的问题:)