0

I inherited some code that makes use of ZipArchive to save some information from the database. It uses BinaryFormatter to do this. When you look at the zip file with 7-zip (for example), you see a couple of folders and a .txt file. All is working well. I simply want to modify the code to also have a folder in the ZipArchive called "temp" that consists of files and folders under C:\temp. Is there an easy way to add a entry (ZipArchiveEntry?) that consist of an entire folder or the disc? I saw "CreateEntryFromFile" in the member methods of ZipArchive, but no CreateEntryFromDirectory. Or perhaps there's some other simple way to do it? Anyone have example code? I should say that C:\temp could have variable number of files and directories (that have child directories and files, etc.) Must I enumerate them somehow, create my own directories use CreateEntryFromFile? Any help is appreciated.

Similarly, when I read the ZipArchive, I want to take the stuff related to C:\temp and just dump it in a directory (like C:\temp_old) Thanks, Dave

Dave
  • 8,095
  • 14
  • 56
  • 99

1 Answers1

0

The answer by user1469065 in Zip folder in C# worked for me. user1469065 shows how to get all the files/directories in the directory (using some cool "yield" statements) and then do the serialization. For completeness, I did add the code to deserialize as user1469065 suggested (at least I think I did it the way he suggested).

private static void ReadTempFileStuff(ZipArchive archive) // adw
    {
        var sessionArchives = archive.Entries.Where(x => x.FullName.StartsWith(@"temp_directory_contents")).ToArray();
        if (sessionArchives != null && sessionArchives.Length > 0)
        {
            foreach (ZipArchiveEntry entry in sessionArchives)
            {
                FileInfo info = new FileInfo(@"C:\" + entry.FullName);
                if (!info.Directory.Exists)
                {
                    Directory.CreateDirectory(info.DirectoryName);
                }
                entry.ExtractToFile(@"C:\" + entry.FullName,true);
            }
        }
    }
Community
  • 1
  • 1
Dave
  • 8,095
  • 14
  • 56
  • 99