I am using the following code to extract all files in a folder
using (ZipArchive archive = new ZipArchive(zipStream)) { archive.ExtractToDirectory(location); }
But if one file exist then it throws an exception. Is there is any way to tell the Compression API to replace the existing files.
I found one way is to get all the file names first then check whether file exist and delete it. But this is somehow very costly for me.
What It Means. Extract all files. Extracts all selected files, including older versions of files that are already in the destination folder. If the Overwrite setting is Prompt user, Smartcrypt asks before overwriting newer files already in the folder. Freshen existing files only.
I have created an extension. any comment to it improve will be appreciated,
public static class ZipArchiveExtensions { public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite) { if (!overwrite) { archive.ExtractToDirectory(destinationDirectoryName); return; } DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName); string destinationDirectoryFullPath = di.FullName; foreach (ZipArchiveEntry file in archive.Entries) { string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName)); if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase)) { throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability"); } if (file.Name == "") {// Assuming Empty for Directory Directory.CreateDirectory(Path.GetDirectoryName(completeFileName)); continue; } file.ExtractToFile(completeFileName, true); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With