5

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.

TheUnrealMegashark
  • 309
  • 1
  • 6
  • 19

4 Answers4

10

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]);
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • This overwrites the existing zip, I need to just ADD the files into the zip, not overwrite it. – TheUnrealMegashark Aug 07 '13 at 02:26
  • @TheUnrealMegashark I see how you would think that, I just caught that I was using the same name for both files. I did test out this code and it does work( it adds new files to an existing zip file) the important part is to Read the other ZipFile then add the additional files to it. As I said this does do want you want it to. – Mark Hall Aug 07 '13 at 03:05
  • @TheUnrealMegashark I changed the code to the full demo code that I used and retested. Change the Paths and File Names to work for you. – Mark Hall Aug 07 '13 at 03:16
  • What if there's a folder in the original zip? – TheUnrealMegashark Aug 07 '13 at 03:29
  • @TheUnrealMegashark I did not test that scenario, give me a few minutes and I will. – Mark Hall Aug 07 '13 at 03:30
  • @TheUnrealMegashark I added a example of how to add a directory to the zip archive, to add multiple levels you will need to flesh it out more. Right now it works for files in the root and one level up – Mark Hall Aug 07 '13 at 03:53
  • Also I just needed to move the folders into the new zip. Not the items in the folder. I figured it out, but it doesn't include the initial folder, only what's in it. – TheUnrealMegashark Aug 07 '13 at 04:33
  • I need to if the file exists in the zip "myOtherZip" file then to overwrite it. How can I do this, because right now it justs tells me it already exists. – TheUnrealMegashark Aug 07 '13 at 04:56
  • @TheUnrealMegashark That is what the last AddFiles does, It adds the files from root. You can experiment with the UpdateFile Method. Something like `zipDest.UpdateFiles(System.IO.Directory.EnumerateFiles(dir), outputDirectory); ` or `zipDest.UpdateFiles(System.IO.Directory.EnumerateFiles(outputDirectory), "");` to see if that is closer to what you are looking for. – Mark Hall Aug 07 '13 at 05:24
  • One last thing, how can I remove a directory and all its contents from a zip? I can remove a directory currently, but I cannot while it has files in it- only when it's empty. – TheUnrealMegashark Aug 07 '13 at 05:44
  • @TheUnrealMegashark I believe it is like standard Dos, you can not remove a directory if it has any contents, only an empty directory. What you are seeing is normal behavior, You will need to remove all contents before you can delete it. – Mark Hall Aug 07 '13 at 06:00
  • How can I get the contents deleted? Sorry, I'm a bit new at zip files and such. – TheUnrealMegashark Aug 07 '13 at 06:01
  • @TheUnrealMegashark I added an example on how you can remove the files from the archive. – Mark Hall Aug 07 '13 at 06:18
  • Nvm I just had to save it – TheUnrealMegashark Aug 07 '13 at 06:42
1

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

Deantwo
  • 1,113
  • 11
  • 18
0

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
EZLearner
  • 1,614
  • 16
  • 25
0

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
}
Furkan Ekinci
  • 2,472
  • 3
  • 29
  • 39