簡體   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