c# 使用 ZipArchive 但不使用 ZipFile 时出现 UnauthorizedAccessException [英] c# UnauthorizedAccessException when using ZipArchive but not ZipFile

查看:25
本文介绍了c# 使用 ZipArchive 但不使用 ZipFile 时出现 UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在以下测试代码中使用 ZipFile.CreateFromDirectory 压缩特定文件夹中的文件(我仅使用此代码来测试压缩的工作原理):

I am able to zip files from a specific folder using ZipFile.CreateFromDirectory in the following test code (I only used this code to test how zipping works):

// Where the files are located
string strStartPath = txtTargetFolder.Text;

// Where the zip file will be placed
string strZipPath = @"C:\Users\smelmo\Desktop\testFinish\" + strFileNameRoot + "_" + txtDateRange1.Text.Replace(@"/", "_") + "_" + txtDateRange2.Text.Replace(@"/", "_") + ".zip";

ZipFile.CreateFromDirectory(strStartPath, strZipPath);

但是,这会将文件夹中的所有内容压缩在一起.我正在尝试使用以下代码中的 ZipArchive 将文件夹中的特定项目压缩在一起:

However, this zips together ALL of the contents in the folder. I am trying to zip together specific items in the folder using ZipArchive in the following code:

// Where the files are located
string strStartPath = txtTargetFolder.Text;

// Where the zip file will be placed
string strZipPath = @"C:\Users\smelmo\Desktop\testFinish\" + strFileNameRoot + "_" + txtDateRange1.Text.Replace(@"/", "_") + "_" + txtDateRange2.Text.Replace(@"/", "_") + ".zip";

using (ZipArchive archive = ZipFile.OpenRead(strStartPath))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        if (!(entry.FullName.EndsWith(".TIF", StringComparison.OrdinalIgnoreCase)))
        {
            entry.ExtractToFile(Path.Combine(strZipPath, entry.FullName));
         }
     }
 }

它在 ZipFile.OpenRead(strStartPath) 处给出错误.为什么我可以访问第一个代码块中的确切文件夹,但不能访问第二个?或者是否有更简单的方法来搜索文件夹并仅压缩特定项目?

It is giving the error at ZipFile.OpenRead(strStartPath). Why am I able to access the exact folder in the first block of code but not the second? Or is there an easier way to search through a folder and only zip specific items?

推荐答案

您错误地使用了 Zip 库

You are utilizing the Zip libraries wrong

实际上,您试图打开一个目录,就好像它是一个 zip 文件,然后遍历该目录的内容(实际上也是一个 zip 文件),然后尝试将每个成员解压缩到 一个不同的 zip 文件

Effectively you are trying to open a directory as if it were a zip file, then loop over the contents of that directory (which again is actually a zip file) and then attempting to extract each member into a different zip file

以下是您所描述的您正在尝试执行的操作的有效示例:

Here is a working example of what you have described you are trying to do:

string strStartPath = @"PATH TO FILES TO PUT IN ZIP FILE";

string strZipPath = @"PATH TO ZIP FILE";

if (File.Exists(strZipPath))
    File.Delete(strZipPath);

using (ZipArchive archive = ZipFile.Open(strZipPath,  ZipArchiveMode.Create))
{
    foreach (FileInfo file in new DirectoryInfo(strStartPath).GetFiles())
    {
        if (!(file.FullName.EndsWith(".TIF", StringComparison.OrdinalIgnoreCase)))
        { 
            archive.CreateEntryFromFile(Path.Combine(file.Directory.ToString(), file.Name), file.Name);
        }
    }
}

这将获取文件夹的所有根级内容并将其放入 zip 文件中.您将需要实现自己的递归获取子文件夹及其内容的方法,但这超出了本问题的范围.

This will take all the root level contents of a folder and put it in the zip file. You will need to implement your own way of getting subfolders and their contents recursively, but that is beyond the scope of this question.

这是一个工作示例,具有适当的文件夹递归以选择即使在子目录中的所有文件

Here is a working example with proper folder recursion to select all files even in subdirectories

public void ZipFolder()
{
    string strStartPath = @"PATH TO FILES TO PUT IN ZIP FILE";

    string strZipPath = @"PATH TO ZIP FILE";

    if (File.Exists(strZipPath))
        File.Delete(strZipPath);

    using (ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create))
    {
        foreach (FileInfo file in RecurseDirectory(strStartPath))
        {
            if (!(file.FullName.EndsWith(".TIF", StringComparison.OrdinalIgnoreCase)))
            {
                var destination = Path.Combine(file.DirectoryName, file.Name).Substring(strStartPath.Length + 1);
                archive.CreateEntryFromFile(Path.Combine(file.Directory.ToString(), file.Name), destination);
            }
        }
    }
}

public IEnumerable<FileInfo> RecurseDirectory(string path, List<FileInfo> currentData = null)
{
    if (currentData == null)
        currentData = new List<FileInfo>();   

    var directory = new DirectoryInfo(path);

    foreach (var file in directory.GetFiles())
        currentData.Add(file);

    foreach (var d in directory.GetDirectories())
        RecurseDirectory(d.FullName, currentData);
    return currentData;
}

这篇关于c# 使用 ZipArchive 但不使用 ZipFile 时出现 UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆