using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.IO.Packaging; using Ionic.Zip; namespace Bowin.Common { public class ZIPHelper { public static IList ExtractFile(string zipFilePath, string targetPath) { IList result = new List(); using (Package zip = Package.Open(zipFilePath, FileMode.OpenOrCreate)) { var parts = zip.GetParts(); foreach (var part in parts) { var stream = part.GetStream(); byte[] buffer = new byte[stream.Length + 100]; long total = 0; for (int offset = 0; ; ) { int bytesRead = stream.Read(buffer, offset, 100); if (bytesRead == 0) break; offset += bytesRead; total += bytesRead; } string filePath = targetPath + @"\" + Path.GetFileName(part.Uri.LocalPath); var fileStream = File.Create(filePath); for (int offset = 0; ; ) { if (total - (offset + 1) > 100) { fileStream.Write(buffer, offset, 100); offset += 100; } else { fileStream.Write(buffer, offset, Convert.ToInt32(total - (offset + 1))); fileStream.Flush(); break; } } result.Add(filePath); } } return result; } /// /// 解压缩.z文件 /// /// /// public static IList ExtractZFile(string zFilePath, string outPutPath) { IList result = new List(); System.Diagnostics.Process proc = new System.Diagnostics.Process(); string pathOf7Zip = System.Configuration.ConfigurationManager.AppSettings["PathOf7Zip"]; if (System.Web.HttpContext.Current != null) { pathOf7Zip = System.Web.HttpContext.Current.Server.MapPath("~/") + pathOf7Zip; } else { pathOf7Zip = System.AppDomain.CurrentDomain.BaseDirectory + pathOf7Zip; } proc.StartInfo.FileName = pathOf7Zip; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.Arguments = "l " + zFilePath; bool currentIsFile = false; try { proc.Start(); do { if (proc.StandardOutput.EndOfStream) { break; } string buffer = proc.StandardOutput.ReadLine(); if (buffer.StartsWith("----")) { currentIsFile = !currentIsFile; } else { if (currentIsFile) { result.Add(outPutPath + @"\" + buffer.Split(' ').Last()); } } } while (true); proc.WaitForExit(); //if (!proc.WaitForExit(3600000)) //{ //} proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = pathOf7Zip; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = false; proc.StartInfo.RedirectStandardInput = false; proc.EnableRaisingEvents = true; proc.StartInfo.Arguments = "e -y -o" + outPutPath + " " + zFilePath; proc.Start(); proc.WaitForExit(); proc.Close(); } catch (Exception ex) { Exception MyEx = new Exception("解压缩出错!" + ex.Message); } proc.Dispose(); return result; } public static string PackageDirectory(string path) { using (ZipFile zipNew = new ZipFile()) { var pathName = Path.GetFileName(path.TrimEnd('\\')); var fileInfos = Directory.GetFiles(path); zipNew.UseUnicodeAsNecessary = true; //zipNew. // add this map file into the "images" directory in the zip archive //zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images"); //// add the report into a different directory in the archive //zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files"); foreach (var fileInfo in fileInfos) { zipNew.AlternateEncodingUsage = ZipOption.AsNecessary; zipNew.AlternateEncoding = Encoding.Unicode; zipNew.AddFile(fileInfo, @"\"); } zipNew.Save(pathName + ".zip"); return path.TrimEnd('\\') + "\\" + pathName + ".zip"; } } } }