c#打包文件解壓縮的實(shí)例
首先要引用一下類庫:using Ionic.Zip;這個類庫可以到網(wǎng)上下載。
下面對類庫使用的封裝方法:
得到指定的輸入流的ZIP壓縮流對象
/// <summary> /// 得到指定的輸入流的ZIP壓縮流對象【原有流對象不會改變】 /// </summary> /// <param name="sourceStream"></param> /// <returns></returns> public static Stream ZipCompress(Stream sourceStream, string entryName = "zip") { MemoryStream compressedStream = new MemoryStream(); if (sourceStream != null) { long sourceOldPosition = 0; try { sourceOldPosition = sourceStream.Position; sourceStream.Position = 0; using (ZipFile zip = new ZipFile()) { zip.AddEntry(entryName, sourceStream); zip.Save(compressedStream); compressedStream.Position = 0; } } catch { } finally { try { sourceStream.Position = sourceOldPosition; } catch { } } } return compressedStream; }
得到指定的字節(jié)數(shù)組的ZIP解壓流對象
/// <summary> /// 得到指定的字節(jié)數(shù)組的ZIP解壓流對象 /// 當(dāng)前方法僅適用于只有一個壓縮文件的壓縮包,即方法內(nèi)只取壓縮包中的第一個壓縮文件 /// </summary> /// <param name="sourceStream"></param> /// <returns></returns> public static Stream ZipDecompress(byte[] data) { Stream decompressedStream = new MemoryStream(); if (data != null) { try { MemoryStream dataStream = new MemoryStream(data); using (ZipFile zip = ZipFile.Read(dataStream)) { if (zip.Entries.Count > 0) { zip.Entries.First().Extract(decompressedStream); // Extract方法中會操作ms,后續(xù)使用時必須先將Stream位置歸零,否則會導(dǎo)致后續(xù)讀取不到任何數(shù)據(jù) // 返回該Stream對象之前進(jìn)行一次位置歸零動作 decompressedStream.Position = 0; } } } catch { } } return decompressedStream; }
壓縮ZIP文件
/// <summary> /// 壓縮ZIP文件 /// 支持多文件和多目錄,或是多文件和多目錄一起壓縮 /// </summary> /// <param name="list">待壓縮的文件或目錄集合</param> /// <param name="strZipName">壓縮后的文件名</param> /// <param name="IsDirStruct">是否按目錄結(jié)構(gòu)壓縮</param> /// <returns>成功:true/失?。篺alse</returns> public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct) { try { using (ZipFile zip = new ZipFile(Encoding.Default))//設(shè)置編碼,解決壓縮文件時中文亂碼 { foreach (string path in list) { string fileName = Path.GetFileName(path);//取目錄名稱 //如果是目錄 if (Directory.Exists(path)) { if (IsDirStruct)//按目錄結(jié)構(gòu)壓縮 { zip.AddDirectory(path, fileName); } else//目錄下的文件都壓縮到Zip的根目錄 { zip.AddDirectory(path); } } if (File.Exists(path))//如果是文件 { zip.AddFile(path,"imges"); } } zip.Save(strZipName);//壓縮 return true; } } catch (Exception) { return false; } }
解壓ZIP文件
/// <summary> /// 解壓ZIP文件 /// </summary> /// <param name="strZipPath">待解壓的ZIP文件</param> /// <param name="strUnZipPath">解壓的目錄</param> /// <param name="overWrite">是否覆蓋</param> /// <returns>成功:true/失?。篺alse</returns> public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite) { try { ReadOptions options = new ReadOptions(); options.Encoding = Encoding.Default;//設(shè)置編碼,解決解壓文件時中文亂碼 using (ZipFile zip = ZipFile.Read(strZipPath, options)) { foreach (ZipEntry entry in zip) { if (string.IsNullOrEmpty(strUnZipPath)) { strUnZipPath = strZipPath.Split('.').First(); } if (overWrite) { entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解壓文件,如果已存在就覆蓋 } else { entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解壓文件,如果已存在不覆蓋 } } return true; } } catch (Exception) { return false; } }
以上動圖由“圖斗羅”提供
這篇c#打包文件解壓縮的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#中結(jié)構(gòu)體定義并轉(zhuǎn)換字節(jié)數(shù)組詳解
本文標(biāo)題:c#打包文件解壓縮的實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5419.html
您可能感興趣的文章
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)多線程寫入同一個文件的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#路徑,文件,目錄及IO常見操作匯總
- 01-10C#中Socket通信用法實(shí)例詳解


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery