问题
I can successfully extract files from a zip folder into a folder, but I am not quite sure how to take those files and add them into an existing zip file. I extract them into a directory onto the desktop called "mod", and then I need to add them to another zip file. Help? Here is my extraction code-
ZipFile zip = ZipFile.Read(myZip);
zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);
Help is appreciated, thank you.
回答1:
Try giving this a try, once you extract the files from the source zip, you will need to read the destination zip file into a ZipFile
object you can then use the AddFiles
method to add your source files to the destination file, then save it.
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();
}
}
}
}
Modified Method for adding a Directory to the ZipFile ( This will work for a single sub directory level )
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();
}
Method for deleting files from a Zip Directory
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]);
}
回答2:
Try to create a new zip:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("1.txt");
zip.AddFile("favicon.png");
zip.AddFile("ElectricityMagnetism.pdf");
zip.Save("BlahBlah.zip");
}
To add files to a zip, try:
string[] filePaths = new String[] { ... } // Your file paths
foreach (string path in filePaths)
zip.AddFile(path);
zip.Save("..."); // ZIP path
回答3:
7-Zip has a commandline executable that can he used.
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();
}
See: https://www.dotnetperls.com/7-zip-examples
回答4:
Recursive function all directories on path with 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
}
来源:https://stackoverflow.com/questions/18091202/add-files-into-existing-zip