乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      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.     /// 功能:壓縮文件(暫時(shí)只壓縮文件夾下一級(jí)目錄中的文件,文件夾及其子級(jí)被忽略)  
      13.     /// </summary>  
      14.     /// <param name="dirPath">被壓縮的文件夾夾路徑</param>  
      15.     /// <param name="zipFilePath">生成壓縮文件的路徑,為空則默認(rèn)與被壓縮文件夾同一級(jí)目錄,名稱(chēng)為:文件夾名+.zip</param>  
      16.     /// <param name="err">出錯(cuò)信息</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.         //壓縮文件名為空時(shí)使用文件夾名+.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">解壓文件存放路徑,為空時(shí)默認(rèn)與壓縮文件同一級(jí)目錄下,跟壓縮文件同名的文件夾</param>  
      82.     /// <param name="err">出錯(cuò)信息</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.         //解壓文件夾為空時(shí)默認(rèn)與壓縮文件同一級(jí)目錄下,跟壓縮文件同名的文件夾  
      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  

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約