我有一个要压缩的内存流:
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;
}
}
}应该很简单的。您为它提供了要压缩的流。这些方法压缩流并返回它。太棒了。
然而,我没有得到压缩的流回来。我得到的是在开头和结尾添加了大约20个字节的流,它们似乎与zip库有关。但是中间的数据是完全未压缩的(值相同的256个字节范围,等等)。我试着把水平提高到9级,但没什么改变。
为什么我的溪流不压缩?
发布于 2017-03-31 22:31:51
您自己通过以下方法将原始流复制到输出流中:
Utils.StreamCopy(unZippedChunk, zippedChunk, new byte[4096]);您应该复制到zipOutputStream,而不是:
StreamCopy(unZippedChunk, zipOutputStream, new byte[4096]);附带注意:不要使用自定义复制流方法--使用默认的方法:
unZippedChunk.CopyTo(zipOutputStream);https://stackoverflow.com/questions/43150555
复制相似问题