use*_*364 3 c# overwrite dotnetzip
我遇到了麻烦,我得到了这个代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,但是当我尝试添加一个在 zip 中命名相同的文件时,它不会覆盖它,这是我想要的.. 关于如何做到这一点有什么想法吗?谢谢。
您可以使用UpdateFile方法。
zip.UpdateFile(fi.FullName, "");
Run Code Online (Sandbox Code Playgroud)
此方法将文件添加到 zip 存档中,或者,如果该文件已存在于 zip 存档中,则此方法更新 zip 存档中给定文件名的内容。UpdateFile 方法可能更准确地称为“AddOrUpdateFile”。
成功后,应用程序无法了解文件是否已添加或更新。