您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C#如何使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C#如何使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能”這篇文章吧。
具體代碼如下所示:
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;//設置流的起始位置 byte[] buffer = new byte[(int)fs.Length]; fs.Read(buffer, 0, buffer.Length);//讀取的時候設置Position,寫入的時候不需要設置 fs.Close(); using (FileStream zfstram = File.Create(zipFile)) { using (ZipOutputStream zipstream = new ZipOutputStream(zfstram)) { zipstream.Password = md5(password);//設置屬性的時候在PutNextEntry函數之前 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">基礎目錄</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)//如果是文件夾繼續遞歸 { 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('\\');//這邊判斷壓縮文件里面是否存在目錄,存在的話先創建目錄后繼續解壓 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; } } }
調用函數如下:
static void Main(string[] args) { var str = @"\學籍導入模板.xls"; //var arr=str.Split('\\'); var filePath = @"D:\程序文件\VS2010學習\StudyProgram\zip壓縮與解壓\File\學籍導入模板.xls"; //ZipHelper.ZipFile(filePath, @"D:\程序文件\VS2010學習\StudyProgram\zip壓縮與解壓\File\test.zip", 6, "123"); var dirPath = @"D:\程序文件\VS2010學習\StudyProgram\zip壓縮與解壓"; //ZipHelper.ZipDir(dirPath + @"\File", dirPath + @"\File.zip", 6, "huage"); ZipHelper.UnZip(dirPath + @"\File.zip", dirPath + @"\test", "huage"); Console.ReadKey(); }
效果圖如下:
以上是“C#如何使用ICSharpCode.SharpZipLib.dll進行文件的壓縮與解壓功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。