使用独立 exe 提取文件 - C#
File extraction using stand alone exe - C#
我正在尝试在 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。
我的 7zip 代码是
string str7ZipPath = @"C:\Tools Usezip";
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)
{
}
到 zip/unzip 文件,你可以,因为 .NET Framework 4.5 使用 System.Io.Compression.ZipFile
class.
但是,如果您不能使用此 .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。
虽然它有一些限制,但在某些项目中无法使用
我正在尝试在 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。
我的 7zip 代码是
string str7ZipPath = @"C:\Tools Usezip";
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)
{
}
到 zip/unzip 文件,你可以,因为 .NET Framework 4.5 使用 System.Io.Compression.ZipFile
class.
但是,如果您不能使用此 .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。 虽然它有一些限制,但在某些项目中无法使用