6

what I'm trying to do is add a list of folders and files all to the root of my Zip file, using the Ionic Zip library (c#).

Here's what I have so far

string k = "B:/My Documents/Workspace";
private void button1_Click(object sender, EventArgs e)
{
   using (ZipFile zip = new ZipFile())
   {   
       //add directory, give it a name
       zip.AddDirectory(k);
       zip.Save("t.zip");
   }
}

Now, I want my zip to be looking like this.

  • t.zip
    • Random Files and Folder

But it's looking like this.

  • t.zip
    • t (folder)
      • Random files and folders

Any help would be appreciated, Thank you.

Guido
  • 46,642
  • 28
  • 120
  • 174
Movieboy
  • 373
  • 1
  • 3
  • 16

3 Answers3

10

The default behavior of AddDirectory should add the contents of the directory to the root path in the zipfile, without creating a subdirectory.

There is a second overload of AddDirectory, which adds a parameter to specify what the path of the added files should be within the zipfile. But since you want the files to go into the root directory, this would just be an empty string.

zip.AddDirectory(k, "");

See this documentation for more info.

None of this explains where your subfolder is coming from. I suspect the problem is from something else in the code. It might be useful to run this in debug and see what 'k' equals when you call AddDirectory, or to print out all the "entries" in the zip.Entries collection.

Slider345
  • 4,558
  • 7
  • 39
  • 47
  • Thanks I'm going to try that out – Movieboy Oct 29 '12 at 23:40
  • Okay, I tried that and it still created the subfolder, any other ideas? – Movieboy Oct 31 '12 at 02:45
  • In my testing, AddDirectory isn't creating a subfolder--even if I don't pass the empty string, it still doesn't create a subfolder. Is there anything else in your code that might be creating that folder? – Slider345 Oct 31 '12 at 17:02
  • Not that I know of, that's literally the only code I have, word for word lol, and I tried it with all different types of data. I could swear though that at one point, it worked as intended and only recently did is start doing that. Let me check my libraries and the version of Ionic that I'm using – Movieboy Nov 01 '12 at 23:36
2

Try this

using (ZipFile zip = new ZipFile())
    {
        zip.AddFiles(sourcefiles, ""); //For saving things at root of your zip
        foreach (DirectoryInfo objFile in objDir.GetDirectories())
        {
            zip.AddDirectory(objFile.FullName, objFile.Name); ///For saving subdirectories within zip
        }
        zip.Save(pathzipfile + ".zip");
    }
Nitin Soni
  • 87
  • 1
  • 13
  • this is great! thanks buddy. you did awesome job by using this line of code zip.AddFiles(sourcefiles, ""); //For saving things at root of your zip – Zia Ur Rahman Nov 02 '18 at 15:22
0

Why not try something like this:

foreach (var file in System.IO.Directory.GetFiles(k))
{
    zip.AddFile(file);
}

To get the directories and files to an unlimited depth, combine that with a recursion method based on System.IO.Directory.GetDirectories("path") - should be pretty straightforward methinks.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • 1
    Oh yeah and testing just now, I did this: `var imagePath = basePath + "Content\\Images\\"; zip.AddDirectory(imagePath);` - it added the files and folders from my Images folder recursively straight to the root like you would want yours to do. – Joel Peltonen Jul 23 '13 at 13:19