zoukankan      html  css  js  c++  java
  • 【转】使用SharpZipLib实现zip压缩

    使用国外开源加压解压库ICSharpCode.SharpZipLib实现加压,该库的官方网站为
    http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

    使用体验:可以照着例子实现简单的加压解压,可以加压一个文件夹中的所有文件,但没有提供加压子文件夹的说明。
    目前网上的一些代码有的无法加压空文件夹,有的加压了用rar解不开,这是一点需要改进的。
    但如果只需要加压文件夹第一级子目录中的“文件”(不包括文件夹和子目录)的情况,使用这个库是很方便的。而且是正常zip格式。
    比.Net提供的GZipStream类强在它可以按照标准zip格式加压多个文件,而GZipStream没有提供加压多个文件的方法,需要自己定义,
    这样解压也只有使用自己的程序才可以,通用性方面不如SharpZipLib。

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

    需要添加对SharpZipLib的引用:

    1 using ICSharpCode.SharpZipLib.Zip;

    【转自】:http://www.cnblogs.com/tuyile006/archive/2008/04/25/1170894.html

  • 相关阅读:
    JavaScript词法结构
    【python】类变量、实例变量
    把pandas dataframe转为list方法
    list 删除一个元素的三种做法--python
    Web.config中rewite 节点引起的500.19错误
    extjs让按钮可用或者不可用
    VS2010启动奔溃
    迟来的年终总结
    Nginx配置多个server
    RestSharp的简单用法
  • 原文地址:https://www.cnblogs.com/zcwvictor-2012/p/3197943.html
Copyright © 2011-2022 走看看