From e37dba43c7dffafd19260ad0fe1a340f7e98f869 Mon Sep 17 00:00:00 2001 From: fuml Date: Sat, 26 Mar 2022 20:13:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=20=E5=A2=9E=E5=8A=A0=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E7=BC=A9=20tar.gz=20=20=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=202=20=E4=BF=AE=E6=94=B9=20zip=20?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E7=BC=A9=E4=B8=AD=E7=9B=AE=E5=BD=95=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SharpTargz.cs" | 88 +++++++++++++++++++ .../SharpZip.cs" | 78 +++------------- 2 files changed, 100 insertions(+), 66 deletions(-) create mode 100644 "DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" diff --git "a/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" new file mode 100644 index 0000000..f422d04 --- /dev/null +++ "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" @@ -0,0 +1,88 @@ +using ICSharpCode.SharpZipLib.GZip; +using ICSharpCode.SharpZipLib.Tar; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace DotNet.Utilities +{ + class SharpTargz + { + + /// + /// 生成 ***.tar.gz 文件 + /// + /// 文件基目录(源文件、生成文件所在目录) + /// 待压缩的源文件夹名 + public static bool CreatTarGzArchive(string strBasePath, string strSourceFolderName) + { + if (string.IsNullOrEmpty(strBasePath) + || string.IsNullOrEmpty(strSourceFolderName) + || !System.IO.Directory.Exists(strBasePath) + || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName))) + { + return false; + } + + Environment.CurrentDirectory = strBasePath; + string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName); + string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar.gz"); + + Stream outTmpStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate); + + //注意此处源文件大小大于4096KB + Stream outStream = new GZipOutputStream(outTmpStream); + TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor); + TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath); + archive.WriteEntry(entry, true); + + if (archive != null) + { + archive.Close(); + } + + outTmpStream.Close(); + outStream.Close(); + + return true; + } + + /// + /// 文件解压 + /// + /// 压缩文件路径 + /// 解压到的目录 + /// + public static bool UnzipTgz(string zipPath, string goalFolder) + { + Stream inStream = null; + Stream gzipStream = null; + TarArchive tarArchive = null; + try + { + using (inStream = File.OpenRead(zipPath)) + { + using (gzipStream = new GZipInputStream(inStream)) + { + tarArchive = TarArchive.CreateInputTarArchive(gzipStream,Encoding.Default); + tarArchive.ExtractContents(goalFolder); + tarArchive.Close(); + } + } + return true; + } + catch (Exception ex) + { + Console.WriteLine("压缩出错!"+ex.Message); + return false; + } + finally + { + if (null != tarArchive) tarArchive.Close(); + if (null != gzipStream) gzipStream.Close(); + if (null != inStream) inStream.Close(); + } + } + } +} diff --git "a/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpZip.cs" "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpZip.cs" index c9f5e45..6178a78 100644 --- "a/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpZip.cs" +++ "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpZip.cs" @@ -3,7 +3,7 @@ using System.IO; using System.Diagnostics; using Microsoft.Win32; -using ICSharpCode.SharpZipLib.Checksums; +using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip; ///压缩、解压缩类 @@ -49,17 +49,19 @@ namespace DotNet.Utilities } ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry; + string tmpDir = string.Empty; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); + tmpDir = Path.Combine(dir, directoryName); if (directoryName != String.Empty) { - Directory.CreateDirectory(dir + directoryName); + Directory.CreateDirectory(tmpDir); } if (fileName != String.Empty) { - FileStream streamWriter = File.Create(dir + theEntry.Name); + FileStream streamWriter = File.Create(Path.Combine(tmpDir,fileName)); int size = 2048; byte[] data = new byte[2048]; while (true) @@ -267,7 +269,7 @@ namespace DotNet.Utilities } ZipInputStream s = null; ZipEntry theEntry = null; - string fileName; + string tmpDir = string.Empty; FileStream streamWriter = null; try { @@ -276,13 +278,15 @@ namespace DotNet.Utilities { if (theEntry.Name != String.Empty) { - fileName = Path.Combine(ZipedFolder, theEntry.Name); - if (fileName.EndsWith("/") || fileName.EndsWith("\\")) + string directoryName = Path.GetDirectoryName(theEntry.Name); + string fileName = Path.GetFileName(theEntry.Name); + tmpDir = Path.Combine(ZipedFolder, directoryName); + if (directoryName != String.Empty) { - Directory.CreateDirectory(fileName); + Directory.CreateDirectory(tmpDir); continue; } - streamWriter = File.Create(fileName); + streamWriter = File.Create(Path.Combine(tmpDir, fileName)); int size = 2048; byte[] data = new byte[2048]; while (true) @@ -322,16 +326,6 @@ namespace DotNet.Utilities } } - public class ZipHelper - { - #region 私有变量 - String the_rar; - RegistryKey the_Reg; - Object the_Obj; - String the_Info; - ProcessStartInfo the_StartInfo; - Process the_Process; - #endregion /// /// 压缩 @@ -339,58 +333,10 @@ namespace DotNet.Utilities /// 要解压的文件名 /// 要压缩的文件目录 /// 初始目录 - public void EnZip(string zipname, string zippath, string dirpath) - { - try - { - the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command"); - the_Obj = the_Reg.GetValue(""); - the_rar = the_Obj.ToString(); - the_Reg.Close(); - the_rar = the_rar.Substring(1, the_rar.Length - 7); - the_Info = " a " + zipname + " " + zippath; - the_StartInfo = new ProcessStartInfo(); - the_StartInfo.FileName = the_rar; - the_StartInfo.Arguments = the_Info; - the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - the_StartInfo.WorkingDirectory = dirpath; - the_Process = new Process(); - the_Process.StartInfo = the_StartInfo; - the_Process.Start(); - } - catch (Exception ex) - { - throw new Exception(ex.Message); - } - } /// /// 解压缩 /// /// 要解压的文件名 /// 要解压的文件路径 - public void DeZip(string zipname, string zippath) - { - try - { - the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command"); - the_Obj = the_Reg.GetValue(""); - the_rar = the_Obj.ToString(); - the_Reg.Close(); - the_rar = the_rar.Substring(1, the_rar.Length - 7); - the_Info = " X " + zipname + " " + zippath; - the_StartInfo = new ProcessStartInfo(); - the_StartInfo.FileName = the_rar; - the_StartInfo.Arguments = the_Info; - the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - the_Process = new Process(); - the_Process.StartInfo = the_StartInfo; - the_Process.Start(); - } - catch (Exception ex) - { - throw new Exception(ex.Message); - } - } - } } \ No newline at end of file -- Gitee From 2e38f32a0a1796bef01db627eb2d22c03faf451a Mon Sep 17 00:00:00 2001 From: fuml Date: Sat, 26 Mar 2022 20:33:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=20=E7=BB=99=20SharpTargz=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20public=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SharpTargz.cs" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" index f422d04..c3384be 100644 --- "a/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" +++ "b/DotNet.Utilities/\345\216\213\347\274\251\350\247\243\345\216\213\347\274\251/SharpTargz.cs" @@ -7,7 +7,7 @@ using System.Text; namespace DotNet.Utilities { - class SharpTargz + public class SharpTargz { /// -- Gitee