C#zip壓縮類
阿新 • • 發佈:2017-07-04
ips 網上 ide true password creat sta puts 目錄
改造了網上的代碼,可以壓縮文件夾、指定文件列表(文件和文件夾的混合列表,需要指定子文件夾下需要壓縮的文件),註釋很詳細不做解釋
public class ZipHelper { /// <summary> /// 壓縮單個文件 /// </summary> /// <param name="fileToZip">要壓縮的文件</param> /// <param name="zipedFile">壓縮後的文件</param> /// <param name="compressionLevel">View Code壓縮等級</param> /// <param name="blockSize">每次寫入大小</param> public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { //如果文件沒有找到,則報錯 if (!System.IO.File.Exists(fileToZip)) {throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!"); } using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) {using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int sizeRead = 0; try { do { sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); } while (sizeRead > 0); } catch (System.Exception ex) { throw ex; } StreamToZip.Close(); } ZipStream.Finish(); ZipStream.Close(); } ZipFile.Close(); } } /// <summary> /// 壓縮單個文件 /// </summary> /// <param name="fileToZip">要進行壓縮的文件名</param> /// <param name="zipedFile">壓縮後生成的壓縮文件名</param> public static void ZipFile(string fileToZip, string zipedFile) { //如果文件沒有找到,則報錯 if (!File.Exists(fileToZip)) { throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + fileToZip + " 不存在!"); } using (FileStream fs = File.OpenRead(fileToZip)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); using (FileStream ZipFile = File.Create(zipedFile)) { using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(5); ZipStream.Write(buffer, 0, buffer.Length); ZipStream.Finish(); ZipStream.Close(); } } } } /// <summary> /// 壓縮文件 /// </summary> /// <param name="copyTopath">壓縮到哪個路徑</param> /// <param name="zipFileName">壓縮文件名稱</param> public static void ZipFile(List<string> sourceFileNames, string zipFileName) { //壓縮文件打包 using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName))) { s.SetLevel(9); byte[] buffer = new byte[4096]; foreach (string file in sourceFileNames) { if (Directory.Exists(file))// 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件 { string pPath = ""; pPath += Path.GetFileName(file); pPath += "\\"; ZipSetp(file, s, pPath, sourceFileNames); } else // 否則直接壓縮文件 { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } } s.Finish(); s.Close(); } } /// <summary> /// 壓縮多層目錄 /// </summary> /// <param name="strDirectory">待壓縮目錄</param> /// <param name="zipedFile">壓縮後生成的壓縮文件名,絕對路徑</param> public static void ZipFileDirectory(string strDirectory, string zipedFile) { using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { s.SetLevel(9); ZipSetp(strDirectory, s, ""); } } } /// <summary> /// 壓縮多層目錄 /// </summary> /// <param name="strDirectory">待壓縮目錄</param> /// <param name="zipedFile">壓縮後生成的壓縮文件名,絕對路徑</param> /// <param name="files">指定要壓縮的文件列表(完全路徑)</param> public static void ZipFileDirectory(string strDirectory, string zipedFile, List<string> files) { using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { s.SetLevel(9); ZipSetp(strDirectory, s, "", files); } } } /// <summary> /// 遞歸遍歷目錄 /// </summary> /// <param name="strDirectory">The directory.</param> /// <param name="s">The ZipOutputStream Object.</param> /// <param name="parentPath">The parent path.</param> /// <param name="files">需要壓縮的文件</param> private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath, List<string> files = null) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } string[] filenames = Directory.GetFileSystemEntries(strDirectory); byte[] buffer = new byte[4096]; foreach (string file in filenames)// 遍歷所有的文件和目錄 { if (files != null && !files.Contains(file)) { continue; } if (Directory.Exists(file))// 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件 { string pPath = parentPath; pPath += Path.GetFileName(file); pPath += "\\"; ZipSetp(file, s, pPath, files); } else // 否則直接壓縮文件 { //打開壓縮文件 string fileName = parentPath + Path.GetFileName(file); ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } } } /// <summary> /// 解壓縮一個 zip 文件。 /// </summary> /// <param name="zipedFile">壓縮文件</param> /// <param name="strDirectory">解壓目錄</param> /// <param name="password">zip 文件的密碼。</param> /// <param name="overWrite">是否覆蓋已存在的文件。</param> public static void UnZip(string zipedFile, string strDirectory, bool overWrite, string password) { if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\\")) strDirectory = strDirectory + "\\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile))) { if (password != null) { s.Password = password; } ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "") { if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName))) { using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName)) { 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(); } } /// <summary> /// 解壓縮一個 zip 文件。 /// </summary> /// <param name="zipedFile">壓縮文件</param> /// <param name="strDirectory">解壓目錄</param> /// <param name="overWrite">是否覆蓋已存在的文件。</param> public static void UnZip(string zipedFile, string strDirectory, bool overWrite) { UnZip(zipedFile, strDirectory, overWrite, null); } /// <summary> /// 解壓縮一個 zip 文件。 /// 覆蓋已存在的文件。 /// </summary> /// <param name="zipedFile">壓縮文件</param> /// <param name="strDirectory">解壓目錄</param> public static void UnZip(string zipedFile, string strDirectory) { UnZip(zipedFile, strDirectory, true); } /// <summary> /// 獲取壓縮文件中指定類型的文件 /// </summary> /// <param name="zipedFile">壓縮文件</param> /// <param name="fileExtension">文件類型(.txt|.exe)</param> /// <returns>文件名稱列表(包含子目錄)</returns> public static List<string> GetFiles(string zipedFile, List<string> fileExtension) { List<string> files = new List<string>(); if (!File.Exists(zipedFile)) { //return files; throw new FileNotFoundException(zipedFile); } using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsFile) { //Console.WriteLine("Name : {0}", theEntry.Name); if (fileExtension != null) { if (fileExtension.Contains(Path.GetExtension(theEntry.Name))) { files.Add(theEntry.Name); } } else { files.Add(theEntry.Name); } } } s.Close(); } return files; } /// <summary> /// 獲取壓縮文件中的所有文件 /// </summary> /// <param name="zipedFile">壓縮文件</param> /// <returns>文件名稱列表(包含子目錄)</returns> public static List<string> GetFiles(string zipedFile) { return GetFiles(zipedFile, null); } }
C#zip壓縮類