DeflateStream CopyTo不写任何内容并且不抛出异常 [英] DeflateStream CopyTo writes nothing and throws no exceptions
问题描述
我基本上从msdn直接复制了这个代码示例,只做了一些最小的修改。 CopyTo
方法默默地失败,我不知道为什么。什么会导致这种行为?它正在传递一个78 KB的压缩文件夹,里面有一个文本文件。返回的 FileInfo
对象指向一个0 KB文件。
I've basically copied this code sample directly from msdn with some minimal changes. The CopyTo
method is silently failing and I have no idea why. What would cause this behavior? It is being passed a 78 KB zipped folder with a single text file inside of it. The returned FileInfo
object points to a 0 KB file. No exceptions are thrown.
public static FileInfo DecompressFile(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension,
// for example "doc" from report.doc.cmp.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length
- fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
// work around for incompatible compression formats found
// here http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
inFile.ReadByte();
inFile.ReadByte();
using (DeflateStream Decompress = new DeflateStream(inFile,
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(outFile);
return new FileInfo(origName);
}
}
}
}
推荐答案
在评论中,你说你正在解压缩一个zip文件。 DeflateStream
类不能像这样在zip文件上使用。您提到的 MSDN示例使用
In a comment you say that you are trying to decompress a zip file. The DeflateStream
class can not be used like this on a zip file. The MSDN example you mentioned uses DeflateStream
to create individual compressed files and then uncompresses them.
虽然zip文件可能使用相同的算法(不确定),但它们是不只是单个文件的压缩版本。 zip文件是一个容纳多个文件和/或文件夹的容器。
Although zip files might use the same algorithm (not sure about that) they are not just compressed versions of a single file. A zip file is a container that can hold many files and/or folders.
如果你可以使用.NET Framework 4.5,我建议使用新的 ZipFile
或 ZipArchive
类。如果您必须使用早期的框架版本,则可以使用免费库(如 DotNetZip 或 SharpZipLib )。
If you can use .NET Framework 4.5 I would suggest to use the new ZipFile
or ZipArchive
class. If you must use an earlier framework version there are free libraries you can use (like DotNetZip or SharpZipLib).
这篇关于DeflateStream CopyTo不写任何内容并且不抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!