Search code examples
c#winformspasswordszip7zip

How to Zip and UnZip Files on C# using Squid-Box.SevenZipSharp Execution has failed error


I have recently tried compressing and unzipping files using the NuGet "Squid-Box.SevenZipSharp" However I can't compress them, the error probably comes from the dll library. I have tried downloading 7zip in 32 bit and 64 bit, using both .dll but the error is the same, I can't think of anything to do.

private void Compress(string source, string output)
        {
/*32-bits version*/  string path = Directory.GetCurrentDirectory() + @"\32-7z.dll";
//64-bits version    string path = Directory.GetCurrentDirectory() + @"\64-7z.dll";
            
            SevenZipCompressor compressor = new SevenZipCompressor();
            compressor.ArchiveFormat = OutArchiveFormat.Zip;
            compressor.TempFolderPath = Path.GetTempPath();
            compressor.CompressionMode = SevenZip.CompressionMode.Create;
            compressor.CompressionLevel = SevenZip.CompressionLevel.Fast;
            compressor.CompressionMethod = CompressionMethod.Lzma2;
            compressor.ZipEncryptionMethod = ZipEncryptionMethod.Aes256;

            var assemblyDllPath = compressor.TempFolderPath + "32-7z.dll";
          //var assemblyDllPath = compressor.TempFolderPath + "64-7z.dll";
            File.Copy(path, assemblyDllPath, overwrite: true);
            SevenZipExtractor.SetLibraryPath(path);
            
            compressor.CompressDirectory(@"A:\C#\random", @"A:\C#\empty\archive.zip", "password");

        }

When im using the 32-bits dll, proyect is set as Debug x86. When im using the 64-bits dll, proyect is set as Debug x64.

The error is the following in both cases:

SevenZip.SevenZipException
  HResult=0x80131500
  Message = Execution has failed due to an internal SevenZipSharp issue (0x80004001 / -2147467263).
Please report it to https://github.com/squid-box/SevenZipSharp/issues/, include the release number, 7z version used, and attach the archive.
  Source = SevenZipSharp

I really appreciate the time you put into this, thanks in advance.


Solution

  • The error code you're getting (0x80004001) corresponds to the error "Not Implemented" - it comes from the fact that you're trying to use a CompressionMethod that is not supported by the .zip format.

    Unless you have specific requirements I'd suggest using CompressionMode.Default - otherwise the following work with the .zip format:

    CompressionMethod.Copy

    CompressionMethod.Deflate

    CompressionMethod.Deflate64

    CompressionMethod.BZip2

    CompressionMethod.Lzma

    CompressionMethod.Ppmd

    That's the answer I got from squid-box on github, just changeCompressionMethod.Lzma2 to CompressionMethod.Lzma