2016-05-13 50 views
0

我想使用ssis中的C#腳本解壓縮文件。 我最初使用IonicZip.dll來完成它,如下所示,它工作正常。使用獨立的EXE文件提取 - C#

private string ExtractFileToDirectory(string strSourceDirectory, string strDestinationDirectory, string strFileName) 
{ 
    string extractFileName = String.Empty; 
    try 
    { 

     ZipFile zip = ZipFile.Read(Path.Combine(strSourceDirectory ,strFileName)); 
     Directory.CreateDirectory(strDestinationDirectory); 
     foreach (ZipEntry e in zip) 
     { 
      e.Extract(strDestinationDirectory, ExtractExistingFileAction.OverwriteSilently); 
      extractFileName = e.FileName; 
     } 
     zip.Dispose(); 
     return extractFileName; 

    } 
    catch 
    { 
     // 
    } 
} 

但是,我沒有權限在服務器上部署dll。所以我切換到7Za.exe(獨立exe文件)。中途我意識到它只支持7z,cab,zip,gzip,bzip2,Z和tar格式。我需要壓縮的文件沒有任何擴展名。

有沒有辦法用獨立的EXE提取文件?我在ssis中使用C#4.0。

我7zip的代碼是

string str7ZipPath = @"C:\Tools Use\7zip"; 
string str7ZipArgs = "a -tzip "+ @"""C:\Source\FileA"""+ @" ""C:\Zip\*.*"""; 

ProcessStartInfo psi = new ProcessStartInfo(); 
psi.CreateNoWindow = false; 
psi.UseShellExecute = false; 
psi.FileName = str7ZipPath + "\\7za.exe"; 
psi.WindowStyle = ProcessWindowStyle.Normal; 
psi.Arguments = str7ZipArgs; 


try 
{ 
    using (Process proc = Process.Start(psi)) 
    { 
     proc.WaitForExit(); 
    } 
} 
catch (Exception ex) 
{ 

} 
+0

選中此項:https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr -via-c-third-edition/ – Sidewinder94

+1

以及FW上已有的.net資源如何? https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx – Gusman

+0

我在處理使用.NET Framework的zip文件時遇到了不好的內存( System.IO.Packaging),並完全忘了他們爲這些目的添加了一個新的命名空間:) – Sidewinder94

回答

1

要壓縮/解壓縮文件,就可以了,因爲在.NET Framework 4.5使用System.Io.Compression.ZipFile類。

但是,如果您不能使用此.NET框架的版本,你必須嵌入一個dll到你的裝配:

您可以將DLL添加到項目,並設置構建動作Embedded Resource

然後,你必須訂閱你的應用程序的AssemblyResolve事件手動加載你的應用程序嵌入式資源的DLL。

例子:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => 
    { 
      String resourceName = 「<YourAssemblyName>.」 + 
       new AssemblyName(args.Name).Name + 「.dll」; 
      using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 
      { 
       Byte[] assemblyData = new Byte[stream.Length]; 
       stream.Read(assemblyData, 0, assemblyData.Length); 
       return Assembly.Load(assemblyData); 
      } 
    }; 

您可以通過傑弗裏裏希特更詳細的解釋在這裏(我leanred從那裏這一招):https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition/

OR

你可以嘗試使用ILMerge 。 它有一些限制,可以防止它在某些項目中不可用

+0

謝謝Sidewinder94 ..我會試試這個。 –

+0

我可以使用System.Reflection加載dll ..請問這是否工作? –

+0

'System.Reflection'應該很好地獲取當前的程序集名稱yes。 (請注意,我沒有嘗試它) – Sidewinder94