c# 文件壓縮zip或?qū)ip文件解壓的方法
1.必須Dll:
ICSharpCode.SharpZipLib.dll??蓮腘utget程序包中獲取。
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è)參考,也希望大家多多支持我們。
上一篇:c#可以創(chuàng)建任意控件的拖動(dòng)方法
欄 目:C#教程
本文標(biāo)題:c# 文件壓縮zip或?qū)ip文件解壓的方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5243.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#路徑,文件,目錄及IO常見操作匯總
- 01-10winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法
- 01-10C#讀寫INI文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置