我使用标准的VB.NET库来提取和压缩文件。它工作得很好,但当我必须提取文件并且文件已经存在时,问题就来了。
我使用的代码
导入:
Imports System.IO.Compression在崩溃时调用的方法
ZipFile.ExtractToDirectory(archivedir, BaseDir)还设置了archivedir和BaseDir,实际上,如果没有要覆盖的文件,它就可以工作。问题恰好出现在这样的时候。
如何在不使用thirdy-part库的情况下覆盖提取中的文件?
(注意,我使用System.IO.Compression和System.IO.Compression.Filesystem作为参考)
因为这些文件放在多个文件夹中,并且已经存在文件,所以我会避免手动操作
IO.File.Delete(..)发布于 2013-03-18 08:53:38
使用ExtractToFile并将overwrite设置为true可覆盖与目标文件同名的现有文件
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
For Each entry As ZipArchiveEntry In archive.Entries
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), True)
Next
End Using 发布于 2015-05-02 05:57:41
我发现下面的实现完全解决了上面描述的问题,运行时没有错误,并成功地覆盖了现有文件并根据需要创建了目录。
' Extract the files - v2
Using archive As ZipArchive = ZipFile.OpenRead(fullPath)
For Each entry As ZipArchiveEntry In archive.Entries
Dim entryFullname = Path.Combine(ExtractToPath, entry.FullName)
Dim entryPath = Path.GetDirectoryName(entryFullName)
If (Not (Directory.Exists(entryPath))) Then
Directory.CreateDirectory(entryPath)
End If
Dim entryFn = Path.GetFileName(entryFullname)
If (Not String.IsNullOrEmpty(entryFn)) Then
entry.ExtractToFile(entryFullname, True)
End If
Next
End Usinghttps://stackoverflow.com/questions/15464740
复制相似问题