123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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<string> ExtractFile(string zipFilePath, string targetPath)
- {
- IList<string> result = new List<string>();
- 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;
- }
- /// <summary>
- /// 解压缩.z文件
- /// </summary>
- /// <param name="rarFilePath"></param>
- /// <returns></returns>
- public static IList<string> ExtractZFile(string zFilePath, string outPutPath)
- {
- IList<string> result = new List<string>();
- 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";
- }
- }
- }
- }
|