提取zip文件并覆盖(在同一目录中-C#)

泽内克

我正在开始一些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,但没有成功...

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

米凯尔

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

编辑:如前所述,您可以使用具有覆盖选项的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 
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

提取zip文件并覆盖(在同一目录中-C#)

来自分类Dev

netbeans在同一目录中找不到C ++头文件

来自分类Dev

MVC C#网站+ WordPress位于同一目录

来自分类Dev

如何在不使用“ ..”的情况下包含与当前源文件不在同一目录中的C ++头文件?

来自分类Dev

多个线程生成的进程将文件复制到同一目录C#时缺少文件条目

来自分类Dev

多个线程生成的进程将文件复制到同一目录C#时缺少文件条目

来自分类Dev

致命错误 C1083:无法打开包含文件,但它在同一目录中

来自分类Dev

io.py在同一目录中时,pandas ImportError C扩展

来自分类Dev

io.py在同一目录中时,pandas ImportError C扩展

来自分类Dev

比较同一目录中的文件

来自分类Dev

从同一目录中的文件读取

来自分类Dev

如何在C#中允许来自同一目录的单个实例应用程序但来自不同目录的多个实例?

来自分类Dev

将相应文件排序到同一目录中

来自分类Dev

Python在同一目录中找不到文件

来自分类Dev

打开不在同一目录中的文件

来自分类Dev

在python中从同一目录修改多个.csv文件

来自分类Dev

如何在同一目录中复制文件

来自分类Dev

在同一目录中创建文件的副本

来自分类Dev

重命名同一目录中的文件-Shell脚本

来自分类Dev

如何从python中的同一目录导入文件?

来自分类Dev

如何让我的scrapy读取同一目录中的文件?

来自分类Dev

在C#中覆盖txt文件

来自分类Dev

使用C将文件从一个目录复制到另一目录。在发送目录中打开文件时遇到麻烦。没有这样的文件或目录

来自分类Dev

合并目录中的文件C#

来自分类Dev

读取文件到内存用户操作信息,然后将数据覆盖回 C# 中的同一文件

来自分类Dev

使用C#提取zip文件时路径中的非法字符

来自分类Dev

ImportError与模块在同一目录中

来自分类Dev

同一目录中同一文件的不同MD5Sum

来自分类Dev

有没有一种方法可以将所选文本提取到同一目录中的新文件中?

Related 相关文章

  1. 1

    提取zip文件并覆盖(在同一目录中-C#)

  2. 2

    netbeans在同一目录中找不到C ++头文件

  3. 3

    MVC C#网站+ WordPress位于同一目录

  4. 4

    如何在不使用“ ..”的情况下包含与当前源文件不在同一目录中的C ++头文件?

  5. 5

    多个线程生成的进程将文件复制到同一目录C#时缺少文件条目

  6. 6

    多个线程生成的进程将文件复制到同一目录C#时缺少文件条目

  7. 7

    致命错误 C1083:无法打开包含文件,但它在同一目录中

  8. 8

    io.py在同一目录中时,pandas ImportError C扩展

  9. 9

    io.py在同一目录中时,pandas ImportError C扩展

  10. 10

    比较同一目录中的文件

  11. 11

    从同一目录中的文件读取

  12. 12

    如何在C#中允许来自同一目录的单个实例应用程序但来自不同目录的多个实例?

  13. 13

    将相应文件排序到同一目录中

  14. 14

    Python在同一目录中找不到文件

  15. 15

    打开不在同一目录中的文件

  16. 16

    在python中从同一目录修改多个.csv文件

  17. 17

    如何在同一目录中复制文件

  18. 18

    在同一目录中创建文件的副本

  19. 19

    重命名同一目录中的文件-Shell脚本

  20. 20

    如何从python中的同一目录导入文件?

  21. 21

    如何让我的scrapy读取同一目录中的文件?

  22. 22

    在C#中覆盖txt文件

  23. 23

    使用C将文件从一个目录复制到另一目录。在发送目录中打开文件时遇到麻烦。没有这样的文件或目录

  24. 24

    合并目录中的文件C#

  25. 25

    读取文件到内存用户操作信息,然后将数据覆盖回 C# 中的同一文件

  26. 26

    使用C#提取zip文件时路径中的非法字符

  27. 27

    ImportError与模块在同一目录中

  28. 28

    同一目录中同一文件的不同MD5Sum

  29. 29

    有没有一种方法可以将所选文本提取到同一目录中的新文件中?

热门标签

归档