System.IO.Compression是.Net 2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。

从[1] 下载动态库,然后在工程里Add Reference,把ICSharpCode.SharpZipLib.dll加进去。

在代码来创建一个zip包的例子如下(摘自ziplib sample code)

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.GZip;

把指定目录下的所有文件压缩到一个zip包里

private void ZipTheReports()
        {
            string year = System.DateTime.Today.Year.ToString();
            string month = System.DateTime.Today.Month.ToString();
            string days = System.DateTime.Today.Day.ToString();

string[] filenames = Directory.GetFiles(curDir);

string zipFileName = "Rplan Report " + year + month + days + ".zip";
            string zipAbsPath = this.curDir + zipFileName;

zipRelativePath = zipFileName;

Crc32 crc = new Crc32();
            ZipOutputStream s = new ZipOutputStream(File.Create(zipAbsPath)); // 指定zip文件的绝对路径,包括文件名

s.SetLevel(6); // 0 - store only to 9 - means best compression

foreach (string file in filenames)
            {
                FileStream fs = File.OpenRead(file); // 文件的绝对路径,包括文件名

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(extractFileName(file)); //这里ZipEntry的参数必须是相对路径名,表示文件在zip文档里的相对路径
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information

// about the size and crc should be stored in the header

// if it is not set it is automatically written in the footer.

// (in this case size == crc == -1 in the header)

// Some ZIP programs have problems with zip files that don't store

// the size and crc in the header.

entry.Size = fs.Length;
                fs.Close();

crc.Reset();

crc.Update(buffer);

entry.Crc = crc.Value;

s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);

}

s.Finish();
            s.Close();

}

// e.g. convert  c:\\temp\test.xls to test.xls

private string extractFileName(string filePath)
        {

int index1 = filePath.LastIndexOf("\\");

string fileName =

filePath.Substring(index1+1);

return fileName;
}

Reference

1.ZipLib
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
A port of zlib library to C#. Its license allows developers to include this library in commercial, closed-source applications.

2. System.IO.Compression Namespace
http://msdn2.microsoft.com/en-us/library/system.io.compression.aspx

DeflateStream Class
http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx

3.Compression Application Sample
http://msdn2.microsoft.com/en-us/library/ywf6dxhx.aspx

This sample demonstrates compression capabilities available in the .NET Framework. It builds a Windows Forms application that employs the GZipStream and DeflateStream types to compress and decompress files. The sample also introduces several types that are new in the .NET Framework version 2.0.

4. Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#

http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx

转载于:https://www.cnblogs.com/yuquanlaobo/archive/2007/01/17/622851.html

Using ZipLib to create a Zip File in C#相关推荐

  1. ionic build Android错误记录 error in opening zip file

    0.写在前头 运行 :cordova requirements Requirements check results for android: Java JDK: installed 1.8.0 An ...

  2. Ubuntu20.04安装zabbix以及Cannot create the configuration file解决

    添加zabbix的扩展源 echo 'deb http://mirrors.aliyun.com/zabbix/zabbix/5.0/ubuntu/dists/ focal main' >> ...

  3. PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0

    为什么80%的码农都做不了架构师?>>>    不同电脑,相同的代码怎么就上传不了图片:本人用的是PHP upload上传项目下的retime文件夹 服务器突然出现这种提示,无法上传 ...

  4. electron builder 打包错误 cannot unpack electron zip file 解决方案

    electron builder 打包错误 cannot unpack electron zip file 解决方案 参考文章: (1)electron builder 打包错误 cannot unp ...

  5. Eclipse europa 更新时 Error retrieving feature.xml. [error in opening zip file]

    Eclipse europa 更新时 Error retrieving "feature.xml". [error in opening zip file]的解决 2009-06- ...

  6. 解决java web项目导入后出现的问题 ---cannot be read or is not a valid ZIP file

    http://www.cnblogs.com/haimingwey/archive/2012/07/17/2595473.html 今天导入以前的2个web项目后会出现以下2个错误: 1. Archi ...

  7. 解决安装下载好的whl库包时,报错:zipfile.BadZipFile: File is not a zip file

    文章目录: 1 问题说明 2 解决问题 1 问题说明 我是在Jetson NX(arm架构)上安装tensorflow的时候,首先把tensorflow的安装包下载下来 1.tensorflow==1 ...

  8. 【错误记录】Android Studio 编译报错 ( Gradle 下载错误导致 Failed to open zip file 报错 )

    文章目录 一.报错信息 二.解决方案 1.修改 gradle-wrapper.properties 配置 ( 失效解决方案 ) 2.手动干预 Gradle 目录 ( 推荐 ) 3.手动下载 Gradl ...

  9. Error opening zip file or JAR manifest missing的解决方法

    错误描述: MyEclipse中启动Tomcat(debug)的时候就出现Error starting Tomcat : A configuration error occured during st ...

最新文章

  1. poj1129Channel Allocation
  2. 想客户之所想 华为全栈云加速行业云化创新
  3. CSS综合征病例,医药-churg-strauss 综合征 (css) 变应性嗜酸性肉芽肿.ppt
  4. LLBLGen 关于类型转换
  5. 手动实现SPring中的AOP(2)
  6. yum 出错,提示Segmentation Fault (core Dumped) 的解决办法
  7. SWF反编译神器ASV2013功能展示(下)
  8. 支付宝集五福1月19日开启 超1000个商家机构齐发福卡
  9. Java保存class文件,[转载]Class文件在JVM中如何存储
  10. ubuntu 安装node.js
  11. IPIP.net状告阿里云抄袭侵权
  12. 毕设论文-word格式问题
  13. 如何让必应bing收录我的网站
  14. 使用Java处理键盘输入(DTMF)
  15. JAVA学习第一步-配置JAVA开发环境和学习资料
  16. 注册时要求获取手机短信码的实现(java)
  17. 《大型网站技术架构》——第一章 大型网站架构演化
  18. Sklearn之KMeans算法
  19. 这5个电脑软件提高工作效率1000%,装机必备
  20. CCNA初学(第三课)

热门文章

  1. oracle 中的几天后,几年后
  2. MySQL子查询介绍
  3. hystrix相关配置
  4. MyBatis 实际使用案例-typeHandlers【重点】
  5. 完成DI 依赖注入功能
  6. 初探Spring MVC 请求处理流程
  7. 复制文件的异常处理【应用】
  8. SpringBoot2.x整合redis实战讲解
  9. 反射_Class对象功能_获取Constructor
  10. 加密与安全 - Java加密与安全