国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

c# 文件壓縮zip或?qū)ip文件解壓的方法

2019-10-29 21:03:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.必須Dll:

ICSharpCode.SharpZipLib.dll。可從Nutget程序包中獲取。

2.壓縮文件

/// <summary>/// 壓縮文件成zip/// </summary>/// <param name="fileZip">壓縮成zip文件的絕對(duì)路徑</param>/// <param name="fileName">被壓縮指定文件的名字</param>/// <param name="zipFilePath"></param>/// <returns></returns>public bool CreateZipFile(string fileZip,string fileName, string zipFilePath)  {   bool isZip = false;   if (!Directory.Exists(zipFilePath))   {    Logger.Info($"Cannot find directory {zipFilePath}", false, "FileToZip");    return isZip;   }   try   {    string[] filenames = Directory.GetFiles(zipFilePath);    using (ZipOutputStream s = new ZipOutputStream(File.Create(fileZip)))    {     s.SetLevel(9); // 壓縮級(jí)別 0-9     //s.Password = "123"; //Zip壓縮文件密碼     byte[] buffer = new byte[4096]; //緩沖區(qū)大小     foreach (string file in filenames.ToList())     {      if (file== zipFilePath+fileName)//指定被壓縮文件的絕對(duì)路徑      {       ZipEntry entry = new ZipEntry(Path.GetFileName(file));       entry.DateTime = DateTime.Now;       s.PutNextEntry(entry);       using (FileStream fs = File.OpenRead(file))       {        int sourceBytes;        do        {         sourceBytes = fs.Read(buffer, 0, buffer.Length);         s.Write(buffer, 0, sourceBytes);        } while (sourceBytes > 0);        fs.Close();        fs.Dispose();       }       break;      }     }     s.Finish();     s.Close();     isZip = true;    }   }   catch (Exception ex)   {    Logger.Info($"Exception during processing {0}", false, "FileToZip");   }   return isZip;  }

3.將zip文件解壓

/// <summary>/// 解壓文件/// </summary>/// <param name="zipFilePath">壓縮文件的絕對(duì)路徑</param> public void UnZipFile(string zipFilePath)  {   if (!File.Exists(zipFilePath))   {    Logger.Info($"Cannot find file {zipFilePath}", false, "FileToZip");    return;   }   using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))   {    ZipEntry theEntry;    while ((theEntry = s.GetNextEntry()) != null)    {     string directoryName = Path.GetDirectoryName(theEntry.Name);     string fileName = Path.GetFileName(theEntry.Name);     // create directory     if (directoryName?.Length > 0)     {      Directory.CreateDirectory(directoryName);     }     if (!string.IsNullOrEmpty(fileName))     {      using (FileStream streamWriter = File.Create(theEntry.Name))      {       int size = 2048;       byte[] data = new byte[2048];       while (true)       {        size = s.Read(data, 0, data.Length);        if (size > 0)        {         streamWriter.Write(data, 0, size);        }        else        {         break;        }       }      }     }    }   }  }

4.其它:其中的Logger是Log4的用法。

以上這篇c# 文件壓縮zip或?qū)ip文件解壓的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 会宁县| 大姚县| 临朐县| 平山县| 六安市| 哈巴河县| 宝丰县| 喀喇| 焦作市| 佳木斯市| 高安市| 常熟市| 惠安县| 辉县市| 盐池县| 武义县| 锦屏县| 深泽县| 时尚| 泰来县| 陆川县| 五华县| 高州市| 古丈县| 安阳县| 衢州市| 杭锦旗| 克什克腾旗| 乡城县| 大田县| 六安市| 大邑县| 濉溪县| 聂荣县| 舟山市| 常宁市| 昭平县| 自治县| 绥江县| 普安县| 兴山县|