Using .NET ZipArchive to zip entire directories, plus more information

I inherited some code that ZipArchive uses to store some information from a database. To do this, use BinaryFormatter. When you look at a zip file with 7-zip (for example), you see a couple of folders and a .txt file. Everything works well. I just want to change the code to also have a folder in ZipArchive called "temp", which consists of files and folders under C: \ temp. Is there an easy way to add an entry (ZipArchiveEntry?) Consisting of an entire folder or disk? I saw "CreateEntryFromFile" in the methods of the ZipArchive members, but not CreateEntryFromDirectory. Or maybe there is another easy way to do this? Does anyone have some sample code? I have to say that C: \ temp can have a variable number of files and directories (having child directories and files, etc.). Do I have to list them somehow, create my own directories using CreateEntryFromFile? Any help is appreciated.

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

0
c # ziparchive
Apr 26 '16 at 17:50
source share
1 answer

Answer of user 1469065 in Mail folder in C # worked for me. user1469065 shows how to get all the files / directories in a directory (using some interesting “output” operators), and then serialize. For completeness, I added code for deserialization as user1469065 (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); } } } 
0
Apr 27 '16 at 15:14
source share



All Articles