DotNetZip Extract Folder & Contents based on folder comment

487 views Asked by At

I have some code that adds different directories to a zip file. Its important that I know each folder based on its comment, during the extraction process. Here is the zip sample code:

   foreach (string folder in BackupDIRS)
        {

            string Source = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folder);

            string Folder = Path.GetFileName(Path.GetDirectoryName(Source));


            ZipEntry e = zip.AddDirectory(Source, Folder);
            e.Comment = "comment here";


        }

Here is the code for unzipping:

  using (ZipFile zip1 = ZipFile.Read(src))
                {

                    foreach (ZipEntry e in zip1.Entries)
                    {

                        // e.comment will be null on actual files.
                    }
                }

The actual entry points for the folder have comments but their files dont, which presents a problem since it will cause most entries to have null comments.

How do I make the files have the same comment as the folder, or does DotNetZip extract directory files sequentially, meaning if its null I could use the last non null value because it would be that folder's files.

1

There are 1 answers

0
Rob On

After calling ZipEntry e = zip.AddDirectory(Source, Folder); you can iterate through all files in ZipEntry and assign a comment:

using (var zipFile = new ZipFile(zipFilePath))
{
    var addDirectory = zipFile.AddDirectory(directoryPathToAdd, "directory");
    addDirectory.Comment = "directory comment";

    var zipEntries = zipFile.Entries
        .Where(x => !x.IsDirectory)
        .Where(x => x.FileName.StartsWith("directory"));

    foreach (var zipentry in zipEntries)
        zipentry.Comment = "zip entry comment";

    zipFile.Save();
}

Hope it helps.