Search code examples
c#exe7zip

Unable to extract .exe (7zip self extracting archive) using SevenZipExtractor


Lib used: https://github.com/adoconnection/SevenZipExtractor

       var file = @"C:\Users\PC\Desktop\SelfExtractFile.exe";
       var destination = @"C:\Users\PC\Desktop\";

       using ( ArchiveFile archiveFile = new ArchiveFile(file))
       {
         archiveFile.Extract(destination, true);
       }

I get: : 'Object reference not set to an instance of an object.' archive and entries are null.

The library says it supports .exe.

Any idea how to fix this?


Solution

  • SevenZipExtractor library doesn't support self-extracting archive (SFX). It just supports extracting standard exe files.

    But you can run self-extracting archive in a process in a silent mode:

       static void Main(string[] args)
        {
            var file = @"c:\selfextracting.exe";
            var destination = @"C:\Users\PC\Desktop\";
    
            var output = ExtractSelfExtractingArchive(file, destination);
    
            Console.WriteLine(output);
        }
    
        private static string ExtractSelfExtractingArchive(string archiveFilePath, string destination)
        {
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = archiveFilePath;
                process.StartInfo.Arguments = $" -o\"{destination}\" -y";
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
                process.Start();
                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
    
                return output;
            }
            catch (Exception exception)
            {
    
                return exception.Message;
            }
        }