简体   繁体   中英

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

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.

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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM