DanielVest
DanielVest

Reputation: 823

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

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);
}

Upvotes: 5

Views: 15930

Answers (4)

Ayub
Ayub

Reputation: 2375

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

Upvotes: 1

mikeagoff
mikeagoff

Reputation: 386

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"));

Upvotes: 2

manjula
manjula

Reputation: 1

This is working well & max compress method ever.the bast solution.

  • give writing access to the output folder or drive.
  • install 7 zip software into your pc

Function:

private void Compress() 
{

    string source = "E:\\w";
    string output = "E:\\3.7z";

    string programFiles1 = "C:\\Program Files\\7-Zip\\7z.dll";

    if (File.Exists(programFiles1))
    {
        SevenZipExtractor.SetLibraryPath(programFiles1);
    }

    SevenZipCompressor compressor = new SevenZipCompressor();
    compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
    compressor.CompressionMode = SevenZip.CompressionMode.Create;
    compressor.TempFolderPath = System.IO.Path.GetTempPath();
    compressor.CompressDirectory(source, output);

}

Upvotes: 0

Vaughan Hilts
Vaughan Hilts

Reputation: 2879

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.

Upvotes: 3

Related Questions