C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
下面給大家介紹C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,具體代碼如下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Checksums; using System.Security.Cryptography; namespace zip壓縮與解壓 { public class ZipHelper { /// <summary> /// 壓縮單個文件 /// </summary> /// <param name="fileToZip">需壓縮的文件名</param> /// <param name="zipFile">壓縮后的文件名(文件名都是絕對路徑)</param> /// <param name="level">壓縮等級(0-9)</param> /// <param name="password">壓縮密碼(解壓是需要的密碼)</param> public static void ZipFile(string fileToZip, string zipFile, int level = 5, string password = "123") { if (!File.Exists(fileToZip)) throw new FileNotFoundException("壓縮文件" + fileToZip + "不存在"); using (FileStream fs = File.OpenRead(fileToZip)) { fs.Position = 0;//設(shè)置流的起始位置 byte[] buffer = new byte[(int)fs.Length]; fs.Read(buffer, 0, buffer.Length);//讀取的時候設(shè)置Position,寫入的時候不需要設(shè)置 fs.Close(); using (FileStream zfstram = File.Create(zipFile)) { using (ZipOutputStream zipstream = new ZipOutputStream(zfstram)) { zipstream.Password = md5(password);//設(shè)置屬性的時候在PutNextEntry函數(shù)之前 zipstream.SetLevel(level); string fileName = fileToZip.Substring(fileToZip.LastIndexOf('\\') + 1); ZipEntry entry = new ZipEntry(fileName); zipstream.PutNextEntry(entry); zipstream.Write(buffer, 0, buffer.Length); } } } } /// <summary> /// 壓縮多個文件目錄 /// </summary> /// <param name="dirname">需要壓縮的目錄</param> /// <param name="zipFile">壓縮后的文件名</param> /// <param name="level">壓縮等級</param> /// <param name="password">密碼</param> public static void ZipDir(string dirname, string zipFile, int level = 5, string password = "123") { ZipOutputStream zos = new ZipOutputStream(File.Create(zipFile)); zos.Password = md5(password); zos.SetLevel(level); addZipEntry(dirname, zos, dirname); zos.Finish(); zos.Close(); } /// <summary> /// 往壓縮文件里面添加Entry /// </summary> /// <param name="PathStr">文件路徑</param> /// <param name="zos">ZipOutputStream</param> /// <param name="BaseDirName">基礎(chǔ)目錄</param> private static void addZipEntry(string PathStr, ZipOutputStream zos, string BaseDirName) { DirectoryInfo dir = new DirectoryInfo(PathStr); foreach (FileSystemInfo item in dir.GetFileSystemInfos()) { if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)//如果是文件夾繼續(xù)遞歸 { addZipEntry(item.FullName, zos, BaseDirName); } else { FileInfo f_item = (FileInfo)item; using (FileStream fs = f_item.OpenRead()) { byte[] buffer = new byte[(int)fs.Length]; fs.Position = 0; fs.Read(buffer, 0, buffer.Length); fs.Close(); ZipEntry z_entry = new ZipEntry(item.FullName.Replace(BaseDirName, "")); zos.PutNextEntry(z_entry); zos.Write(buffer, 0, buffer.Length); } } } } /// <summary> /// 解壓多個文件目錄 /// </summary> /// <param name="zfile">壓縮文件絕對路徑</param> /// <param name="dirname">解壓文件目錄</param> /// <param name="password">密碼</param> public static void UnZip(string zfile, string dirname, string password) { if (!Directory.Exists(dirname)) Directory.CreateDirectory(dirname); using (ZipInputStream zis = new ZipInputStream(File.OpenRead(zfile))) { zis.Password = md5(password); ZipEntry entry; while ((entry = zis.GetNextEntry()) != null) { var strArr = entry.Name.Split('\\');//這邊判斷壓縮文件里面是否存在目錄,存在的話先創(chuàng)建目錄后繼續(xù)解壓 if (strArr.Length > 2) Directory.CreateDirectory(dirname + @"\" + strArr[1]); using (FileStream dir_fs = File.Create(dirname + entry.Name)) { int size = 1024 * 2; byte[] buffer = new byte[size]; while (true) { size = zis.Read(buffer, 0, buffer.Length); if (size > 0) dir_fs.Write(buffer, 0, size); else break; } } } } } private static string md5(string pwd) { var res = ""; MD5 md = MD5.Create(); byte[] s = md.ComputeHash(Encoding.Default.GetBytes(pwd)); for (int i = 0; i < s.Length; i++) res = res + s[i].ToString("X"); return res; } } }
調(diào)用函數(shù)如下:
static void Main(string[] args) { var str = @"\學(xué)籍導(dǎo)入模板.xls"; //var arr=str.Split('\\'); var filePath = @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓\File\學(xué)籍導(dǎo)入模板.xls"; //ZipHelper.ZipFile(filePath, @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓\File\test.zip", 6, "123"); var dirPath = @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓"; //ZipHelper.ZipDir(dirPath + @"\File", dirPath + @"\File.zip", 6, "huage"); ZipHelper.UnZip(dirPath + @"\File.zip", dirPath + @"\test", "huage"); Console.ReadKey(); }
效果圖如下:
總結(jié)
以上所述是小編給大家介紹的C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:C# 通過反射初探ORM框架的實(shí)現(xiàn)原理(詳解)
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)的二維數(shù)組排序算法示例
本文標(biāo)題:C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5319.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
本欄相關(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ī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法