I'm trying to compress some files using SevenZipSharp but getting an error

14,473

Solution 1

You're probably missing the internal COM component that is required. If you check the InnerException, it should give you a good idea of what's missing. Copy these to your working directory and you should be set.

Solution 2

As mentioned at the end of the OP's post, you need to set the library path. But to overcome the uniqueness of the environment, you could always use reflection to set the path to the DLL. As long as the 7z.dll is in the project's bin folder, this will get you the path to it.

add this to your using statements:

using System.Reflection;

Then set the path like this:

SevenZipCompressor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll"));

Solution 3

Copy 7z.dll (32bit version) to working directory. This exception sometimes thrown in the 64-bit version.

Share:
14,473
DanielVest
Author by

DanielVest

Updated on June 12, 2022

Comments

  • DanielVest
    DanielVest almost 2 years

    I referenced to my project the dll file: SevenZipSharp.dll

    Then in the top of Form1 I added:

    using SevenZip;
    

    Then I created a function that I'm calling from a button click event:

    private void Compress()
    {
                string source = @"C:\Users\bout0_000\AppData\Local\Diagnostic_Tool_Blue_Screen\Diagnostic Tool Blue Screen\SF_02-08-13";
                string output = @"D:\Zipped.zip";
    
                SevenZipCompressor compressor = new SevenZipCompressor();
                compressor.ArchiveFormat = OutArchiveFormat.Zip;
                compressor.CompressionMode = CompressionMode.Create;
                compressor.TempFolderPath = System.IO.Path.GetTempPath();
                compressor.CompressDirectory(source, output);
    }
    

    I used a breakpoint and the error is on the line:

    compressor.CompressDirectory(source, output);
    

    But I'm getting an error:

    Cannot load 7-zip library or internal COM error! Message: DLL file does not exist

    But I referenced the dll already, so why this error? How can I fix it?

    Solved the problem:

    private void Compress()
    {
                string source = @"C:\Users\bout0_000\AppData\Local\Diagnostic_Tool_Blue_Screen\Diagnostic Tool Blue Screen\SF_02-08-13";
                string output = @"D:\Zipped.zip";
                SevenZipExtractor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
                SevenZipCompressor compressor = new SevenZipCompressor();
                compressor.ArchiveFormat = OutArchiveFormat.Zip;
                compressor.CompressionMode = CompressionMode.Create;
                compressor.TempFolderPath = System.IO.Path.GetTempPath();
                compressor.CompressDirectory(source, output);
    }
    
  • DanielVest
    DanielVest almost 11 years
    Added this line and its working now : SevenZipExtractor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll"); the problem is that if i give my program to someone else now he will not have the 7z.dll on his pc . So he will gt an exception.
  • Vaughan Hilts
    Vaughan Hilts almost 11 years
    @DanielVest That's why you should copy the 7z.dll to your working directory.
  • DanielVest
    DanielVest almost 11 years
    Vaughan ok i copied the 7z.dll to the \Debug directory. But i cant add this dll as reference it wont let me. So how do i make now that the dll will be in my program without writing specific path for it ?
  • Vaughan Hilts
    Vaughan Hilts almost 11 years
    @DanielVest Set your library path with the code you indicated above to be in your current working directory. msdn.microsoft.com/en-us/library/… Let me know how it works out!
  • DanielVest
    DanielVest almost 11 years
    Ok just did it now the dll file is in the working directory and the path in my code is set to this directory. So the file is in the working directory thats in this case: D:\\C-Sharp\\Diagnostic Tool Blue Screen\\Diagnostic Tool Blue Screen\\Diagnostic Tool Blue Screen\\bin\\Debug and the dll file is in the \\Debug too. What now ? Not everyone have this working directory and the dll file there.
  • Vaughan Hilts
    Vaughan Hilts almost 11 years
    @DanielVest Is there any reason they can't? Simply set it to "Copy Local" in Visual Studio and you should be set.
  • Vaughan Hilts
    Vaughan Hilts almost 11 years
    This'll ensure it's always copied to the bin, accompanying your EXE.
  • DanielVest
    DanielVest almost 11 years
    Where do i set this Copy Local ? I can set it when the dll is referened so there is a property for the dll called Copy Local and is set to true. But this dll is not referenced its just in the working directory so where in my project i set this Copy Local option ?
  • Vaughan Hilts
    Vaughan Hilts almost 11 years
    Add hte 7ZIp.dll to your project and set it to copy local. (the root directory.)
  • mayowa ogundele
    mayowa ogundele over 7 years
    This is the best answer i got. You have to install the 32-bit version of the Application(7-Zip).