日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

分享一个ASP.NET 文件压缩解压类 C#

發布時間:2025/7/14 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分享一个ASP.NET 文件压缩解压类 C# 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需要引用一個ICSharpCode.SharpZipLib.dll?

?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using ICSharpCode.SharpZipLib.Zip; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using System.Web; namespace Mvc51Hiring.Common.Tool {/// <summary>
/// 作者:來自網格
/// 修改人:sunkaixaun/// 壓縮和解壓文件 /// </summary> public class ZipClass{/// <summary> /// 所有文件緩存 /// </summary> List<string> files = new List<string>();/// <summary> /// 所有空目錄緩存 /// </summary> List<string> paths = new List<string>();/// <summary> /// 壓縮單個文件根據文件地址/// </summary> /// <param name="fileToZip">要壓縮的文件</param> /// <param name="zipedFile">壓縮后的文件全名</param> /// <param name="compressionLevel">壓縮程度,范圍0-9,數值越大,壓縮程序越高</param> /// <param name="blockSize">分塊大小</param> public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize){if (!System.IO.File.Exists(fileToZip))//如果文件沒有找到,則報錯 {throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");}FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);FileStream zipFile = File.Create(zipedFile);ZipOutputStream zipStream = new ZipOutputStream(zipFile);ZipEntry zipEntry = new ZipEntry(fileToZip);zipStream.PutNextEntry(zipEntry);zipStream.SetLevel(compressionLevel);byte[] buffer = new byte[blockSize];int size = streamToZip.Read(buffer, 0, buffer.Length);zipStream.Write(buffer, 0, size);try{while (size < streamToZip.Length){int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);zipStream.Write(buffer, 0, sizeRead);size += sizeRead;}}catch (Exception ex){GC.Collect();throw ex;}zipStream.Finish();zipStream.Close();streamToZip.Close();GC.Collect();}/// <summary> /// 壓縮目錄(包括子目錄及所有文件) /// </summary> /// <param name="rootPath">要壓縮的根目錄</param> /// <param name="destinationPath">保存路徑</param> /// <param name="compressLevel">壓縮程度,范圍0-9,數值越大,壓縮程序越高</param> public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel){GetAllDirectories(rootPath);/* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//檢查路徑是否以"\"結尾 { rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是則去掉末尾的"\" } *///string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到當前路徑的位置,以備壓縮時將所壓縮內容轉變成相對路徑。 string rootMark = rootPath + "\\";//得到當前路徑的位置,以備壓縮時將所壓縮內容轉變成相對路徑。 Crc32 crc = new Crc32();ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression foreach (string file in files){FileStream fileStream = File.OpenRead(file);//打開壓縮文件 byte[] buffer = new byte[fileStream.Length];fileStream.Read(buffer, 0, buffer.Length);ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));entry.DateTime = DateTime.Now;// set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fileStream.Length;fileStream.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;outPutStream.PutNextEntry(entry);outPutStream.Write(buffer, 0, buffer.Length);}this.files.Clear();foreach (string emptyPath in paths){ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");outPutStream.PutNextEntry(entry);}this.paths.Clear();outPutStream.Finish();outPutStream.Close();GC.Collect();}/// <summary> /// 多文件打包下載/// </summary> public void DwonloadZip(string[] filePathList, string zipName){MemoryStream ms = new MemoryStream();byte[] buffer = null;var context = HttpContext.Current;using (ICSharpCode.SharpZipLib.Zip.ZipFile file = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(ms)){file.BeginUpdate();file.NameTransform = new MyNameTransfom();//通過這個名稱格式化器,可以將里面的文件名進行一些處理。默認情況下,會自動根據文件的路徑在zip中創建有關的文件夾。foreach (var it in filePathList){file.Add(context.Server.MapPath(it));}file.CommitUpdate();buffer = new byte[ms.Length];ms.Position = 0;ms.Read(buffer, 0, buffer.Length);}context.Response.AddHeader("content-disposition", "attachment;filename=" + zipName);context.Response.BinaryWrite(buffer);context.Response.Flush();context.Response.End();}/// <summary> /// 取得目錄下所有文件及文件夾,分別存入files及paths /// </summary> /// <param name="rootPath">根目錄</param> private void GetAllDirectories(string rootPath){string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目錄 foreach (string path in subPaths){GetAllDirectories(path);//對每一個字目錄做與根目錄相同的操作:即找到子目錄并將當前目錄的文件名存入List }string[] files = Directory.GetFiles(rootPath);foreach (string file in files){this.files.Add(file);//將當前目錄中的所有文件全名存入文件List }if (subPaths.Length == files.Length && files.Length == 0)//如果是空目錄 {this.paths.Add(rootPath);//記錄空目錄 }}/// <summary> /// 解壓縮文件(壓縮文件中含有子目錄) /// </summary> /// <param name="zipfilepath">待解壓縮的文件路徑</param> /// <param name="unzippath">解壓縮到指定目錄</param> /// <returns>解壓后的文件列表</returns> public List<string> UnZip(string zipfilepath, string unzippath){//解壓出來的文件列表 List<string> unzipFiles = new List<string>();//檢查輸出目錄是否以“\\”結尾 if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false){unzippath += "\\";}ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(unzippath);string fileName = Path.GetFileName(theEntry.Name);//生成解壓目錄【用戶解壓到硬盤根目錄時,不需要創建】 if (!string.IsNullOrEmpty(directoryName)){Directory.CreateDirectory(directoryName);}if (fileName != String.Empty){//如果文件的壓縮后大小為0那么說明這個文件是空的,因此不需要進行讀出寫入 if (theEntry.CompressedSize == 0)break;//解壓文件到指定的目錄 directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);//建立下面的目錄和子目錄 Directory.CreateDirectory(directoryName);//記錄導出的文件 unzipFiles.Add(unzippath + theEntry.Name);FileStream streamWriter = File.Create(unzippath + 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;}}streamWriter.Close();}}s.Close();GC.Collect();return unzipFiles;}}public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform{#region INameTransform 成員public string TransformDirectory(string name){return null;}public string TransformFile(string name){return Path.GetFileName(name);}#endregion} }

  

轉載于:https://www.cnblogs.com/sunkaixuan/p/4943248.html

總結

以上是生活随笔為你收集整理的分享一个ASP.NET 文件压缩解压类 C#的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 在线香蕉视频 | 爱爱视频免费看 | 在线观看av大片 | 99视频在线观看免费 | 成人蜜桃视频 | 古典武侠av | aaaaav| 第一区免费在线观看 | 99热国产在线观看 | 欧美a天堂| 今天高清视频在线观看视频 | 亚洲天堂2014 | 人人做人人爽 | 成人免费在线观看网站 | 国产三级a| 国产精品日韩欧美 | www.色com | 亚洲四区 | 久久国产成人 | 91精品国产欧美一区二区成人 | www.在线 | 顶级尤物极品女神福利视频 | 亚洲日本在线观看 | av不卡在线观看 | 伊人久久一区二区三区 | 制服丝袜中文字幕在线 | 亚洲欧美一区二区三区 | 午夜精品久久久久久久99热浪潮 | 日日骚av一区二区 | 美女网站在线看 | 深夜精品福利 | 午夜视频久久 | 日韩在线第一 | 国产另类精品 | 手机在线看片你懂的 | 国产在线一区二区三区 | 成人免费毛片aaaaaa片 | 日韩欧美视频一区 | 麻豆91茄子在线观看 | 欧美精品一区二区三区在线 | 无码精品在线视频 | 第一色影院| 97影音| 亚洲最大福利 | 狠狠插狠狠操 | av网在线 | 色老头影视| 免费在线观看a级片 | 在线观看国产亚洲 | av直播在线观看 | 在线观看色视频 | ,国产精品国产三级国产 | 国语精品 | av男人天堂网| 五月天天色 | 国产精品欧美一区喷水 | 亚洲一级黄色片 | 亚洲免费在线视频 | 一区二区自拍偷拍 | 免费看一级 | 国产手机在线 | 少妇人妻好深好紧精品无码 | 亚洲视频在线免费 | 国产三区视频 | 成品短视频泡芙 | 打开免费观看视频在线播放 | 日韩激情在线观看 | 久久久精品国产免费爽爽爽 | 国产一级啪啪 | 亚洲av无码一区二区二三区软件 | 美女扒开粉嫩尿口 | www日| 污污的网站在线免费观看 | 婷婷五月在线视频 | 国产高清一区在线观看 | 成人欧美一区二区三区小说 | 国内自拍欧美 | 求欧美精品网址 | 国产综合福利 | 日韩不卡av| 美女被娇喘流出白 | www.久久精品.com| 韩国激情呻吟揉捏胸视频 | 国产又粗又硬又黄的视频 | 亚洲制服一区二区 | 国产精品香蕉国产 | 91视频 - 8mav| 中文文字幕一区二区三三 | 奇米成人网 | 极品美女扒开粉嫩小泬 | 在线免费三级 | 精品一区二区三区不卡 | 日韩精品123 | 女儿的朋友5中汉字晋通话 欧美成人免费高清视频 | 中日一级片 | 日噜噜夜噜噜 | 好妞色妞国产在线视频 | 国产日韩在线观看一区 | 日韩国产成人无码av毛片 |