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

首頁 > 編程 > C# > 正文

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

2020-01-24 00:19:57
字體:
供稿:網(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文件解壓的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 榆树市| 丹棱县| 裕民县| 光山县| 嫩江县| 治县。| 商洛市| 花垣县| 丰台区| 余干县| 建昌县| 时尚| 大同市| 永安市| 巨野县| 虞城县| 清流县| 拉孜县| 西乌珠穆沁旗| 利辛县| 建昌县| 宁武县| 灵山县| 伊宁县| 永新县| 宜宾市| 报价| 寿宁县| 浦北县| 北川| 寿阳县| 安平县| 通榆县| 黑龙江省| 蓝山县| 修武县| 镇康县| 孟津县| 修文县| 富川| 承德市|