如何使用System.IO.Compression读取/写入ZIP文件?
我知道有可用于处理 ZIP 文件的库 。 而且,您也可以使用 Windows 内置的功能来处理 ZIP 文件。
但是,我想知道是否有人已经弄清楚如何使用 .NET 中 System.IO.Compression 命名空间中内置的工具来读取/写入 ZIP 文件? 或者,仅使用这个命名空间是不可能的吗?
更新:我似乎有人评论说 System.IO.Packaging 命名空间对此也可能有用。 有谁知道具体该怎么做?
I know there are libraries out there for working with ZIP files. And, you can alternatively use the functionality built into Windows for working ZIP files.
But, I'm wondering if anyone has worked out how to use the tools built into the System.IO.Compression namespace within .NET for reading/writing ZIP files? Or, is it not possible using only this namespace?
UPDATED: I've seem someone comment that the System.IO.Packaging namespace might be usefull with this also. Does anyone know exactly how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
MSDN 有一个完整的示例 http://msdn.microsoft.com/en-us/library/system .io.packaging.zippackage.aspx 使用 ZipPackage 类。 需要 .NET 3.5。
MSDN has a complete example http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx using the ZipPackage class. Requires .NET 3.5.
戴夫,非常好!! 我不知道里面有那个。
现在我知道要寻找什么,我可以找到一篇文章,其中包含有关如何使用它的小代码示例:
http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx
在相关说明中,我还发现 DotNetZip 项目 看起来非常易于使用。
Dave, very nice!! I didn't know that was in there.
Now that I know what to look for, I was able to find an article with a small code sample on how to use it:
http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx
On a related note, I also found the DotNetZip project that looks extremely easy to use.
您将需要使用第三方库,例如 http://www.codeplex.com/DotNetZip ,而不是尝试使用 GZipStream 或 DeflateStream 来读取 zip 文件。
.NET 中的 ***Stream 类可以让您读取或写入压缩的字节流。 这些类不读取或写入 zip 文件。 zip 文件是由“信封”或标头包围的压缩数据。 将其视为元数据 - 它包括文件名、时间戳、CRC 和一堆其他内容。 **Stream 类仅生成压缩数据流,不知道如何生成或使用元数据,这在 PKWare 维护的 PKZip 格式规范中进行了描述。
DotNetZip 等第三方库处理 ZIP 存档中的元数据。 他们可能会也可能不会使用 System.IO. Compression.DeflateStream() 类来生成压缩的字节流。 例如,在以前的版本中,DotNetZip 使用内置的 DeflateStream。 从 v1.7 开始,DotNetZip 包含了自己的 DeflateStream,它比 .NET Framework 中提供的 DeflateStream 更高效。 作为一个额外的好处,DotNetZip 中嵌入的 DeflateStream 允许在 .NET Compact Framework 2.0 上使用 DotNetZip,该框架缺少 System.IO.Compression.DeflateStream。 (它是在 Compact Framework 3.5 中添加的)
如果您有更多问题,DotNetZip 站点上有一个很好的论坛。
C# 代码示例:
You will want to use a third-party library, like http://www.codeplex.com/DotNetZip, rather than trying to use GZipStream or DeflateStream to read a zip file.
The ***Stream classes in .NET can let you read or write compressed streams of bytes. These classes DO NOT read or write zip files. The zip file is compressed data surrounded by "an envelope" or a header. Think of it as metadata - it includes the name of the file, timestamp, CRC, and a bunch of other stuff. The **Stream classes produce only the stream of compressed data, and do not know how to produce or consume the metadata, which is described in the PKZip format specification maintained by PKWare.
Third party libraries like DotNetZip handle the metadata in a ZIP archive. They may or may not use the System.IO.Compression.DeflateStream() class to produced the compressed stream of bytes. In previous releases, for example, DotNetZip used the built-in DeflateStream. As of v1.7, DotNetZip includes its own DeflateStream which is more efficient than the one shipped in the .NET Framework. As an added benefit, the embedded DeflateStream in DotNetZip allows DotNetZip to be used on the .NET Compact Framework 2.0, which lacks a System.IO.Compression.DeflateStream. (it was added in Compact Framework 3.5)
There's a good forum on the DotNetZip site if you have more questions.
Example C# code:
从 .NET 4.5 开始,Microsoft 提供了 ZipArchive 类添加到
System.IO.Compression
命名空间。 不过,与该命名空间中的其他类(如 GZipStream 和 Deflate 流)不同,ZipArchive 需要对 System.IO 的引用。 Compression.dll 程序集。它使用起来相当简单,上面的 MSDN 链接提供了一些很好的示例。
此外,Microsoft 还创建了 Microsoft Compression NuGet 包,它添加了对 < code>ZipArchive 以及 Windows Phone 8 和其他可移植类库的相关类
Since .NET 4.5, Microsoft has offered the ZipArchive class to the
System.IO.Compression
namespace. Unlike other classes in that namespace, though, likeGZipStream
andDeflate
stream, theZipArchive
requires a reference to the System.IO.Compression.dll assembly.It's fairly simple to use, and the link above to MSDN provides some good examples.
Additionally, Microsoft has created the Microsoft Compression NuGet package, which adds support for
ZipArchive
and related classes to Windows Phone 8 and other Portable Class Libraries我知道这是一个较旧的线程,但我想指出 .NET 4.5 对 system.io.compression 添加了广泛的改进。 现在,它可以很好地操作 .zip 文件,甚至可以将 zip 中的各个文件公开为能够读取和写入的流,而无需提取和压缩文件的步骤。
An older thread, I know, but I'd like to point out that .NET 4.5 added extensive improvements to system.io.compression. It can now manipulate .zip files quite well, even down to exposing individual files within the zip as streams capable or read and write without the step of extracting and compressing the files.
使用打包命名空间的 .NET Framework Zip/UnZip 工具
.NET Framework Zip / UnZip Tool Using the Packaging Namespace
是的,我过去用过它。 我对 DataSet 进行了一次子类化以支持持久化本身输出到文件(通过 ReadXML /WriteXML 方法)。 作为额外的好处,我决定允许它根据需要进行压缩(大家都应该知道,这对于像 XML 这样的文本数据非常有效)。
我使用了 GZipStream 类(它是我当时的理解是相关的 DeflateStream 只是没有标题信息的 GZip,或者类似的信息 - 我相信有人可以纠正我这一点)。 它的工作原理非常简单,只需在另一个流的顶部搭载即可,因此您可以使用 GZipStream 代替它。 就我而言,它是搭载在 FileStream 上的。
给定一个 MemoryStream 来填充以下输出
myDataSet.WriteXML()
,我做了类似以下的事情:其中
Save()
和Pump()
是如下简单方法:Yes, I've used it in the past. I sub-classed DataSet once to support persisting itself out to a file (via the ReadXML/WriteXML method). As an added bonus, I decided to allow it to be, optionally, compressed if desired (this, as you all should already know, is extremely effective with textual data like XML).
I used the GZipStream class (it was my understanding at the time that the related DeflateStream was merely GZip without header information, or some such — I'm sure someone could correct me on this). It works quite simply by piggy-backing on top of another stream and thus you then use the GZipStream in its place. In my case, it was piggy-backing on a FileStream.
Given a MemoryStream to be filled with the output of
myDataSet.WriteXML()
, I did something like the following:Where
Save()
andPump()
are simple methods like the following:一个旧线程,但我想分享我发现的最简单的解决方案,仅在一行代码中使用 System.IO.Compression (读/写):
An old thread but I want to share the most simple solution that I found using only System.IO.Compression in one line of code (read/write):