以下内容来自:blogs.msdn.com

.NET System.IO.Compression and zip files

.NET Zip Library

.NET 2.0 does Compression

There's a new namespace in the .NET Framework base class library for .NET 2.0, called System.IO.Compression. It has classes called DeflateStream and GZipStream.

These classes are streams; they're useful for compressing a stream of bytes as you transfer it, for example across the network to a cooperating application (a peer, or a client, whatever). The DeflateStream implements the Deflate algorithm, see the IETF's RFC 1951. "DEFLATE Compressed Data Format Specification version 1.3." The GZipStream is an elaboration of the Deflate algorithm, and adds a cyclic-redundancy-check. For more on GZip, see the IETF RFC 1952, "Gzip".

Gzip has been done

The GZip format described in RFC 1952 is also used by the popular gzip utility included in many *nix distributions. The Base Class Library team at Microsoft previously published example source code for a simple utility that behaves just like the *nix gzip, but is written in .NET and based on the GZipStream class. This simple utility can interoperate with the *nix gzip, can read and write .gz files.

What about .zip files?

As a companion to that example, enclosed here as an attachment (see the bottom of this post) is an example class than can read and write zip archives. It is packaged as a re-usable library, as well as a couple of companion example command-line applications that use the library. The example apps are useful on their own, for example for just zipping up a directory quickly, from within a script or a command-prompt. But the library will be useful also, for including zip capability into arbitrary applications. For example, you could include a zip task in a msbuild session, or into a smart-client GUI application. I've included both the binaries and source code here.

This is the class diagram for the ZipFile class, and the ZipEntry class, as generated by Visual Studio 2005. The ZipFile is the main class.

If you don't quite grok all that notation, I will point out a few highlights. The ZipFile itself supports a generic IEnumerable interface. What this means is you can enumerate the ZipEntry's within the ZipFile using a foreach loop. Makes usage really simple. ( Implementing that little trick is also dead-simple, thanks to the new-for-2.0 support for iterators in C# 2.0, and the "yield return" statement.)

Using the ZipFile class

You can extract all files from an existing .zip file by doing this:

 1        ZipFile zip = ZipFile.Read("MyZip.zip" );
 2
 3        foreach (ZipEntry e in  zip)
 4
 5         {
 6
 7            e.Extract("NewDirectory");
 8
 9        }
10

Of course, you don't have want to extract the files, you can just fiddle with the properties on the ZipEntry things in the collection. Creating a new .zip file is also simple:

1       ZipFile zip =   new  ZipFile( " MyNewZip.zip " ); 
2
3       zip.AddDirectory( " My Pictures " ,  true );  //  AddDirectory recurses subdirectories
4
5       zip.Save(); 
6

You can add a directory at a time, as shown above, and you can add individual files as well. It seems to be pretty fast, though I haven't benchmarked it. It doesn't compress as much as winzip; This library is at the mercy of the DeflateStream class, and that class doesn't support multiple levels of compression.

Hmmm, What About Intellectual Property?

I am no lawyer, but it seems to me the ZIP format is PKware's intellectual property. PKWare has some text in their zip spec which states:

PKWARE is committed to the interoperability and advancement of the .ZIP format. PKWARE offers a free license for certain technological aspects described above under certain restrictions and conditions. However, the use or implementation in a product of certain technological aspects set forth in the current APPNOTE, including those with regard to strong encryption or patching, requires a license from PKWARE. Please contact PKWARE with regard to acquiring a license.

I checked with pkware for more on that.  I described what I was doing with this example, and got a nice email reply from Jim Peterson at PKWare, who wrote:

From the description of your intended need, no license would be necessary for the compression/decompression features you plan to use.

Which would mean, anyone could use this example without a license. But like I said, I am no lawyer.

Later,

-Dino

[Update 11 April 2005 1036am US Pacific time]: After a bit of testing it seems that there are some anomalies with the DeflateStream class in .NET. One of them is, it performs badly with already compressed data. For example, imagine that you use the zipDir.exe tool included in the attachment here, to zip a directory that itself contains a large zip file. The DeflateStream in .NET can actually Inflate the size of the stream. The output is still valid, but it isn't compressing as you;d like.

The base class library team is aware of this anomaly and is considering it. If you'd like to weigh in on this behavior, and I encourage you to do so if you value this class, use the Product Feedback Center, see here.

Published Wednesday, April 05, 2006 11:38 AM by DotNetInterop

Filed under: Interop, Zip

Attachment(s): DotnetZipLib.zip

转载于:https://www.cnblogs.com/yangbeibei/archive/2006/10/19/533452.html

利用System.IO.Compression实现文件压缩和解压缩相关推荐

  1. 利用System.IO.Compression操作压缩文件

    引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...

  2. 在C#中利用SharpZipLib进行文件的压缩和解压缩

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  3. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  4. 利用WinRar压缩和解压缩文件

    今天的rar shell只是一个简单应用,rar.exe和winrar.exe语法都是一样的. 对rar而言,用rar.exe最好,不需要判断winrar在哪里,而且非常小,因为没有界面,所有压缩选项 ...

  5. Unity3D压缩和解压缩文件

    今天试了试,在Unity不能用C# IO类里的压缩和解压缩,看了论坛里的一些讨论,就想着能不能用第三方的压缩和解压缩动态库. 于是乎就下了一个Ionic.Zip.dll.然后,然后就没有然后了. Co ...

  6. java 解压与压缩代码_Java实现多文件压缩和解压缩代码详解

    Java实现多文件压缩和解压缩代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...

  7. java.util.zip_[Java 基础] 使用java.util.zip包压缩和解压缩文件

    Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作. Zi ...

  8. Java使用winrar压缩和解压缩文件

    Java使用winrar压缩和解压缩文件 2015-08-17| 发布: | 浏览: 740 |保存PDF Winrar可以使用命令行进行压缩和解压缩,如: 将D:/aa.doc 压缩为:aa.rar ...

  9. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的很好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,需要导入该包. 使用zipoutputs ...

最新文章

  1. OpenGL 三角形要点总结
  2. 阿里云在应用扩缩容下遇到的挑战与选型思考
  3. Ext 组件的一些操作
  4. 14岁女孩模仿视频中易拉罐做爆米花意外身亡 博主办公室小野回应...
  5. 【java】java 命令 Unable to open socket file: target process not responding or HotSpot VM not loaded
  6. ios学习:Xcode工具
  7. vs2002 vs2003 可能存在的问题以及解决办法!
  8. 秀恩爱分得快-模拟题
  9. iOS 之NSOperation(一)
  10. python烟花代码
  11. Android inflate解析
  12. 使用Spring Environment遇到的问题, 如读取到配置不是自己实际想要的
  13. 机器学习系列之coursera week 1 Introduction 以及模型评估
  14. PCI总线及发展历程(详细)总结
  15. 互联网金融中的分期贷款利率剖析
  16. 全国地理信息相关单位名单汇总2010
  17. Uber及Cruise开发自驾车可视化工具名为Worldview的React函式库
  18. 百万员工不上班也能领工资!原来黑客破解了保险公司的考勤系统
  19. 取色器(Snipaste) 截图 贴图 取色
  20. java和ansys,HumanResourceManSys

热门文章

  1. 光学设计CAD:基于ZEMAX的望远物镜优化设计
  2. 最全的机器学习模型训练全流程
  3. Scientific Paper Recommendation: A Survey2020翻译
  4. QT 中 QCustomplot的用法
  5. c语言休眠函数 vs,编纂C语言跨平台函数(以清屏和休眠函数为例)
  6. 基于SSM校园宿舍管理系统
  7. mac终端命令显示/隐藏文件夹
  8. 4月14,一个特殊的日子
  9. hash碰撞的概率推导(生日攻击生日问题)
  10. LPNet for Image Derain