【问题标题】:Extract zip file and overwrite (in the same directory - C#)提取 zip 文件并覆盖(在同一目录中 - C#)
【发布时间】:2021-03-02 00:02:11
【问题描述】:

我正在开始一些 C# 的东西,我想提取并强制覆盖 zip 存档中的所有文件。我知道 Stack 中有许多其他解决方案,但对我没有任何作用:/

我试过这个方法:

try
{
   string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
   Console.WriteLine("Zip's path: " + zipPath);
   string extractPath = Directory.GetCurrentDirectory();

   ZipFile.ExtractToDirectory(zipPath, extractPath);
   return (0); // 0 all fine 
}
catch (Exception)
{
   return (1); // 1 = extract error
}

这个提取器工作正常,但不允许我在提取的同时覆盖文件,它返回错误和异常......我试图看看MS-Documention,但没有成功......

有人知道它是如何工作的吗?

【问题讨论】:

  • 如果文件/文件夹已打开或其他人拥有该文件/文件夹,则您无法写入该文件/文件夹。这是 Windows 的问题,恰好适用于 unzip 方法。
  • @jdweng 感谢您的回复!我正在尝试提取-覆盖只有没有人打开的已关闭文件!
  • 如果新旧文件相同,那么如何知道它们是否被覆盖?
  • @jdweng 他们怎么能一样呢?让我们按照我的例子:我有一个文件夹,其中有一个名为“random.txt”的文本文件,还有一个“update.zip”存档,其中包含另一个“random.txt”。这两个文件“random.txt”的内容是不同的,即使名称相同。我需要提取 zip 存档中的哪个,并替换存档中的哪个
  • 你是对的。 Extract ToDirectory 没有办法覆盖现有文件异常。所以你必须使用 ExtractToFile 方法,并将 overwrite 参数设置为 true。

标签: c# zip extract updates archive


【解决方案1】:

试试这样的。远离我的开发箱,所以这可能需要一些调整,只需从内存中写入即可。

编辑:正如有人提到的,您可以使用具有覆盖选项的 ExtractToFile。 ExtractToDirectory 没有。

基本上你解压缩到一个临时文件夹,然后检查目标文件夹中是否已经存在解压缩文件的名称。如果是这样,它会删除现有文件并将新解压缩的文件移动到目标文件夹。

    try
    {
        string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
        Console.WriteLine("Zip's path: " + zipPath);
        //Declare a temporary path to unzip your files
        string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
        string extractPath = Directory.GetCurrentDirectory();
        ZipFile.ExtractToDirectory(zipPath, tempPath);

        //build an array of the unzipped files
        string[] files = Directory.GetFiles(tempPath);

        foreach (string file in files)
        {
            FileInfo f = new FileInfo(file);
            //Check if the file exists already, if so delete it and then move the new file to the extract folder
            if (File.Exists(Path.Combine(extractPath,f.Name)))
            {
                File.Delete(Path.Combine(extractPath, f.Name));
                File.Move(f.FullName, Path.Combine(extractPath, f.Name));
            }
            else
            {
                File.Move(f.FullName, Path.Combine(extractPath, f.Name));
            }
        }
        //Delete the temporary directory.
        Directory.Delete(tempPath);
        return (0); // 0 all fine 
    }
    catch (Exception)
    {
        return (1); // 1 = extract error
    }

编辑,在事件目录被解压缩(再次,可能需要调整,我没有测试它):

        try
        {
            string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
            Console.WriteLine("Zip's path: " + zipPath);
            //Declare a temporary path to unzip your files
            string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
            string extractPath = Directory.GetCurrentDirectory();
            ZipFile.ExtractToDirectory(zipPath, tempPath);

            //build an array of the unzipped directories:
            string[] folders = Directory.GetDirectories(tempPath);

            foreach (string folder in folders)
            {
                DirectoryInfo d = new DirectoryInfo(folder);
                //If the directory doesn't already exist in the destination folder, move it to the destination.
                if (!Directory.Exists(Path.Combine(extractPath,d.Name)))
                {
                    Directory.Move(d.FullName, Path.Combine(extractPath, d.Name));
                    continue;
                }
                //If directory does exist, iterate through the files updating duplicates.
                else
                {
                    string[] subFiles = Directory.GetFiles(d.FullName);
                    foreach (string subFile in subFiles)
                    {
                        FileInfo f = new FileInfo(subFile);
                        //Check if the file exists already, if so delete it and then move the new file to the extract folder
                        if (File.Exists(Path.Combine(extractPath, d.Name, f.Name)))
                        {
                            File.Delete(Path.Combine(extractPath, d.Name, f.Name));
                            File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
                        }
                        else
                        {
                            File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
                        }
                    }
                }
            }

            //build an array of the unzipped files in the parent directory
            string[] files = Directory.GetFiles(tempPath);

            foreach (string file in files)
            {
                FileInfo f = new FileInfo(file);
                //Check if the file exists already, if so delete it and then move the new file to the extract folder
                if (File.Exists(Path.Combine(extractPath,f.Name)))
                {
                    File.Delete(Path.Combine(extractPath, f.Name));
                    File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                }
                else
                {
                    File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                }
            }
            Directory.Delete(tempPath);
            return (0); // 0 all fine 
        }

【讨论】:

  • 太棒了......哇......真的很好的回复兄弟!有用!现在我只需要弄清楚如何在提取后删除拉链!真的很有帮助!
  • 没那么难,只是一个简单的“File.Delete(file_name);”够了!
  • 你的作品看起来不错,但文件夹似乎有问题:如果我尝试提取文件夹然后替换它,它不起作用!
  • @Zenek 你想用它的所有内容替换一个文件夹吗?如果是这样,那就完全是另一个问题了。
  • 你完全正确。可能我的问题没有正确发布。我的意思是,我需要提取和替换目录中的每种类型的文件,即使它们位于子文件夹中!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 2013-12-28
  • 2013-01-27
  • 2020-02-25
  • 2011-12-27
  • 1970-01-01
  • 2019-02-03
相关资源
最近更新 更多