免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
C#壓縮解壓zip 文件
 
[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">生成壓縮文件的路徑,為空則默認(rèn)與被壓縮文件夾同一級目錄,名稱為:文件夾名+.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("http://"))  
  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">解壓文件存放路徑,為空時默認(rèn)與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</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.         //解壓文件夾為空時默認(rèn)與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾  
  98.         if (unZipDir == string.Empty)  
  99.             unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));  
  100.         if (!unZipDir.EndsWith("http://"))  
  101.             unZipDir += "http://";  
  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("http://"))  
  120.                         directoryName += "http://";  
  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.     }//解壓結(jié)束  
  152.     #endregion  
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
文件夾操作及zip的子文件讀取,解壓-文件下載
NW.JS教程(二) 本地文件的操作及process進(jìn)程 | 顧陌 | Blog
c++統(tǒng)計文件行數(shù)
golang判斷是文件還是文件夾
[轉(zhuǎn)]C# FTP上傳文件及文件夾至服務(wù)器代碼
使用commons
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服