using ICSharpCode.SharpZipLib.Zip; using System; using System.IO; using System.Runtime.CompilerServices; namespace Zhaizj.Framework.Utils { /// /// 描述:压缩类 /// 作者:黄淑莺 /// 日期:2010-3-22 /// public class Zip { private int _blockSize = 0; private int _compressionLevel = 0; private int _fileCount = 0; private int _fileIndex = 0; /// /// 得到目录的数量 /// /// 目录 private void GetDirCount(string directory) { DirectoryInfo info = new DirectoryInfo(directory); foreach (FileInfo info2 in info.GetFiles()) { this._fileCount++; } foreach (DirectoryInfo info3 in info.GetDirectories()) { this.GetDirCount(info3.FullName); } } /// /// 得到文件的数量 /// /// 源文件 private void GetFileCount(string sourceFile) { ZipInputStream stream = new ZipInputStream(File.OpenRead(sourceFile)); while (stream.GetNextEntry() != null) { this._fileCount++; } stream.Close(); } /// /// 解压缩 /// /// 源文件 /// 目标文件夹 public void UnZip(string sourceFile, string destFolder) { ZipEntry entry; this.GetFileCount(sourceFile); ZipInputStream stream = new ZipInputStream(File.OpenRead(sourceFile)); while ((entry = stream.GetNextEntry()) != null) { if (!destFolder.EndsWith(@"\")) { destFolder = destFolder + @"\"; } string directoryName = Path.GetDirectoryName(destFolder); string fileName = Path.GetFileName(entry.Name); DirectoryInfo info = new DirectoryInfo(directoryName); if (!info.Exists) { Directory.CreateDirectory(directoryName); } string str3 = entry.Name.Replace("/", @"\"); if (str3.LastIndexOf(@"\") > 0) { str3 = str3.Substring(0, str3.LastIndexOf(@"\")); info = new DirectoryInfo(destFolder + str3); if (!info.Exists) { Directory.CreateDirectory(destFolder + str3); } } if (fileName != string.Empty) { this._fileIndex++; FileStream stream2 = File.Create(destFolder + entry.Name.Replace("/", @"\")); try { try { int count = 0x800; byte[] buffer = new byte[0x800]; int num2 = 0; goto Label_01B7; Label_013E: count = stream.Read(buffer, 0, buffer.Length); if (count <= 0) { goto Label_01F1; } stream2.Write(buffer, 0, count); num2 += count; long num3 = Math.Abs(entry.Size); if (num3 == 1L) { num3 = num2; } //this.ZipProgress((long)num2, num3, this.FileIndex, this.FileCount, fileName); Label_01B7: goto Label_013E; } catch (Exception exception) { stream2.Close(); throw new Exception("error 2 :" + exception.Message); } } finally { stream2.Close(); } Label_01F1: ; } } stream.Close(); //this.ZipProgress(1L, 1L, 1, 1, ""); this._fileCount = 0; this._fileIndex = 0; } /// /// 遍历ZIP目录 /// /// s /// directory /// zipDir private void ZipDirectory(ZipOutputStream s, string directory, string zipDir) { DirectoryInfo info = new DirectoryInfo(directory); foreach (FileInfo info2 in info.GetFiles()) { this.ZipFile(s, info2.FullName, zipDir + @"\" + info2.Name); } foreach (DirectoryInfo info3 in info.GetDirectories()) { this.ZipDirectory(s, info3.FullName, zipDir + @"\" + info3.Name); } } /// /// 遍历ZIP目录 /// /// s /// private void ZipDirectory(ZipOutputStream s,string directory) { DirectoryInfo info = new DirectoryInfo(directory); foreach (FileInfo info2 in info.GetFiles()) { this.ZipFile(s, new string[] { info2.FullName, info2.Name }); } foreach (DirectoryInfo info3 in info.GetDirectories()) { this.ZipDirectory(s, info3.FullName); } } /// /// 压缩文件 /// /// 压缩前的文件 /// 压缩后的文件 public void ZipFile(string fileToZip, string zipedFile) { if (!File.Exists(fileToZip)) { throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd"); } FileInfo info = new FileInfo(fileToZip); ZipOutputStream s = new ZipOutputStream(File.Create(zipedFile)); this._fileCount = 1; this.ZipFile(s, fileToZip, info.Name); s.Finish(); s.Close(); //this.ZipProgress(1L, 1L, 1, 1, ""); this._fileCount = 0; this._fileIndex = 0; } /// /// 压缩文件 /// /// s /// file /// zipDir private void ZipFile(ZipOutputStream s, string file, string zipDir) { FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read); ZipEntry entry = new ZipEntry(zipDir); s.PutNextEntry(entry); s.SetLevel(this.CompressionLevel); byte[] buffer = new byte[this.BlockSize]; int count = stream.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, count); this._fileIndex++; try { while (count < stream.Length) { int num2 = stream.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, num2); count += num2; // this.ZipProgress((long)count, stream.Length, this.FileIndex, this.FileCount, file); } stream.Close(); } catch (Exception exception) { stream.Close(); throw exception; } } /// /// 压缩文件 /// /// s /// param[0]文件路径及文件名param[1]文件名 private void ZipFile(ZipOutputStream s,string[] param) { FileStream stream = new FileStream(param[0], FileMode.Open, FileAccess.Read); ZipEntry entry = new ZipEntry(param[1]); s.PutNextEntry(entry); s.SetLevel(this.CompressionLevel); byte[] buffer = new byte[this.BlockSize]; int count = stream.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, count); this._fileIndex++; try { while (count < stream.Length) { int num2 = stream.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, num2); count += num2; // this.ZipProgress((long)count, stream.Length, this.FileIndex, this.FileCount, file); } stream.Close(); } catch (Exception exception) { stream.Close(); throw exception; } } /// /// 压缩文件 /// /// 源文件夹 /// 目标文件 public void ZipFileMain(string sourceFolder, string destFile) { this.GetDirCount(sourceFolder); DirectoryInfo info = new DirectoryInfo(sourceFolder); ZipOutputStream s = new ZipOutputStream(File.Create(destFile)); s.SetLevel(this._compressionLevel); this.ZipDirectory(s, sourceFolder, info.Name); s.Finish(); s.Close(); // this.ZipProgress(1L, 1L, 1, 1, ""); this._fileCount = 0; this._fileIndex = 0; } /// /// 压缩文件 /// /// param[0]源路径param[1]目标路径 public void ZipFileMain(string[] param) { this.GetDirCount(param[0]); DirectoryInfo info = new DirectoryInfo(param[0]); ZipOutputStream s = new ZipOutputStream(File.Create(param[1])); s.SetLevel(this._compressionLevel); this.ZipDirectory(s, param[0]); s.Finish(); s.Close(); // this.ZipProgress(1L, 1L, 1, 1, ""); this._fileCount = 0; this._fileIndex = 0; } /// /// 包大小 /// public int BlockSize { get { if (this._blockSize == 0) { return 0x400; } return this._blockSize; } set { this._blockSize = value; } } /// /// 压缩级别 /// public int CompressionLevel { get { if (this._compressionLevel == 0) { return 6; } return this._compressionLevel; } set { this._compressionLevel = value; } } } }