将文件添加到现有Zip中

时间:2013-08-06 21:54:33

标签: c# dotnetzip

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

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

非常感谢帮助,谢谢。

4 个答案:

答案 0 :(得分:8)

尝试尝试一下,一旦从源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]);
}

答案 1 :(得分:0)

尝试创建一个新的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

答案 2 :(得分:0)

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

答案 3 :(得分: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
}