如何覆盖现有的 zip 文件?
How do I overwrite an existing zip file?
我在我的解决方案中使用了以下实用方法来压缩文件。我的印象是,每当它生成一个与现有文件同名的 .zip 存档时,它就会覆盖它。但这似乎不是这种情况,并且会抛出一个异常,说明该文件已经存在。
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);
}
}
是否有我遗漏的布尔覆盖参数,或者我是否需要编写一个检查来删除同名的预先存在的存档?
请参阅 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.
所以问题出在底层的文件处理上。当您使用 Create
作为 ZipArchiveMode 时,API 隐含地尝试创建一个文件而不处理它已经存在的文件。在您的情况下, Update
ZipArchiveMode 可能更合适。 FileMode enumeration 详细介绍了上述每个值允许和要求的内容。
话虽这么说,如果您希望 替换 现有存档而不是更改其内容,您需要 check for its existence and delete it 如果它存在,然后再打开存档在 Create
模式下。
简单 -- 在写入之前删除 zip。如果它以前不存在 -- 没有坏处。
为了安全起见,您也可以先创建新的,然后使用强制命令将其移动到旧的之上。
根据:https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx
ZipArchive.Open 将新文件添加到现有的 zip 存档中。请务必阅读文档以确保您使用的是正确的方法!
内森
正如@Dan J 在他的回答中提到的那样,您所要做的就是检查文件是否存在,如果不需要则删除它,然后创建它。下面给出了一个工作代码片段。
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}");
}
}
希望对您有所帮助。
我在我的解决方案中使用了以下实用方法来压缩文件。我的印象是,每当它生成一个与现有文件同名的 .zip 存档时,它就会覆盖它。但这似乎不是这种情况,并且会抛出一个异常,说明该文件已经存在。
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);
}
}
是否有我遗漏的布尔覆盖参数,或者我是否需要编写一个检查来删除同名的预先存在的存档?
请参阅 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.
所以问题出在底层的文件处理上。当您使用 Create
作为 ZipArchiveMode 时,API 隐含地尝试创建一个文件而不处理它已经存在的文件。在您的情况下, Update
ZipArchiveMode 可能更合适。 FileMode enumeration 详细介绍了上述每个值允许和要求的内容。
话虽这么说,如果您希望 替换 现有存档而不是更改其内容,您需要 check for its existence and delete it 如果它存在,然后再打开存档在 Create
模式下。
简单 -- 在写入之前删除 zip。如果它以前不存在 -- 没有坏处。
为了安全起见,您也可以先创建新的,然后使用强制命令将其移动到旧的之上。
根据:https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx
ZipArchive.Open 将新文件添加到现有的 zip 存档中。请务必阅读文档以确保您使用的是正确的方法!
内森
正如@Dan J 在他的回答中提到的那样,您所要做的就是检查文件是否存在,如果不需要则删除它,然后创建它。下面给出了一个工作代码片段。
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}");
}
}
希望对您有所帮助。