C# DeflateStream CopyTo不写入任何内容,也不抛出任何异常
我基本上是直接从msdn复制了这个代码示例,只做了一些微小的更改。C# DeflateStream CopyTo不写入任何内容,也不抛出任何异常,c#,.net,compression,zip,C#,.net,Compression,Zip,我基本上是直接从msdn复制了这个代码示例,只做了一些微小的更改。CopyTo方法正在默默地失败,我不知道为什么。什么会导致这种行为?它被传递给一个78 KB的压缩文件夹,里面只有一个文本文件。返回的FileInfo对象指向一个0 KB的文件。没有抛出异常 public static FileInfo DecompressFile(FileInfo fi) { // Get the stream of the source file. using
CopyTo
方法正在默默地失败,我不知道为什么。什么会导致这种行为?它被传递给一个78 KB的压缩文件夹,里面只有一个文本文件。返回的FileInfo
对象指向一个0 KB的文件。没有抛出异常
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文件中使用。您提到的使用DeflateStream
创建单个压缩文件,然后解压缩它们
尽管zip文件可能使用相同的算法(对此不确定),但它们不仅仅是单个文件的压缩版本。zip文件是一个可以容纳许多文件和/或文件夹的容器
如果您可以使用.NETFramework4.5,我建议您使用新的or类。如果您必须使用早期的框架版本,则可以使用免费库(如或)。您正在尝试读取和解压缩哪种类型的文件?一个.gz,一个.zip,还是别的什么?@huntharo它是一个.zip。不幸的是,我在.net4上。我最初试图使用
Zipfile
类,但意识到这不是一个选项。我可能不得不使用其中一个第三方库,但我不愿意这样做。