无法解压存档.NET框架

问题描述 投票:0回答:1

我正在 .NET 框架上开发 WPF 项目,因此我可以

t use the latest assembly of 
System.IO.ZipFile`。这个问题看似微不足道,但却占用了我的时间。

我有这个任务:

public async Task SetupEnvironment(IProgressReporter progressReporter)
{
    string basePath = AppDomain.CurrentDomain.BaseDirectory;
    string pythonExePath = System.IO.Path.Combine(basePath, @"CPython\python-embed\python.exe");

    if (!File.Exists(pythonExePath))
    {
        progressReporter.ReportProgress($"Python environment not found. Setting up environment in {basePath}");
        Thread.Sleep(300);
        string zipFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CPython\python.zip");
        //string extractPath = System.IO.Path.Combine(basePath, @"CPython");
        string extractPath = @"C:\Data\python";

        try
        {
            progressReporter.ReportProgress($"exctraxcting environment ");
            //ZipFile.ExtractToDirectory(zipFilePath, extractPath);
            using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (progressReporter != null)
                    {
                        progressReporter.ReportProgress($"exctraxcting {entry.Name}");
                    }

                    entry.ExtractToFile(System.IO.Path.Combine(extractPath, entry.Name), overwrite: true);
                }
            }

            progressReporter.ReportProgress($"Python environment set up successfully.");
            Thread.Sleep(5000);
        }
        catch (Exception ex)
        {
            progressReporter.ReportProgress($"Error setting up Python environment: {ex.Message}");
            MessageBox.Show(ex.ToString());
            Thread.Sleep(5000);
        }
    }
    else
    {
        progressReporter.ReportProgress($"Found existing Python environment in {basePath}");
        Thread.Sleep(5000);
    }
}

这个函数的核心是

entry.ExtractToFile(System.IO.Path.Combine(extractPath, entry.Name), overwrite: true);

无论我使用哪个路径作为输出,我都会在这行代码上遇到相同的错误:

await Task.Run(() => { SetupEnvironment(this); });

该进程无法访问该文件

C:\Data\python\_asyncio.pyd
,因为该文件正在被另一个进程使用。显然 _asyncio.pyd 是我试图提取的存档的一部分

还有

System.UnauthorizedAccessException:对路径“C:\Data\python”的访问被拒绝。

谷歌搜索我找到了这个帖子

进程无法访问文件,因为该文件正在被另一个进程使用

但也尝试更改目标路径(其中一个不包含在源路径中(正如我在示例中所做的那样))没有帮助

c# .net wpf
1个回答
0
投票

问题在于循环中最初某些entry.name为空的事实。因此,提取方法在尝试访问繁忙对象时崩溃了。 这是更新的功能:

公共异步任务设置环境3(IProgressReporter ProgressReporter) { 尝试 { string zipFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CPython\python.zip"); ProgressReporter.ReportProgress($"正在解压环境"); 字符串 tempExtractPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString()); if (!Directory.Exists(tempExtractPath)) { 目录.CreateDirectory(tempExtractPath); }

    using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            //string directoryPath = System.IO.Path.GetDirectoryName(entry.FullName);
            Debug.WriteLine(entry.FullName);
            if (string.IsNullOrEmpty(entry.FullName))
            {
                continue; // Skip entries with empty names
            }

            if (progressReporter != null)
            {
                progressReporter.ReportProgress($"extracting {entry.FullName}");
            }

            string destinationPath = System.IO.Path.Combine(tempExtractPath, entry.FullName);

            // Handle both files and directories
            if (entry.FullName.EndsWith("/"))
            {
                // Entry represents a directory
                Directory.CreateDirectory(destinationPath);
            }
            else
            {
                // Entry represents a file
                string directoryPath = System.IO.Path.GetDirectoryName(destinationPath);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                entry.ExtractToFile(destinationPath, overwrite: true);
            }

        }
        Process.Start(tempExtractPath);

    }
}
catch (Exception ex)
{ Debug.WriteLine(ex); }

}

© www.soinside.com 2019 - 2024. All rights reserved.