解压缩文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace CommonHelper {
//引用的实体类 using ICShareCode.SharpZipLib; using ICShareCode.SharpZipLib.Zip; using ICShareCode.SharpZipLib.Checksums; public class ZipHelper { /// <summary> /// 压缩单个文件 /// </summary> /// <param name="fileToZip">要压缩的文件</param> /// <param name="zipedFile">压缩后的文件</param> /// <param name="compressionLevel">压缩等级</param> /// <param name="blockSize">每次写入大小</param> public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { if (File.Exists(fileToZip) == false) { throw new FileNotFoundException("指定压缩文件:"+fileToZip+" 不存在"); } using (FileStream ZipFile=File.Create(zipedFile)) { using (ZipOutputStream ZipStream=new ZipOutputStream(ZipFile)) { using (FileStream StreamToZip=new FileStream(fileToZip,FileMode.Open,FileAccess.Read)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1); ZipEntity zipEntity = new ZipEntity(fileName); ZipStream.PutNextEntry(zipEntity); 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 (Exception ex) { throw ex; } ZipStream.Finish(); ZipStream.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)==false) { throw new 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); ZipEntity zipEntry = new ZipEntity(fileName); ZipStream.PutNextEntry(zipEntry); ZipStream.SetLevel(5); ZipStream.Write(buffer, 0, buffer.Length); ZipStream.Finish(); ZipStream.Close(); } } } } /// <summary> /// 压缩多层目录 /// </summary> /// <param name="strDirectory"></param> /// <param name="zipedFile"></param> public static void ZipFileDirectory(string strDirectory, string zipedFile) { using (FileStream ZipFile=File.Create(zipedFile)) { using (ZipOutputStream s=new ZipOutputStream(ZipFile)) { ZipSetp(strDirectory, s, ""); } } } /// <summary> /// 递归遍历目录 /// </summary> /// <param name="strDirectory">The directory</param> /// <param name="s">The ZipOutputStream Object</param> /// <param name="parentPath">The parent path</param> public static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length-1]!=Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] fileNames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in fileNames) { if (Directory.Exists(file)) { string strPath = parentPath; strPath += file.Substring(file.LastIndexOf("\\") + 1); strPath += "\\"; ZipSetp(file, s, strPath); } else { using (FileStream fs=File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } } /// <summary> /// 解压一个zip文件 /// eg:ZipHelper.UnZip("上传路径","保存路径","",true) /// </summary> /// <param name="zipedFile">The ziped file</param> /// <param name="strDirectory">The STR directory</param> /// <param name="password">zip 文件密码</param> /// <param name="overWirte">是否覆盖已存在的文件</param> public static void UnZip(string zipedFile, string strDirectory, string password, bool overWirte) { if (strDirectory=="") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\\")) strDirectory = strDirectory + "\\"; using (ZipOutputStream s=new ZipOutputStream(File.OpenRead(zipedFile))) { 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)&& overWirte)|| (!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(); } } } }
不忘初心,方得始终