C# GZipStream 和解压

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1581694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:01:53  来源:igfitidea点击:

GZipStream and decompression

c#gzipstreamcompression

提问by

I have code that should do the compression:

我有应该进行压缩的代码:

FileStream fs = new FileStream("g:\gj.txt", FileMode.Open);
FileStream fd = new FileStream("g:\gj.zip", FileMode.Create);
GZipStream csStream = new GZipStream(fd, CompressionMode.Compress);

byte[] compressedBuffer = new byte[500];
int offset = 0;
int nRead;

nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
while (nRead > 0)
{
    csStream.Write(compressedBuffer, offset, nRead);
    offset = offset + nRead;
    nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
}

fd.Close();
fs.Close();

and I think it does, but I want to decompress what was compressed the way above. I do somethink like that:

我认为确实如此,但我想解压缩以上述方式压缩的内容。我确实是这样想的:

FileStream fd = new FileStream("g:\gj.new", FileMode.Create);
FileStream fs = new FileStream("g:\gj.zip", FileMode.Open);
GZipStream csStream = new GZipStream(fs, CompressionMode.Decompress);

byte[] decompressedBuffer = new byte[500];
int offset = 0;
int nRead;

nRead=csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
while (nRead > 0)
{
    fd.Write(decompressedBuffer, offset, nRead);
    offset = offset + nRead;
    nRead = csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
}

fd.Close();
fs.Close();

and here it doesn't... I've got nRead = 0 befeore entering the loop... What I do wrong?? The test file I use is the simpliest TEXT file (size: 104 bytes)...

在这里它没有......我在进入循环之前有nRead = 0......我做错了什么??我使用的测试文件是最简单的 TEXT 文件(大小:104 字节)...

采纳答案by Marc Gravell

My first thought is that you haven't closed csStream. If you use usingthis happens automatically. Since gzip buffers data, you could be missing some.

我的第一个想法是你还没有关闭csStream。如果你使用using这会自动发生。由于 gzip 缓冲数据,您可能会遗漏一些。

Secondly; don't increment offset; that is the offset in the buffer(not the stream). Leave at 0:

其次; 不要增加offset; 这是缓冲区(不是流)中的偏移量。在 0 离开:

using (Stream fs = File.OpenRead("gj.txt"))
using (Stream fd = File.Create("gj.zip"))
using (Stream csStream = new GZipStream(fd, CompressionMode.Compress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = fs.Read(buffer, 0, buffer.Length))> 0)
    {
        csStream.Write(buffer, 0, nRead);
    }
}

using (Stream fd = File.Create("gj.new.txt"))
using (Stream fs = File.OpenRead("gj.zip"))
using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fd.Write(buffer, 0, nRead);
    }
}

回答by Stephen Himes

The two methods I have are like James Roland mentioned.

我拥有的两种方法就像 James Roland 提到的那样。

private static byte[] Compress(HttpPostedFileBase file)
{
    using var to = new MemoryStream();
    using var from = new GZipStream(to, CompressionMode.Compress);
    file.InputStream.CopyTo(from);
    return to.ToArray();
}

private static byte[] Decompress(byte[] compressed)
{
    using var to = new MemoryStream();
    using var from = new MemoryStream(img);
    using var compress = new GZipStream(from, CompressionMode.Decompress);
    compress.CopyTo(to);
    return to.ToArray();
}

However, I'm using an upload with

但是,我正在使用上传

Request.Files[0] 

then compress and save in the db. Then I pull the img out, decompress and set a src with

然后压缩并保存在数据库中。然后我把 img 拉出来,解压并设置一个 src

$"data:image/gif;base64,{ToBase64String(Decompress(img))}";