GZipStream用于压缩和解压缩流...您不能用它来压缩和解压缩多个文件。实际上,你可以,但是你应该开发一些将这些文件合并成一个流的方法,并且知道如何使操作反转(从流中获取这些文件)。如果你有一个单一的文件,你会做这样的:
using (var outFile = File.Create(outputFileName))
{
using (GZipStream gzip = new GZipStream(download, CompressionMode.Decompress))
{
var buffer = new byte[4096];
var numRead = 0;
while ((numRead = gzip.Read(buffer, 0, buffer.Length)) != 0)
{
outFile.Write(buffer, 0, numRead);
}
}
}
Here是描述GZipStream如何可以用来压缩/解压缩多个文件的文章,但你可以看到,笔者开发了他自己的“ zip“格式来存储多个文件,并且使用GZipStream压缩各个流。
就你而言,如果你没有做这种压缩,你很可能会收到标准的压缩文件,在这种情况下,你可以使用名为SharpZipLib的库来解压缩你的内容。
下面是使用SharpZipLib
using (var s = new ZipInputStream(download)
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if(fileName == myFileName)
{
using (FileStream streamWriter = File.Create(theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
http://blogs.msdn.com/b/bclteam/archive/2006/05/10/592551.aspx – 2012-02-14 14:57:39
gzipstream有什么样的信息? – caesay 2012-02-14 15:00:51
存档包含三个压缩文件,我只对其中一个感兴趣。 – 2012-02-14 15:03:41