2

From examples, I've got a pretty good grasp over how to extract a zip file.

In nearly every example, the method of identifying when a ZipEntry is a directory is as follows

string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);

if (directoryName.Length > 0)
  Directory.CreateDirectory(Path.Combine(destinationDirectory, directoryName));                    

if (fileName != String.Empty)
{
  //read data and write to file
}

Now is is fine and all (directory encountered, create it), directory is available when the file is extracted.

I can add files to a zip fine, but how do I add folders? I understand I'll be looping through the directories, adding the files encountered (and their ZipEntry.Name property is populated properly), but how do I add a ZipEntry to the archive and instruct the ZipOutputStream that it is a directory?

MoSlo
  • 2,780
  • 4
  • 33
  • 37

1 Answers1

1

ZipFile.AddDirectory does what you want. Small sample code here.

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • 2
    Thanks, I've gone to using a ZipInputStream for extracting and utilizing the ZipFile class for creating. Once I knew what to look for, some searching led me to this article, which helped: http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx – MoSlo Jun 17 '11 at 11:05