简体   繁体   中英

File extraction using stand alone exe - C#

I am trying to unzip a file using C# script in ssis. I initially used IonicZip.dll to do it as shown below, and it worked fine.

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. So I switched to 7Za.exe (the stand alone exe). Halfway through I realized it only supports 7z, cab, zip, gzip, bzip2, Z and the tar formats. The file I need to zip does not have any extension.

Is there a way to extract files with a standalone exe? I am using C# 4.0 in ssis.

My 7zip code is

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.

However, if you can't use this .Net Framework's version, you'll have to embed a dll into your assembly :

You can add the dll to the project and set the build action to Embedded Resource

Then you'll have to subscribe to your application's AssemblyResolve event to manually load the dll from your application embedded resources.

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/

OR

You can try to use ILMerge. It has some limitations that prevent it's une in some projects though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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