Search code examples
c#zipcompressionarchivec#-ziparchive

Adding files into a folder inside a zip file in c#


My code to zip files is as follows

ZipArchive zip = ZipFile.Open(destToZip, ZipArchiveMode.Create);
zip.CreateEntry("pubEd/");

string[] fileEntries = Directory.GetFiles(dirToZip);
foreach (string fileName in fileEntries)
    zip.CreateEntryFromFile(fileName,Path.GetFileName(fileName), CompressionLevel.Optimal);

zip.Dispose();

In the second line of the code once after creating a zip file I create a folder with a name pubEd inside the zip file.

In the next line I am adding files to the zip folder.

What is happening is files get added to the zip directly.

I want to add these files inside the directory which I created inside the zip.

How do I do that?


Solution

  • By the looks of it you would changePath.GetFileName(fileName) to "pubEd/" + Path.GetFileName(fileName). And get rid of the second line. Thats just based on my reading of the documentation. I have not actually tried it.