C#压缩文件为zip格式

需要ICSharpCode.SharpZipLib.dll,网上下载的到。

代码是从网上找来的:

  1 public class ZipClass
  2     {
  3         #region 加压
  4         /// <summary>
  5         /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
  6         /// </summary>
  7         /// <param name="dirPath"> 被压缩的文件夹夹路径 </param>
  8         /// <param name="zipFilePath"> 生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名 +.zip</param>
  9         /// <param name="err"> 出错信息</param>
 10         /// <returns> 是否压缩成功 </returns>
 11         static public bool ZipFile( string dirPath, string zipFilePath, out string err)
 12         {
 13             err = "";
 14             if (dirPath == string .Empty)
 15             {
 16                 err = "要压缩的文件夹不能为空! ";
 17                 return false ;
 18             }
 19             if (!Directory .Exists(dirPath))
 20             {
 21                 err = "要压缩的文件夹不存在! ";
 22                 return false ;
 23             }
 24             //压缩文件名为空时使用文件夹名+ zip
 25             if (zipFilePath == string .Empty)
 26             {
 27                 if (dirPath.EndsWith("\\" ))
 28                 {
 29                     dirPath = dirPath.Substring(0, dirPath.Length - 1);
 30                 }
 31                 zipFilePath = dirPath + ".zip";
 32             }
 33
 34             try
 35             {
 36                 string[] filenames = Directory .GetFiles(dirPath);
 37                 using (ZipOutputStream s = new ZipOutputStream(File .Create(zipFilePath)))
 38                 {
 39                     s.SetLevel(9);
 40                     byte[] buffer = new byte[4096];
 41                     foreach (string file in filenames)
 42                     {
 43                         ZipEntry entry = new ZipEntry( Path.GetFileName(file));
 44                         entry.DateTime = DateTime.Now;
 45                         s.PutNextEntry(entry);
 46                         using (FileStream fs = File.OpenRead(file))
 47                         {
 48                             int sourceBytes;
 49                             do
 50                             {
 51                                 sourceBytes = fs.Read(buffer, 0, buffer.Length);
 52                                 s.Write(buffer, 0, sourceBytes);
 53                             } while (sourceBytes > 0);
 54                         }
 55                     }
 56                     s.Finish();
 57                     s.Close();
 58                 }
 59             }
 60             catch (Exception ex)
 61             {
 62                 err = ex.Message;
 63                 return false ;
 64             }
 65             return true ;
 66         }
 67
 68         #endregion
 69
 70         #region 解压方法
 71         /// <summary>
 72         /// 功能:解压 zip格式的文件。
 73         /// </summary>
 74         /// <param name="zipFilePath"> 压缩文件路径 </param>
 75         /// <param name="unZipDir"> 解压文件存放路径 ,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 </param>
 76         /// <param name="err"> 出错信息</param>
 77         /// <returns> 解压是否成功 </returns>
 78         static public bool UnZipFile( string zipFilePath, string unZipDir, out string err)
 79         {
 80             err = "";
 81             if (zipFilePath == string .Empty)
 82             {
 83                 err = "压缩文件不能为空!";
 84                 return false ;
 85             }
 86             if (!File .Exists(zipFilePath))
 87             {
 88                 err = "压缩文件不存在!";
 89                 return false ;
 90             }
 91             //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
 92             if (unZipDir == string .Empty)
 93                 unZipDir = zipFilePath.Replace( Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
 94             if (!unZipDir.EndsWith("\\" ))
 95                 unZipDir += "\\";
 96             if (!Directory .Exists(unZipDir))
 97                 Directory.CreateDirectory(unZipDir);
 98
 99             try
100             {
101                 using (ZipInputStream s = new ZipInputStream(File .OpenRead(zipFilePath)))
102                 {
103
104                     ZipEntry theEntry;
105                     while ((theEntry = s.GetNextEntry()) != null)
106                     {
107                         string directoryName = Path .GetDirectoryName(theEntry.Name);
108                         string fileName = Path .GetFileName(theEntry.Name);
109                         if (directoryName.Length > 0)
110                         {
111                             Directory.CreateDirectory(unZipDir + directoryName);
112                         }
113                         if (!directoryName.EndsWith("\\"))
114                             directoryName += "\\";
115                         if (fileName != String .Empty)
116                         {
117                             using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
118                             {
119
120                                 int size = 2048;
121                                 byte[] data = new byte[2048];
122                                 while (true )
123                                 {
124                                     size = s.Read(data, 0, data.Length);
125                                     if (size > 0)
126                                     {
127                                         streamWriter.Write(data, 0, size);
128                                     }
129                                     else
130                                     {
131                                         break;
132                                     }
133                                 }
134                             }
135                         }
136                     } //while
137                 }
138             }
139             catch (Exception ex)
140             {
141                 err = ex.Message;
142                 return false ;
143             }
144             return true ;
145         } //解压结束
146         #endregion
147     }

C#压缩文件为zip格式的更多相关文章

  1. python实现压缩文件成zip格式

    实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...

  2. 如何在linux下解压缩rar和zip格式的文件压缩包

    转载:http://oldboy.blog.51cto.com/2561410/597515 使用apt-get安装:  sudo apt-get install  rar  zip rar使用: 将 ...

  3. Java生成压缩文件(zip、rar 格式)

    jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

  4. linux压缩文件命令-zip

    首先cd到要压缩文件的目录,然后使用zip命令压缩文件 zip -r importExcel.zip importExcel -r表示递归 zip [参数]  [打包后的文件名]  [打包的目录路径] ...

  5. mac终端命令加密压缩文件为zip包

    mac终端命令加密压缩文件为zip包,命令如下: zip -e ~/desktop/a.zip b.doc c.txt d.sql 注释:a.zip为加密后的文件 b.doc c.txt d.sql为 ...

  6. 下载zip格式文件(压缩Excel文件为zip格式)

    Mongodb配置文件参考这一篇:http://www.cnblogs.com/byteworld/p/5913061.html package util; import java.io.Buffer ...

  7. Linux 解压/压缩xxx.zip格式(unZip Zip的安装和使用)

    Linux系统没有自带的压缩解压工具:需要我们自己安装:当压缩包为.zip格式时,需要安装zip包 1.apt-get安装: apt-get install zip 2.yum安装: yum inst ...

  8. linux 学习笔记 显示压缩文件 gong.zip 的文件内容

    #zip -v gong zip zip info: xxx >删除压缩文件中俄smart.txt 文件 #zip -d gong.zip smart.txt deleting:smart.tx ...

  9. python 压缩文件为zip后删除原文件

    压缩.log 文件为zip后删除原文件 需要注意:本人作为小白,该脚本需要和.log在一起,后面有时间需要改正. #!/usr/local/python/bin/python #-*-coding=u ...

随机推荐

  1. 暂时告别Solr了

    好久没更新博客了,是因为最近一直忙于找工作,以及生活的一些琐碎事情. 新的工作虽然薪水不高,但是全新的项目还是让我蛮兴奋的. 现在从事的是数据工程师,又重新接触了Hadoop,Hive,Sqoop这些 ...

  2. LightOJ_1248 Dice (III)

    题目链接 题意: 给一个质地均匀的n的骰子, 求投掷出所有点数至少一次的期望次数. 思路: 这就是一个经典的邮票收集问题(Coupon Collector Problem). 投掷出第一个未出现的点数 ...

  3. 应用Java(环境变量)

    工作中,不一定非要设置Java环境变量 因为,IDE自身环境的设置,代替了系统环境变量 环境变量 系统的环境变量,相当于软件工作的环境.工作中,经常需要设置以下变量: Path ClassPath 自 ...

  4. 【产品体验】echo回声

    本人产品新人,学习中,希望大家用过该产品的给点意见,不吝赐教哦~~ 先来两张echo的界面图镇楼——        echo简介: “echo”是一款做声音社交的APP,在这里,你可以感受到声音无限的 ...

  5. tyvj 1934 高精度

    「Poetize3」Heaven Cow与God Bull From wwwwodddd     背景 Background __int64 ago,there's a heaven cow call ...

  6. Two shortest

    sgu185:http://acm.sgu.ru/problem.php?contest=0&problem=185 题意:找两条最短路径,没有边相交的最短路劲,并且输出路径. 题解:这一题和 ...

  7. Ext.Ajax.request同步请求

    导读: ajax分为2种,一种是同步,一种是异步同步:代码执行完了之后才执行后面的代码 异步:代码刚执行,后面的代码就马上接着执行了,不管前面的代码是否执行完异步的情况下,要获得返回信息,就需要在异步 ...

  8. 5种php加密工具zendGuard、ionCube、SourceCop、SourceGuardian、phpShield

    PHP做桌面应用的想法: 除去icudt55.dll,PHP7用7ZIP压缩后不足7MB,而PHP自带了SQLite和CLI HTTP Server,用户打开浏览器就能访问PHP开发的桌面应用.如果源 ...

  9. Eclipse下安装及配置maven项目管理工具

    ①eclipse下maven插件安装. 本地maven安装.环境变量配置完成后,打开eclipse,点击eclipse菜单栏Help->Eclipse Marketplace搜索关键字maven ...

  10. HDOJ 1995 汉诺塔V

    Problem Description 用1,2,-,n表示n个盘子,称为1号盘,2号盘,-.号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔 ...