将文件添加到现有Zip压缩包

5

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

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

Help is appreciated, thank you.

4个回答

10

尝试一下这个方法,在从源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]);
}

这会覆盖现有的压缩文件,我只需要将文件添加到压缩文件中,而不是覆盖它。 - TheUnrealMegashark
@TheUnrealMegashark 我明白你为什么会这样想,我刚刚发现我在两个文件中使用了相同的名称。我已经测试过这段代码并且它是有效的(它可以将新文件添加到现有的zip文件中),重要的是要读取其他ZipFile,然后将附加文件添加到其中。正如我所说,这确实可以实现你想要的功能。 - Mark Hall
@TheUnrealMegashark,我已经更改了代码为我使用的完整演示代码,并重新测试了一遍。请根据您的需要更改路径和文件名。 - Mark Hall
@TheUnrealMegashark 我没有测试过那种情况,给我几分钟,我会测试一下。 - Mark Hall
最后一个问题,我如何从zip文件中删除一个目录及其所有内容?我可以删除一个目录,但只有在它为空时才能删除它,如果它里面有文件,我就不能删除它。 - TheUnrealMegashark
显示剩余9条评论

1

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


0
尝试创建新的压缩文件:
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

它只是覆盖它。 - TheUnrealMegashark
添加了你可能想要的内容。 - EZLearner
它覆盖了原始的压缩文件,我需要将文件添加到原始的压缩文件中而不是覆盖它。 - TheUnrealMegashark

0

使用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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接