2

我遇到了麻烦,我得到了这个代码:

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();

        }
    }
}

一切都很好,但是当我尝试在 zip 中添加一个名称相同的文件时,它不会覆盖它,我想要它......关于我如何做到这一点的任何想法?谢谢。

4

2 回答 2

5

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.

于 2017-02-23T12:14:54.540 回答
2

行前

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

您必须测试该名称是否已存在于条目中。如果是,请将其取出,然后重新插入。

于 2013-02-12T15:03:44.217 回答