123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using SharpCompress.Archives;
- using SharpCompress.Archives.GZip;
- using SharpCompress.Archives.Zip;
- using SharpCompress.Common.GZip;
- using SharpCompress.Common;
- using SharpCompress.Common.SevenZip;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace PackingPress.Common
- {
- public class ZipHelper
- {
- private ZipHelper(){ }
- public static void Zips(string inDirectory, string zipFile)
- {
- ArchiveEncoding archiveEncoding = new ArchiveEncoding();
- archiveEncoding.Default = Encoding.GetEncoding("utf-8");
- SharpCompress.Writers.WriterOptions options = new SharpCompress.Writers.WriterOptions(CompressionType.Deflate);
- options.ArchiveEncoding = archiveEncoding;
- options.LeaveStreamOpen = false;
- using (var archive = ZipArchive.Create())
- {
- archive.AddAllFromDirectory(inDirectory + "\\");
- archive.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.BestSpeed;
- using (FileStream fs_scratchPath = new FileStream(zipFile, FileMode.Create, FileAccess.Write))
- {
- try
- {
- archive.SaveTo(fs_scratchPath, options);
- }
- catch (Exception ex)
- {
- FileHelper.AppendWriteFile("InstallLog.txt", ex.Message + "|" + ex.StackTrace);
- }
- }
- }
- }
- /// <summary>
- /// 解压文件
- /// </summary>
- /// <param name="zipFile">解压文件路径</param>
- /// <param name="outDirectory">解压文件后路径</param>
- public static void Decompression(string zipFile, string outDirectory)
- {
- if (File.Exists(zipFile))
- {
- ArchiveEncoding archiveEncoding = new ArchiveEncoding();
- archiveEncoding.Default = Encoding.GetEncoding("utf-8");
- SharpCompress.Readers.ReaderOptions options = new SharpCompress.Readers.ReaderOptions();
- options.ArchiveEncoding = archiveEncoding;
- options.LeaveStreamOpen = false;
- var archive = ArchiveFactory.Open(zipFile);
- foreach (var entry in archive.Entries)
- {
- if (!entry.IsDirectory)
- {
- entry.WriteToDirectory(outDirectory, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
- }
- }
- }
- }
- /// <summary>
- /// 解压文件7z
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="outPath"></param>
- /// <param name="waitForCompleted"></param>
- /// <returns></returns>
- public static bool DeZip(string filePath, string outPath, bool waitForCompleted = false)
- {
- using (Process ps = new Process())
- {
- ps.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\7z\\7za.exe";
- ps.StartInfo.Arguments = $"x \"{filePath}\" -o\"{outPath}\" -y";
- ps.StartInfo.UseShellExecute = false;
- ps.StartInfo.CreateNoWindow = true;
- ps.StartInfo.Verb = "runas";
- if (waitForCompleted)
- {
- ps.StartInfo.RedirectStandardOutput = true;
- ps.StartInfo.RedirectStandardInput = true;
- }
- else
- {
- ps.StartInfo.RedirectStandardOutput = false;
- ps.StartInfo.RedirectStandardInput = false;
- }
- ps.Start();
- if (waitForCompleted)
- {
- string text = ps.StandardOutput.ReadToEnd();
- if (text.IndexOf("Everything is Ok") > -1)
- {
- ps.StandardInput.Write("Exit");
- return true;
- }
- }
- else
- {
- ps.WaitForExit(10000);
- }
- return false;
- }
- }
- /// <summary>
- /// 压缩文件7z
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="zipFile"></param>
- /// <param name="waitForCompleted"></param>
- /// <returns></returns>
- public static bool EnZip(string filePath, string zipFile, bool waitForCompleted = false)
- {
- using (Process ps = new Process())
- {
- ps.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\7z\\7za.exe";
- ps.StartInfo.Arguments = $"a -t7z \"{zipFile}\" \"{filePath}\"";
- ps.StartInfo.UseShellExecute = false;
- ps.StartInfo.CreateNoWindow = true;
- ps.StartInfo.Verb = "runas";
- if (waitForCompleted)
- {
- ps.StartInfo.RedirectStandardOutput = true;
- ps.StartInfo.RedirectStandardInput = true;
- }
- else
- {
- ps.StartInfo.RedirectStandardOutput = false;
- ps.StartInfo.RedirectStandardInput = false;
- }
- ps.Start();
- if (waitForCompleted)
- {
- string text = ps.StandardOutput.ReadToEnd();
- if (text.IndexOf("Everything is Ok") > -1)
- {
- ps.StandardInput.Write("Exit");
- return true;
- }
- }
- else
- {
- ps.WaitForExit(10000);
- }
- return false;
- }
- }
- }
- }
|