繁体   English   中英

SevenZipSharp 不提取存档

[英]SevenZipSharp doesn't extract archive

我正在尝试使用https://github.com/squid-box/SevenZipSharp中的 SevenZipSharp 来提取 zip 存档。 dll 设置如下:

public class Paths
{
    private static readonly string SynthEBDexeDirPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    public static readonly string ResourcesFolderPath = Path.Combine(SynthEBDexeDirPath, "Resources");
    // Toggle between the x86 and x64 bit dll
    public readonly string SevenZipPath = Path.Combine(ResourcesFolderPath, "7Zip", Environment.Is64BitProcess ? "x64" : "x86", "7z.dll");

dll 文件从最新版本的 7-Zip 复制到我的 Resources 文件夹中。 调用代码如下所示:

using System.IO;
using System.Windows.Controls;
using SevenZip;

namespace SynthEBD;
public class VM_ZipArchiveHandler : VM
{
    public VM_ZipArchiveHandler(Window_SevenZipArchiveHandler window) 
    { 
        if (File.Exists(PatcherSettings.Paths.SevenZipPath))
        {
            SevenZipBase.SetLibraryPath(PatcherSettings.Paths.SevenZipPath);
            Initialized = true;
        }
        else
        {
            CustomMessageBox.DisplayNotificationOK("Initialization Error", "Could not initialize Seven Zip from " + PatcherSettings.Paths.SevenZipPath);
        }
        
        Window = window;
    }

    public string DispString { get; set; } = string.Empty;
    public ProgressBar Prog = new ProgressBar();
    public Window_SevenZipArchiveHandler Window { get; set; }
    private bool Initialized { get; set; } = false;

    public void Unzip(string archivePath, string destinationFolder)
    {
        if (!Initialized)
        {
            return;
        }
        Prog.Minimum = 0;
        Prog.Maximum = 100;
        Prog.Value = 0;

        Window.Show();

        var progressHandler = new Progress<byte>(
            percentDone => Prog.Value = percentDone);
        var progress = progressHandler as IProgress<byte>;

        var file = new SevenZipExtractor(archivePath);
        file.Extracting += (sender, args) =>
        {
            progress.Report(args.PercentDone);
        };
        file.ExtractionFinished += (sender, args) =>
        {
            // Do stuff when done
        };

        Task.Run(() =>
        {
            //Extract the stuff
            file.ExtractArchive(destinationFolder);
        });

        Window.Close();
    }

    public static void UnzipArchive(string archivePath, string destinationDir)
    {
        Window_SevenZipArchiveHandler window = new Window_SevenZipArchiveHandler();
        VM_ZipArchiveHandler handler = new(window);
        window.DataContext = handler;
        handler.Unzip(archivePath, destinationDir);
    }
}

我打电话给UnzipArchive()

string tempFolderPath = Path.Combine(PatcherSettings.ModManagerIntegration.TempExtractionFolder, DateTime.Now.ToString("yyyy-MM-dd-HH-mm", System.Globalization.CultureInfo.InvariantCulture));

try
{
    Directory.CreateDirectory(tempFolderPath);
}
catch (Exception ex)
{
    Logger.LogError("Could not create or access the temp folder at " + tempFolderPath + ". Details: " + ex.Message);
    return installedConfigs;
}

try
{
    VM_ZipArchiveHandler.UnzipArchive(path, tempFolderPath);
}   

     
    

最后我得到一个空目录; the.7z 内容永远不会提取到它。 我尝试使用 a.zip 和 .7z 文件作为输入,每个文件都包含两个 json 文件,仅此而已。 当我在 file.ExtractArchive(destinationFolder) 处设置断点时,它似乎已半正确初始化: https://imgur.com/qjYpDur

看起来它被正确识别为 SevenZip 存档,但像 _filesCount 这样的字段是 null。

我的设置有问题吗?

我认为问题在于您的ExtractArchive包装在Task中,并且调用线程在提取完成之前返回并且没有等待。 不是 100% 的细节,但作为一个实验,我发现了什么有效,什么使目标目录为空:

public static void Main(string[] args)
{
    SevenZipBase.SetLibraryPath(
        Path.Combine(
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"7z.dll"));
    string archivePath = "D:\\Downloads\\imgs.7z";
    var file = new SevenZipExtractor(archivePath);

    // works 
    file.ExtractArchive("D:\\Downloads\\unzipped");

    // doesnt work
    Task.Run(() =>
    {
        file.ExtractArchive("D:\\Downloads\\unzipped");
    });

    // works
    Task.Run(() =>
    {
        file.ExtractArchive("D:\\Downloads\\unzipped");
    }).Wait();
}

暂无
暂无

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

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