I have made the following utility method to compress files in my solution. I was under the impression that when ever it produced a .zip archive that has the same name as an existing one it would overwrite it. This does not seem to be case though and an exception is thrown stating that the file already exists.
public static void CompressFile(string zipName, string filePath, string fileName)
{
try
{
using (ZipArchive archive = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(filePath, fileName, CompressionLevel.Fastest);
}
}
catch(Exception e)
{
_log.Error("Exception Caught: {0}", e.Message);
}
}
Is there a boolean overwrite param somewhere that I'm missing or do I need to write a check that will delete a pre-existing archive of the same name?
A group of selected files (left) can be replaced with Zip files (right). TIP: If you want to keep unzipped versions of the files, make a copy before you use the Replace Files with Zipped Files command. WinZip lets you replace a group of files with Zip files.
Open the archive in 7-Zip. Locate the file to be edited. Right-click on the file to edit and select "Edit" (alternative shortcut = F4). Make your changes, save them and close the editor window - 7-Zip will only detect the file has changed when the editor has been closed.
Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location. To rename it, press and hold (or right-click) the folder, select Rename, and then type the new name.
See the Remarks in the documentation for ZipFile.Open:
When you set the mode parameter to Create, the archive is opened with FileMode.CreateNew as the file mode value. If the archive already exists, an IOException is thrown. When you set the mode parameter to Update, the archive is opened with FileMode.OpenOrCreate as the file mode value. If the archive exists, it is opened. The existing entries can be modified and new entries can be created. If the archive does not exist, a new archive is created; however, creating a zip archive in Update mode is not as efficient as creating it in Create mode.
So the problem is down to the underlying File handling. When you use Create
as the ZipArchiveMode, the API is implicitly attempting to create a file without handling its already existing. In your case, the Update
ZipArchiveMode is probably more appropriate. The FileMode enumeration goes into more detail about what each value mentioned above allows and requires.
That being said, if you are looking to replace an existing archive rather than alter its contents, you will need to check for its existence and delete it if it exists before opening the archive in Create
mode.
Simple -- delete the zip before you write to it. If it didn't exist before -- no harm.
For safety's sake you could also first create the new one, then move it over the old one with a force command.
According to: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx
ZipArchive.Open adds new files to an existing zip archive. Always make sure to read the docs to make sure you are using the right method!
Nathan
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