【问题标题】:Add Files Into Existing Zip将文件添加到现有 Zip
【发布时间】:2013-08-06 21:54:33
【问题描述】:

我可以成功地将 zip 文件夹中的文件提取到文件夹中,但我不太确定如何获取这些文件并将它们添加到现有的 zip 文件中。我将它们解压到桌面上一个名为“mod”的目录中,然后我需要将它们添加到另一个 zip 文件中。帮助?这是我的提取代码-

ZipFile zip = ZipFile.Read(myZip);
zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);

感谢您的帮助,谢谢。

【问题讨论】:

    标签: c# dotnetzip


    【解决方案1】:

    尝试一下,从源 zip 中提取文件后,您需要将目标 zip 文件读入 ZipFile 对象,然后您可以使用 AddFiles 方法将源文件添加到目标文件,然后保存。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 
    using Ionic.Zip;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string myZip = @"C:\temp\test.zip";
                string myOtherZip = @"C:\temp\anotherZip.zip";
                string outputDirectory = @"C:\ZipTest";
    
                using (ZipFile zip = ZipFile.Read(myZip))
                {
                    zip.ExtractAll(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
                }
    
                using (ZipFile zipDest = ZipFile.Read(myOtherZip))
                {
                    //zipDest.AddFiles(System.IO.Directory.EnumerateFiles(outputDirectory)); //This will add them as a directory
                    zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root
                    zipDest.Save();
                }
            }
        }
    }
    

    将目录添加到 ZipFile 的修改方法(这将适用于单个子目录级别

    using (ZipFile zipDest = ZipFile.Read(myOtherZip))
    {
        foreach (var dir in System.IO.Directory.GetDirectories(outputDirectory))
        {
            zipDest.AddFiles((System.IO.Directory.EnumerateFiles(dir)),false,outputDirectory ); //directory to the root
        }
        zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root
        zipDest.Save();
    }
    

    从 Zip 目录中删除文件的方法

    List<string> files = zipDest.EntryFileNames.ToList<string>(); // Get List of all the archives files
    for (int i = 0; i < files.Count; i++)
    {
        if(files[i].Contains("ZipTest")) //replace the directory you wish to delete files from here
            zipDest.RemoveEntry(files[i]);
    }
    

    【讨论】:

    • 这会覆盖现有的 zip,我只需要将文件添加到 zip 中,而不是覆盖它。
    • @TheUnrealMegashark 我知道你会怎么想,我刚刚发现我对两个文件使用了相同的名称。我确实测试了这段代码,它确实有效(它将新文件添加到现有的 zip 文件中)重要的部分是读取其他 ZipFile,然后将其他文件添加到其中。正如我所说,这确实希望你想要它。
    • @TheUnrealMegashark 我将代码更改为我使用并重新测试的完整演示代码。更改路径和文件名以适合您。
    • 如果原始zip中有文件夹怎么办?
    • @TheUnrealMegashark 我没有测试那个场景,给我几分钟,我会的。
    【解决方案2】:

    尝试创建一个新的 zip:

    using (ZipFile zip = new ZipFile())
    {
      zip.AddFile("1.txt");
      zip.AddFile("favicon.png");
      zip.AddFile("ElectricityMagnetism.pdf");        
      zip.Save("BlahBlah.zip");
    }
    

    要将文件添加到 zip,请尝试:

    string[] filePaths = new String[] { ... } // Your file paths
    foreach (string path in filePaths)
        zip.AddFile(path);
    zip.Save("..."); // ZIP path
    

    【讨论】:

    • 它只是覆盖它。
    • 添加了您可能想要的内容
    • 它会覆盖原始 zip,我需要它来将文件添加到原始 zip 而不会覆盖它。
    【解决方案3】:

    7-Zip 有一个可以使用的命令行可执行文件。

    public static void AddFileToZip(string zipPath, string filePath)
    {
        if (!File.Exists(zipPath))
            throw new FileNotFoundException("Zip was not found.", zipPath);
        if (!File.Exists(filePath))
            throw new FileNotFoundException("File was not found.", filePath);
    
        // Create the commandline arguments.
        string argument = "a -tzip \"" + zipPath + "\" \"" + zipPath + "\"";
    
        // Run the 7-Zip command line executable with the commandline arguments.
        System.Diagnostics.Process process = System.Diagnostics.Process.Start("7za.exe", argument);
        process.WaitForExit();
    }
    

    见:https://www.dotnetperls.com/7-zip-examples

    【讨论】:

      【解决方案4】:

      使用ionic.zip,C#递归函数路径上的所有目录

      static private void CompressDirRecursive(string path, ref Ionic.Zip.ZipFile dzip)
      {
          dzip.AddFiles((System.IO.Directory.GetFiles(path)), false, path); // ROOT
      
          foreach (string dir in System.IO.Directory.GetDirectories(path))
          {
              CompressDirRecursive(dir, ref dzip);// NEXT DIRECTORY
          }
      }
      
      static private void MyTestFunction()
      {
          Ionic.Zip.ZipFile dzip = new Ionic.Zip.ZipFile();
          System.IO.MemoryStream msOut = new System.IO.MemoryStream();
          CompressDirRecursive("ruta que quieras", ref dzip);
          try
          {
              dzip.Save(outputStream: msOut);
          }
          catch (Exception ex)
          {
              throw ex;
          }
      
          dzip.Dispose();
          // etc
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多