c#SharpZipLib实现压缩

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值