分享

C#压缩解压zip 文件

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约