0

Using the DOTNETZIP library, I can currently unzip/extract .ZIP files using the following code:

private void Unzip(object sender, EventArgs e)
{
    string zipPath = pub.DepoDirDaily + @"\";
    string unzipPath = pub.DepoDirDaily + @"\";

    DirectoryInfo efaFiles = new DirectoryInfo(zipPath);
    foreach (var file in efaFiles.GetFiles("*.exe*"))
    {
        if (file.ToString().Contains(pub.YYYMMDD_t0) && file.ToString().Contains(".exe"))
        {
            try
            {
                String FileName = file.ToString().Replace(zipPath, "");
                String FileNameClean = FileName.Replace(".exe", "");
                String OutputDir = unzipPath + FileNameClean;
                String InputDir = zipPath + file;

                ExtractFileToDirectory(InputDir, OutputDir, "password1");
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex);
             }
        }
    }
}

public void ExtractFileToDirectory(string existingZipFile, string outputDirectory, string Password)
{
    try
    {
         ZipFile zip = ZipFile.Read(existingZipFile);
         Directory.CreateDirectory(outputDirectory);

         foreach (ZipEntry e in zip)
         {
             e.Password = Password;
             e.Extract(outputDirectory); 
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
}

However, when attempting to unzip .EXE files, I get the following error:

Ionic.Zip.ZipException: Cannot read that as a ZipFile ---> Ionic.Zip.BadReadException: Bad signature (0x00505A4D) at position 0x00000000

I have researched further, and it looks like that DOTNETZIP is not capable of handling .EXE files, so this does actually make sense. I have tried renaming the extension to .ZIP and attempt to unzip, and it comes up with the same error unfortunately.

It looks like it is possible to extract using the 7ZIP library, however I am struggling to find any proper examples online, and the ones that I do, don't include the possibility to include a password, so aren't useful.

Does anyone have anything that may be of help to me? Either a different library, a work-around, or some help understanding the 7ZIP library would be much appreciated!

THANKS!

Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • You can use the 7ZIP from command line inside your c# code. Just use the C# code to execute a .exe file and pass the correct paramters to 7zip. You only need to have access to the 7zip.exe path where your running your app from – Brad Nov 12 '18 at 17:34
  • Dotnetzip can of course hande *.exe file. Maybe it is a virus scanner interfering with extraction? – Klaus Gütter Nov 12 '18 at 17:44
  • @Brad To execute via command line do I use something similar to: System.Diagnostics.Process.Start("CMD.exe",comm); Where 'comm' is my 'command text'? – dhowells217 Nov 12 '18 at 17:45
  • @KlausGütter Thanks for confirmation. In respect to the virus scanner interfering, how should I approach this? – dhowells217 Nov 12 '18 at 17:46
  • 1
    What kind of exe files are you talking about? Are you trying to unpack self-extracting archives, or do you just want to get some resource sections from exe files? –  Nov 12 '18 at 17:53
  • The .Net `System.IO.Compression.ZipFile` class can handle self-extracting archives. – Jimi Nov 12 '18 at 18:14
  • @Jimi This works for .EXE files as far as I am aware, but does not allow for password to be included if I am correct? – dhowells217 Nov 13 '18 at 08:27
  • Unfortunately, it does not. Check out [SharpZipLib](https://github.com/icsharpcode/SharpZipLib/blob/master/README.md), I think it can handle both, but I'm not sure. – Jimi Nov 13 '18 at 08:37
  • Hi @elgonzo I'm not 100% sure what you mean to be honest. We get these .EXE files dropped in an SFTP, and they contain some files. I can use the 7zip program to manually unzip them in which they "become" a regular folder than contains the .txt file that I actually want. Does that help at all? – dhowells217 Nov 13 '18 at 09:10
  • Thanks @Jimi, I will check this out now. Much appreciated. – dhowells217 Nov 13 '18 at 09:10
  • Check this one out, too [SevenZipSharp - GitHub](https://github.com/squid-box/SevenZipSharp). Also available as NuGet Package. This one is a port of the original 7-Zip lib. It claims it supports both Password encryption and Sfx archives (well, same as 7-Zip). – Jimi Nov 13 '18 at 09:19
  • @Jimi Thanks for the suggestion. I will look at SevenZipSharp, will try and find a good example that I can use. – dhowells217 Nov 13 '18 at 12:08
  • IIRC, it already includes a test solution/project. – Jimi Nov 13 '18 at 12:09

1 Answers1

0

.exe file is not a zip file, it is a portable executable file. Have a look at the suggestions from Parsing plain Win32 PE File (Exe/DLL) in .NET for libraries that can parse the file to some preprocessed form, or you could just read it as a binary file and pull the fields you want yourself.

MadKarel
  • 299
  • 2
  • 6
  • Thanks for the link. The only reason I "want" to unzip it, is to access the file manually I am unzipping it (on 7zip funnily enough) to be able to access the file itself. I will read your links though, thanks. – dhowells217 Nov 12 '18 at 17:48
  • 1
    I'm reasonably sure that the OP isn't refering to **any** exe file, but to a self-extracting zip file, which is nothing that a zip file plus a mini executable module that extracts the thing. – Alejandro Nov 12 '18 at 17:50
  • Thanks @Alejandro – dhowells217 Nov 12 '18 at 17:53
  • 2
    @Alejandro, that is not certain. Note how OP mentions 7zip being capable to do so. But consider that 7zip can be used to extract resources from exe files. So, it is not clear to me whether the OP speaks about self-extracting archives or not... –  Nov 12 '18 at 17:53
  • I'm pretty sure that @Alejandro has it right. The thing is, that a self-executing zip file is not a zip file. A program like 7zip reads the file, recognizes it as a PE file, then strips away the PE-ness of the file and un-zips the zippy part. Your zip library probably doesn't di that – Flydog57 Nov 12 '18 at 17:56