加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – GZipStream没有读取整个文件

发布时间:2020-12-15 07:46:20 所属栏目:百科 来源:网络整理
导读:我有一些代码可以下载gzip压缩文件并对其进行解压缩.问题是,我无法解压缩整个文件,它只读取前4096个字节,然后再读取大约500个字节. Byte[] buffer = new Byte[4096];int count = 0;FileStream fileInput = new FileStream("input.gzip",FileMode.Open,FileAc
我有一些代码可以下载gzip压缩文件并对其进行解压缩.问题是,我无法解压缩整个文件,它只读取前4096个字节,然后再读取大约500个字节.
Byte[] buffer = new Byte[4096];
int count = 0;
FileStream fileInput = new FileStream("input.gzip",FileMode.Open,FileAccess.Read,FileShare.Read);
FileStream fileOutput = new FileStream("output.dat",FileMode.Create,FileAccess.Write,FileShare.None);
GZipStream gzipStream = new GZipStream(fileInput,CompressionMode.Decompress,true);

// Read from gzip steam
while ((count = gzipStream.Read(buffer,buffer.Length)) > 0)
{
    // Write to output file
    fileOutput.Write(buffer,count);
}

// Close the streams
...

我检查了下载的文件;它在压缩时为13MB,包含一个XML文件.我手动解压缩了XML文件,内容就在那里.但是,当我使用此代码执行此操作时,它只输出XML文件的最开头.

任何人都有任何想法为什么会发生这种情况?

解决方法

编辑

尽量不要打开GZipStream:

GZipStream gzipStream = new GZipStream(fileInput,false);

要么

GZipStream gzipStream = new GZipStream(fileInput,CompressionMode.Decompress);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读