using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Diagnostics; using System.IO; using ICSharpCode.SharpZipLib.Zip; using SwireBev.Mobile.Utility; namespace MobileTest { public class ZipUtility { public delegate void ThreadStop(int zipType, int fileIndex, string status, string title); //线程操作完成 public event ThreadStop threadStop; /// <summary> /// 写入ZIP /// </summary> /// <param name="zos"></param> /// <param name="topDirName"></param> /// <param name="pathStr"></param> /// <param name="index"></param> /// <param name="absolute"></param> private void addZipWrite(ZipOutputStream zos, string topDirName, string pathStr, int index, string absolute) { string strEntryName = pathStr; if (!topDirName.Equals("")) { strEntryName = pathStr.Replace(topDirName, ""); } try { FileStream fs = File.OpenRead(pathStr); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(Path.GetFileName(strEntryName)); if (absolute.Equals("0")) { entry = new ZipEntry(strEntryName); } zos.PutNextEntry(entry); zos.Write(buffer, 0, buffer.Length); fs.Close(); threadStop(0, index, "OK", Path.GetFileName(strEntryName)); } catch { threadStop(0, index, "Error", Path.GetFileName(strEntryName)); } } /// <summary> /// 回调 /// </summary> /// <param name="zos"></param> /// <param name="topDirName"></param> /// <param name="pathStr"></param> /// <param name="absolute"></param> private void addZipEntry(ZipOutputStream zos, string topDirName, string pathStr, string absolute) { if (!Path.GetFileName(pathStr).Equals("")) { addZipWrite(zos, topDirName, pathStr, 0, absolute); } else { DirectoryInfo di = new DirectoryInfo(pathStr); foreach (DirectoryInfo item in di.GetDirectories()) { addZipEntry(zos, topDirName, item.FullName, absolute); } int index = 0; foreach (FileInfo item in di.GetFiles()) { addZipWrite(zos, topDirName, item.FullName, index++, absolute); } } } /// <summary> /// 压缩指定文件生成ZIP文件 /// </summary> /// <param name="topDirName">顶层文件夹名称</param> /// <param name="fileNamesToZip">待压缩文件列表</param> /// <param name="ZipedFileName">ZIP文件</param> /// <param name="CompressionLevel">压缩比</param> /// <param name="password">密码</param> /// <param name="comment">压缩文件注释文字</param> public void ZipFile ( string topDirName, //顶层文件夹名称 \Storage Card\PDADataExchange\send\xml\ string[][] fileNamesToZip, //待压缩文件列表 new string[2]{"version.xml","0") 0 相对 包含文件夹 1 绝对 不包含文件夹 string zipFileName, //ZIP文件 \Storage Card\PDADataExchange\send\zip\version.zip int CompressionLevel, //压缩比 7 string password, //密码 "" string comment //压缩文件注释文字 "" ) { ZipOutputStream zos = new ZipOutputStream(System.IO.File.Open(zipFileName, FileMode.Create)); if (password != null && password.Length > 0) zos.Password = password; if (comment != null && comment.Length > 0) zos.SetComment(comment); zos.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression if (fileNamesToZip.Length > 0) { foreach (string[] file in fileNamesToZip) { string filePath = topDirName + file[0]; addZipEntry(zos, topDirName, filePath, file[1]); } } else { string filePath = topDirName; addZipEntry(zos, topDirName, filePath, "0"); } zos.Finish(); zos.Close(); } /// <summary> /// 解压缩ZIP文件到指定文件夹 /// </summary> /// <param name="zipfileName">ZIP文件</param> /// <param name="UnZipDir">解压文件夹</param> /// <param name="password">压缩文件密码</param> public void UnZipFile(string zipFileName, string UnZipDir, string password) { ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)); if (password != null && password.Length > 0) s.Password = password; try { ZipEntry theEntry; int index = 0; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(UnZipDir); string pathname = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); string file = ""; try { //生成解压目录 pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题 directoryName = directoryName + "\\" + pathname; Directory.CreateDirectory(directoryName); file = directoryName + "\\" + fileName; if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(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(); } if (File.Exists(file)) { if (threadStop != null) { if (threadStop != null) { if (!Path.GetFileName(zipFileName).Equals(Path.GetFileName(file))) { threadStop(1, index++, "OK", Path.GetFileName(file)); } } } } } catch { if (File.Exists(file)) { if (threadStop != null) { if (threadStop != null) { if (!Path.GetFileName(zipFileName).Equals(Path.GetFileName(file))) { threadStop(1, index++, "Error", Path.GetFileName(file)); } } } } } } s.Close(); } catch (Exception eu) { throw eu; } finally { s.Close(); } } } }