Search code examples
c#zip

Unzip and overwrite if file exists


What I want to do is unzip file into an existing directory and overwrite older file if it exists.

I am using this command to unzip file:

ZipFile.ExtractToDirectory(path + file_name_zip, path);

Solution

  • Use ZipFileExtensions.ExtractToFile

    Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.

    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                        {
                            entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true);
                        }
                    }