Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overwrite an existing zip file?

Tags:

c#

.net

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?

like image 776
Stavros_S Avatar asked Jan 27 '15 21:01

Stavros_S


People also ask

Can you replace a file in a zipped folder?

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.

Can you modify a file in a zip?

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.

How do I change a Compressed ZIP file?

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.


2 Answers

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.

like image 168
Dan J Avatar answered Oct 03 '22 22:10

Dan J


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

like image 43
Nathan Bellowe Avatar answered Oct 03 '22 21:10

Nathan Bellowe