1

The files I want to zip are in different folders

The folder structure is as follows:

FileId/FileType/File.extension
FileId/FileType/File.extension

I'm trying to create a zip with the following structure

FileType/File.extension
FileType/File.extension

I have tried a stackoverflow link but couldn't get it to work in my scenario.

I have the files in the zip directly. But I couldn't find a good source to have the files in a specific folder.

Code I have referred from previous posts:

using (ZipArchive archive = ZipFile.Open(zipPath + @"\release.zip", ZipArchiveMode.Update))
        {
            ZipArchiveEntry readmeEntry;
            DirectoryInfo d = new DirectoryInfo(folderToAdd);
            FileInfo[] Files = d.GetFiles("*");
            foreach (FileInfo file in Files)
            {
                archive.CreateEntry(preset.FileFormat);
                readmeEntry = archive.CreateEntryFromFile(newFile, fileName);
            }
        }

I also tried creating a zip file in update mode and then as follows:

ZipFile.Open(zipPath + @"\release.zip", ZipArchiveMode.Update);
ZipFile.CreateFromDirectory(folderToAdd, zipPath + @"\release.zip", CompressionLevel.Fastest, true);

But the CreateFromDir breaks with an exception saying that the release.zip already exists.

Should I copy my files into respective folders and then apply the CreateFromDir method, or there is a better way to do this without re-copying and then CreateFromDir on top.

Binoy Cherian
  • 364
  • 1
  • 6
  • 23
  • So you want to update zip file or create new one? – Renatas M. Nov 30 '18 at 10:32
  • @Reniuz, Keep/Create one zip file called release.zip with the following conditions: recursively add files to the folder structure as mentioned above, so it's gonna create one and then keep modifying until the end of files. – Binoy Cherian Nov 30 '18 at 10:34
  • Why modify? Why not just zip all FileType folder? `ZipFile.CreateFromDirectory("FileId/FileType/", zipPath + @"\release.zip", CompressionLevel.Fastest, true);` ? – Renatas M. Nov 30 '18 at 10:37
  • @Reniuz, FileId is a protected info so cannot do this, I already thought of this actually. – Binoy Cherian Nov 30 '18 at 10:38
  • But it will create release.zip which contains folder FileType with all the files in it. There will be no /FileId folder in it – Renatas M. Nov 30 '18 at 11:03

0 Answers0