简体   繁体   English

我正在尝试使用SevenZipSharp压缩一些文件,但收到错误

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

I referenced to my project the dll file: SevenZipSharp.dll 我在项目中引用了dll文件: SevenZipSharp.dll

Then in the top of Form1 I added: 然后在Form1的顶部我添加:

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! 无法加载7-zip库或内部COM错误! Message: DLL file does not exist 消息:DLL文件不存在

But I referenced the dll already, so why this error? 但是我已经引用了dll,为什么会出现这个错误呢? 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. 您可能缺少所需的内部COM组件。 If you check the InnerException, it should give you a good idea of what's missing. 如果你检查InnerException,它应该让你知道缺少什么。 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. 如OP的帖子末尾所述,您需要设置库路径。 But to overcome the uniqueness of the environment, you could always use reflection to set the path to the DLL. 但是为了克服环境的独特性,您始终可以使用反射来设置DLL的路径。 As long as the 7z.dll is in the project's bin folder, this will get you the path to it. 只要7z.dll位于项目的bin文件夹中,就可以获得它的路径。

add this to your using statements: 将此添加到您的using语句:

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. 将7z.dll (32位版本)复制到工作目录。 This exception sometimes thrown in the 64-bit version. 此异常有时会在64位版本中引发。

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 在您的电脑上安装7个zip软件

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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