Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite entry in zip file

I'm having trouble, I got this code:

DirectoryInfo di = new DirectoryInfo(dir);
FileInfo[] rgFiles = di.GetFiles();
DirectoryInfo[] d = di.GetDirectories();
if(rgFiles != null && d != null) {
foreach (FileInfo fi in rgFiles)
{
    foreach (DirectoryInfo dii in d)
    {
        using (ZipFile zip = ZipFile.Read(locateZipFile()))
        {

            zip.AddFile(fi.FullName, "");

            zip.AddDirectory(dii.FullName,dii.Name);
            toolStripStatusLabel1.Text = "Inserting " + fi.Name;
            toolStripStatusLabel1.Text = "Inserting " + dii.Name + " and all of it's contents";

            MessageBox.Show("Inserted the file " + fi.Name);
            MessageBox.Show("Inserted the folder " + dii.Name + " and all contents in it.");
            zip.Save();

        }
    }
}

Everything works great, but when I'm trying to add a file that is named the same in the zip, it does not overwrite it, which i want it to.. Any ideas on how i can do that? thanks.

like image 846
user1568364 Avatar asked Jul 18 '26 14:07

user1568364


1 Answers

You can use the UpdateFile method.

zip.UpdateFile(fi.FullName, "");

This method adds a file to a zip archive, or, if the file already exists in the zip archive, this method Updates the content of that given filename in the zip archive. The UpdateFile method might more accurately be called "AddOrUpdateFile".

Upon success, there is no way for the application to learn whether the file was added versus updated.

like image 144
Deantwo Avatar answered Jul 20 '26 03:07

Deantwo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!