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 0000000000000000000000000000000000000000..c3384be0f78409a92a6390e846995e13c1cfde1b
--- /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
+{
+ public 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 c9f5e45467e4a87c02e316f6ed35b9ca8e471264..6178a782c4a63bbfd2e0c9f20e8b5a8d016b38f8 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