35

我正在使用以下代码提取文件夹中的所有文件

        using (ZipArchive archive = new ZipArchive(zipStream))
        {
            archive.ExtractToDirectory(location);
        }

但是如果存在一个文件,那么它会引发异常。有什么方法可以告诉 Compression API 替换现有文件。

我发现一种方法是先获取所有文件名,然后检查文件是否存在并删除它。但这对我来说是非常昂贵的。

4

11 回答 11

62

我创建了一个扩展。任何对其改进的评论将不胜感激,

public static class ZipArchiveExtensions
{
    public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
    {
        if (!overwrite)
        {
            archive.ExtractToDirectory(destinationDirectoryName);
            return;
        }

        DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
        string destinationDirectoryFullPath = di.FullName;

        foreach (ZipArchiveEntry file in archive.Entries)
        {
            string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));

            if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
            {
                throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
            }

            if (file.Name == "")
            {// Assuming Empty for Directory
                Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
                continue;
            }
            file.ExtractToFile(completeFileName, true);
        }
    }
}
于 2013-02-10T07:40:48.557 回答
20

当文件夹不存在时,此代码不会引发异常,而是会创建文件夹。

public static class ZipArchiveExtensions
{
    public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
    {
        if (!overwrite)
        {
            archive.ExtractToDirectory(destinationDirectoryName);
            return;
        }
        foreach (ZipArchiveEntry file in archive.Entries)
        {
            string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
            string directory = Path.GetDirectoryName(completeFileName);

            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            if (file.Name != "")
                file.ExtractToFile(completeFileName, true);
        }
    }
}
于 2015-05-24T15:23:01.547 回答
8

看看这个:在 .NET 4.5 中轻松创建 zip 文件。你的问题似乎得到了解决。或者,您也可以检查DotNetZip

于 2013-02-10T06:25:45.290 回答
6

由于我是 Linq 的忠实粉丝,Linq-ish 方式仅供参考:

using (var strm = File.OpenRead(zipPath))
using (ZipArchive a = new ZipArchive(strm))
{
    a.Entries.Where(o => o.Name == string.Empty && !Directory.Exists(Path.Combine(basePath, o.FullName))).ToList().ForEach(o => Directory.CreateDirectory(Path.Combine(basePath, o.FullName)));
    a.Entries.Where(o => o.Name != string.Empty).ToList().ForEach(e => e.ExtractToFile(Path.Combine(basePath, e.FullName), true));
}
于 2018-06-21T08:50:02.210 回答
5

您可以将文件提取到某个临时目录,然后使用“File.Copy”和 ovveride 选项将文件复制到目标目录

我知道这不是一个完美的解决方案,但是这样你就不需要检查文件是否存在

于 2013-02-10T06:32:09.160 回答
4

这是一个获取 zip 文件路径的方法。

基于接受的答案。

    public void ExtractZipFileToDirectory(string sourceZipFilePath, string destinationDirectoryName, bool overwrite)
    {
        using (var archive = ZipFile.Open(sourceZipFilePath, ZipArchiveMode.Read))
        {
            if (!overwrite)
            {
                archive.ExtractToDirectory(destinationDirectoryName);
                return;
            }

            DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
            string destinationDirectoryFullPath = di.FullName;

            foreach (ZipArchiveEntry file in archive.Entries)
            {
                string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));

                if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
                {
                    throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
                }

                if (file.Name == "")
                {// Assuming Empty for Directory
                    Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
                    continue;
                }
                file.ExtractToFile(completeFileName, true);
            }
        }
    }
于 2019-07-05T12:41:01.003 回答
3

自 .NET Standard 2.1 起,只需在以下位置设置overwriteFiles为 true 即可: ZipFile.ExtractToDirectory(string sourceFile, string destDir, Encoding entryNameEncoding, bool overwriteFiles)

Example:
ZipFile.ExtractToDirectory("c:\\file.zip","c:\\destination_folder", Encoding.UTF8, true);
于 2020-04-09T22:15:40.343 回答
1

嗨,我正在使用从 nugget 下载的 DotNetZip。我只是简单地使用这段代码。如果存在,这将自动替换目录中的文件。“静默覆盖”!

using (ZipFile archive = new ZipFile(@"" + System.Environment.CurrentDirectory + "\\thezipfile.zip"))
{
     archive.ExtractAll(@"" + System.Environment.CurrentDirectory, ExtractExistingFileAction.OverwriteSilently);
}
于 2019-05-21T05:23:15.427 回答
0

从答案中重新制作的方法来创建所有文件夹

public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
{
    if (!overwrite)
    {
        archive.ExtractToDirectory(destinationDirectoryName);
        return;
    }
    foreach (ZipArchiveEntry file in archive.Entries)
    {
        string completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
        if (file.Name == "")
        {// Assuming Empty for Directory
            Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
            continue;
        }
        // create dirs
        var dirToCreate = destinationDirectoryName;
        for (var i = 0; i < file.FullName.Split('/').Length - 1; i++)
        {
            var s = file.FullName.Split('/')[i];
            dirToCreate = Path.Combine(dirToCreate, s);
            if (!Directory.Exists(dirToCreate))
                Directory.CreateDirectory(dirToCreate);
        }
        file.ExtractToFile(completeFileName, true);
    }
}
于 2018-04-06T20:52:37.400 回答
0

当您有 zip 文件路径时,这很有用

public static class ZipArchiveHelper
    {
        public static void ExtractToDirectory(string archiveFileName, string destinationDirectoryName, bool overwrite)
        {
            if (!overwrite)
            {
                ZipFile.ExtractToDirectory(archiveFileName, destinationDirectoryName);
            }
            else
            {
                using (var archive = ZipFile.OpenRead(archiveFileName))
                {

                    foreach (var file in archive.Entries)
                    {
                        var completeFileName = Path.Combine(destinationDirectoryName, file.FullName);
                        var directory = Path.GetDirectoryName(completeFileName);

                        if (!Directory.Exists(directory) && !string.IsNullOrEmpty(directory))
                            Directory.CreateDirectory(directory);

                        if (file.Name != "")
                            file.ExtractToFile(completeFileName, true);
                    }

                }
            }            
        }
    }
于 2019-02-25T18:13:02.920 回答
0

看起来躲避包含该代码块的唯一方法是在使用以下命令提取存档之前简单地删除文件del

del (location + "\*")
using (ZipArchive archive = new ZipArchive(zipStream))
{
  archive.ExtractToDirectory(location);
}

这不完全是 OP 想要的,但它很紧凑。

于 2021-11-18T16:04:22.413 回答