0

I am trying to zip together all folders and their contents into a zip file. From what I have researched (through StackOverflow and Microsoft), Directory.CreateDirectory(Path) should create the given directory. However, my ZipFile is showing up empty.

For example, I have a folder (C:\Users\smelmo\Desktop\test) with the subfolders (0001, 0002) in it. Within 0001 and 0002 are other documents. I am wanting to zip 0001 and 0002 together within the test folder. This is what I have so far:

// Open the directory of the target folder
using (ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create))
{
    // Grab each directory within the target folder
    foreach (var directoryName in Directory.GetDirectories(strStartPath))
    {
         // Add each directory to the ZipFile
         Directory.CreateDirectory(directoryName);
    }
}

This does not produce anything. HOWEVER, I also have code that will grab ALL files within the subfolders and place them in the ZipFile.

// Open the directory of the target folder
using (ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create))
{
    // Grab each directory within the target folder
    foreach (var directoryName in Directory.GetDirectories(strStartPath))
    {
        //Grab all files in each directory
        foreach (var filePath in Directory.GetFiles(directoryName))
        {
            var fileName = Path.GetFileName(@filePath);

            // Place each directory and its repsective files in the zip file
            var entry = archive.CreateEntryFromFile(filePath, fileName);         
         }
    }
}

But this does not place subfolders 0001 and 0002 in it, just the CONTENTS of 0001 and 0002. I am wanting 0001 and 0002 and their respective contents.

stuartd
  • 70,509
  • 14
  • 132
  • 163
smelmo
  • 75
  • 1
  • 10
  • 4
    `Directory.CreateDirectory` _only works on the file system_ so you are recreating directories that already exist! See [Creating Directories in a ZipArchive](https://stackoverflow.com/questions/15133626/creating-directories-in-a-ziparchive-c-sharp-net-4-5) – stuartd Jun 20 '17 at 15:57

1 Answers1

1

Edited:

If you are looking for more control, you can just create a function that adds files to a zip archive. You can then use recursion to add any subfolders and files.

First you can define a function that accepts a zip archive to add files to:

public void AddFolderToZip(ZipArchive archive, string rootPath, string path, bool recursive) 
{
    foreach (var filePath in Directory.GetFiles(path))
    {               
        //Remove root path portion from file path, so entry is relative to root
        string entryName = filePath.Replace(rootPath, "");
        entryName = entryName.StartsWith("\\") ? entryName.Substring(1) : entryName;

        var entry = archive.CreateEntryFromFile(filePath, entryName);
    }

    if (recursive)
    {
        foreach (var subPath in Directory.GetDirectories(path))
        {
            AddFolderToZip(archive, rootPath, subPath, recursive);
        }
    }
}

Then you can call it using the following code:

string strStartPath = @"C:\Test\zip";
string strZipPath = @"C:\Test\output.zip";
ZipArchive archive = ZipFile.Open(strZipPath, ZipArchiveMode.Create);
bool recursive = true;

foreach (var directoryPath in Directory.GetDirectories(strStartPath))
{
    AddFolderToZip(archive, strStartPath, directoryPath, recursive);
}

archive.Dispose();

This code adds each directory it finds in a top level directory. And for each directory it finds, it will use recursion to add any subdirectories or files found within those.

Ehz
  • 2,027
  • 1
  • 12
  • 11
  • This is currently taking all files from the strStartPath, which is a directory that contains both files and sub directories. I cannot seem to figure out how to just grab the sub directories and the files within the sub directories. – smelmo Jun 20 '17 at 18:28
  • This is great. So basically there is no function that will directly zip subfolders and its contents, making recursion the only way to accomplish this? – smelmo Jun 20 '17 at 19:30
  • Yeah, as far as I know (for this library) it is either add everything using ZipFile.CreateFromDirectory or add entries individually – Ehz Jun 20 '17 at 20:11