C# Extract/Unzip a .EXE file

-1

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!

c#
.net
exe
unzip
asked on Stack Overflow Nov 12, 2018 by dhowells217 • edited Nov 12, 2018 by Spara

1 Answer

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.

answered on Stack Overflow Nov 12, 2018 by MadKarel

User contributions licensed under CC BY-SA 3.0