首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SevenZipSharp不解压缩存档

SevenZipSharp不解压缩存档
EN

Stack Overflow用户
提问于 2022-09-25 23:45:17
回答 1查看 38关注 0票数 1

我正在尝试使用SevenZipSharp从https://github.com/squid-box/SevenZipSharp提取一个压缩存档。dll设置如下:

代码语言:javascript
复制
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复制到文件夹中。调用代码如下所示:

代码语言:javascript
复制
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()

代码语言:javascript
复制
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);
}   

最后,我得到了一个空目录;.7z内容从未被解压缩到它。我尝试使用.zip和.7z文件作为输入,每个文件都包含两个json文件,没有其他任何内容。当我在file.ExtractArchive(destinationFolder)设置一个断点时,它似乎是半正确初始化的:https://imgur.com/qjYpDur

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

我的设置有问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-09-26 02:39:16

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

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73848525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档