简体   繁体   English

使用独立exe提取文件-C#

[英]File extraction using stand alone exe - C#

I am trying to unzip a file using C# script in ssis. 我试图在SSIS中使用C#脚本解压缩文件。 I initially used IonicZip.dll to do it as shown below, and it worked fine. 我最初使用IonicZip.dll进行了如下所示的操作,并且工作正常。

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 
    {
        //
    }
}

However, I do not have the permission to deploy the dll in the server. 但是,我没有在服务器中部署dll的权限。 So I switched to 7Za.exe (the stand alone exe). 因此,我切换到7Za.exe(独立exe)。 Halfway through I realized it only supports 7z, cab, zip, gzip, bzip2, Z and the tar formats. 中途我意识到它仅支持7z,cab,zip,gzip,bzip2,Z和tar格式。 The file I need to zip does not have any extension. 我需要压缩的文件没有任何扩展名。

Is there a way to extract files with a standalone exe? 有没有办法使用独立的exe提取文件? I am using C# 4.0 in ssis. 我在SSIS中使用C#4.0。

My 7zip code is 我的7邮递区号是

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)
{

}

To zip/unzip files, you can, since the .NET Framework 4.5 use the System.Io.Compression.ZipFile class. 要压缩/解压缩文件,由于.NET Framework 4.5使用System.Io.Compression.ZipFile类,因此可以。

However, if you can't use this .Net Framework's version, you'll have to embed a dll into your assembly : 但是,如果无法使用此.Net Framework的版本,则必须将dll嵌入到程序集中:

You can add the dll to the project and set the build action to Embedded Resource 您可以将dll添加到项目中,并将生成操作设置为Embedded Resource

Then you'll have to subscribe to your application's AssemblyResolve event to manually load the dll from your application embedded resources. 然后,您必须订阅应用程序的AssemblyResolve事件,才能从应用程序嵌入式资源中手动加载dll。

Example : 范例:

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);
          }
     };

You can a more detailed explanation by Jeffrey Richter here (I leanred this trick from there): https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition/ 您可以在此处(由Jeffrey Richter提供更详细的解释)(我从那里开始研究此技巧): https ://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr -通过c-第三版/

OR 要么

You can try to use ILMerge. 您可以尝试使用ILMerge。 It has some limitations that prevent it's une in some projects though 它有一些局限性,尽管在某些项目中它无法正常运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM