【问题标题】:File extraction using stand alone exe - C#使用独立 exe 提取文件 - C#
【发布时间】:2016-09-09 20:51:28
【问题描述】:

我正在尝试使用 ssis 中的 C# 脚本解压缩文件。 我最初使用 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 
    {
        //
    }
}

但是,我没有在服务器中部署 dll 的权限。所以我切换到 7Za.exe(独立 exe)。中途我意识到它只支持 7z、cab、zip、gzip、bzip2、Z 和 tar 格式。我需要压缩的文件没有任何扩展名。

有没有办法用独立的 exe 提取文件?我在 ssis 中使用 C# 4.0。

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

}

【问题讨论】:

标签: c# .net c#-4.0 zip extract


【解决方案1】:

您可以压缩/解压缩文件,因为 .NET Framework 4.5 使用 System.Io.Compression.ZipFile 类。

但是,如果您不能使用此 .Net Framework 版本,则必须将 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);
          }
     };

您可以在此处由 Jeffrey Richter 提供更详细的解释(我从那里学习了这个技巧):https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition/

您可以尝试使用 ILMerge。 不过,它有一些限制,可以防止它在某些项目中出现问题

【讨论】:

  • 谢谢 Sidewinder94.. 我会试试这个。
  • 我可以使用 System.Reflection 来加载 dll .. 可以吗?
  • System.Reflection 应该可以很好地获取当前程序集名称 yes 。 (请注意,我没有尝试过)
猜你喜欢
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 2016-05-01
相关资源
最近更新 更多