您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“c#如何打包文件實(shí)現(xiàn)解壓縮”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“c#如何打包文件實(shí)現(xiàn)解壓縮”這篇文章吧。
首先要引用一下類庫(kù):using Ionic.Zip;這個(gè)類庫(kù)可以到網(wǎng)上下載。
下面對(duì)類庫(kù)使用的封裝方法:
得到指定的輸入流的ZIP壓縮流對(duì)象
/// <summary> /// 得到指定的輸入流的ZIP壓縮流對(duì)象【原有流對(duì)象不會(huì)改變】 /// </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解壓流對(duì)象
/// <summary> /// 得到指定的字節(jié)數(shù)組的ZIP解壓流對(duì)象 /// 當(dāng)前方法僅適用于只有一個(gè)壓縮文件的壓縮包,即方法內(nèi)只取壓縮包中的第一個(gè)壓縮文件 /// </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方法中會(huì)操作ms,后續(xù)使用時(shí)必須先將Stream位置歸零,否則會(huì)導(dǎo)致后續(xù)讀取不到任何數(shù)據(jù) // 返回該Stream對(duì)象之前進(jìn)行一次位置歸零動(dòng)作 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è)置編碼,解決壓縮文件時(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//目錄下的文件都?jí)嚎s到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è)置編碼,解決解壓文件時(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í)現(xiàn)解壓縮”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。