5

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

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

感谢您的帮助,谢谢。

4

4 回答 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]);
}
于 2013-08-06T22:48:26.883 回答
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
于 2013-08-06T22:00:27.667 回答
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

于 2017-02-22T14:24:37.037 回答
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
}
于 2017-03-22T13:09:00.040 回答