简体   繁体   English

如何覆盖现有的zip文件?

[英]How do I overwrite an existing zip file?

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. 我的印象是,当它生成一个与现有存档具有相同名称的.zip存档时,它会覆盖它。 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? 在某个地方是否存在我缺少的布尔覆盖参数,或者我是否需要编写一个检查来删除同名的预先存在的存档?

See the Remarks in the documentation for ZipFile.Open : 请参阅ZipFile.Open文档中的备注:

When you set the mode parameter to Create, the archive is opened with FileMode.CreateNew as the file mode value. 将mode参数设置为Create时,将使用FileMode.CreateNew作为文件模式值打开存档。 If the archive already exists, an IOException is thrown. 如果存档已存在,则抛出IOException。 When you set the mode parameter to Update, the archive is opened with FileMode.OpenOrCreate as the file mode value. 将mode参数设置为Update时,将使用FileMode.OpenOrCreate作为文件模式值打开存档。 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. 但是,在“更新”模式下创建zip存档的效率不如在“创建”模式下创建它。

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. 当您使用Create作为ZipArchiveMode时,API会隐式尝试创建文件而不处理其已存在的文件。 In your case, the Update ZipArchiveMode is probably more appropriate. 在您的情况下, Update ZipArchiveMode可能更合适。 The FileMode enumeration goes into more detail about what each value mentioned above allows and requires. FileMode枚举更详细地介绍了上面提到的每个值允许和要求的内容。

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. 话虽这么说,如果您要替换现有存档而不是更改其内容,您需要检查其存在删除它(如果存在),然后在Create模式下打开存档。

Simple -- delete the zip before you write to it. 简单 - 在写入之前删除zip。 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. 为安全起见,您还可以先创建新的,然后使用force命令将其移到旧的上面。

According to: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx 根据: 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. ZipArchive.Open将新文件添加到现有的zip存档中。 Always make sure to read the docs to make sure you are using the right method! 始终确保阅读文档以确保您使用正确的方法!

Nathan 弥敦道

As @Dan J was mentioning in his answer, all you have to do is to check for existence of the file and delete the same if it is not necessary and then create it. 正如@Dan J在他的回答中提到的那样,你所要做的就是检查文件是否存在,如果没有必要则删除它,然后创建它。 A working code snippet is given below. 下面给出了一个工作代码片段。

private void ZipTheFolder() {
    try {
        if (File.Exists(ZipPath)) {
            File.Delete(ZipPath);
        }
        ZipFile.CreateFromDirectory(FolderFromZipLocation, ZipPath);
    }
    catch(Exception ex) {
        throw new CustomConfigurationException($ "Error when zip: {ex.Message}");
    }
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM