Hello! There is a class that extracts files from the archive with overwriting:

public static class ZipArchiveExtension { public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName) { foreach (ZipArchiveEntry file in archive.Entries) { string completeFileName = Path.Combine(destinationDirectoryName, file.FullName); if (file.Name == "") { Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); continue; } file.ExtractToFile(completeFileName, true); } } } 

Here I call him:

 ZipPath = textBox4.Text + textBox5.Text + ".zip"; ExtractPath = textBox4.Text; ZipToOpen = new FileStream(ZipPath, FileMode.Open); Archive = new ZipArchive(ZipToOpen, ZipArchiveMode.Update, true, Encoding.GetEncoding("cp866")); ZipArchiveExtension.ExtractToDirectory(Archive, ExtractPath); 

I need something to call the method, I also specified a string array with the names of the files that should not be overwritten. Suppose I add a string array to the incoming parameters of the method and I start a string array with the names of files that should not be overwritten.

 public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, string[] Massive) string[] Massive = {"file1.txt", "file2.txt"}; 

What to do next? To loop through an array with file.Name in foreach using if, executing code if the names do not match?

    1 answer 1

    To loop through an array with file.Name in foreach using if, executing code if the names do not match?

    This is an option. Alternatively, you can use Linq instead of a loop:

     foreach (ZipArchiveEntry file in archive.Entries) { //ΠΏΡ€ΠΎΠΏΡƒΡ‰Π΅Π½ΠΎ Ρ‚Π΅Π»ΠΎ Ρ†ΠΈΠΊΠ»Π° //ignoredFilenames - массив ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠ΅ΠΌΡ‹Ρ… Ρ„Π°ΠΉΠ»ΠΎΠ² bool overwrite = !ignoredFilenames.Contains(file.Name); file.ExtractToFile(completeFileName, overwrite); 

    If the array is not used anywhere else, it is logical to make Set from it:

     var ignoredFilenamesSet = new HashSet<String>(Arrays.asList(ignoredFilenames)); //Π·Π°Ρ‚Π΅ΠΌ Π² Ρ†ΠΈΠΊΠ»Π΅ bool overwrite = !ignoredFilenamesSet.Contains(file.Name); 

    can also take iset as an argument.

    If the ignored file does not need to be extracted at all, you can skip the iteration for the file:

     foreach (ZipArchiveEntry file in archive.Entries) { if(ignoredFilenamesSet.Contains(file.Name) continue; //... Ссли Π½Π΅ игнорируСтся, ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ