首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GZipStream和解压

GZipStream和解压
EN

Stack Overflow用户
提问于 2009-10-17 15:58:42
回答 2查看 55.9K关注 0票数 16

我有应该做压缩的代码:

代码语言:javascript
复制
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();

我认为是这样的,但我想要解压上面压缩的内容。我确实是这么想的:

代码语言:javascript
复制
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();

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-10-17 08:07:16

我的第一个想法是你还没有关闭csStream。如果你使用using,这会自动发生。由于gzip对数据进行缓冲,因此可能会丢失一些数据。

其次,不要递增offset;这是缓冲区(而不是流)中的偏移量。离开时间为0:

代码语言:javascript
复制
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);
    }
}
票数 22
EN

Stack Overflow用户

发布于 2018-01-25 19:04:05

我有两个方法,就像James Roland提到的那样。

代码语言:javascript
复制
private static byte[] Compress(HttpPostedFileBase file)
{
    using var to = new MemoryStream();
    using var gZipStream = new GZipStream(to, CompressionMode.Compress);
    file.InputStream.CopyTo(gZipStream);
    gZipStream.Flush();
    return to.ToArray();
}

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

但是,我使用的是upload

代码语言:javascript
复制
Request.Files[0] 

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

代码语言:javascript
复制
$"data:image/gif;base64,{ToBase64String(Decompress(img))}";
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1581694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档