本文介绍了System.IO.Compression和ZipFile-提取并覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准的VB.NET库提取和压缩文件。它也可以正常工作,但是问题出在我必须提取文件并且文件已经存在的时候。

I'm using the standard VB.NET libraries for extracting and compressing files. It works as well but the problem comes when I have to extract and files exist already.

我使用的代码

导入:

Imports System.IO.Compression

崩溃时我调用的方法

ZipFile.ExtractToDirectory(archivedir, BaseDir)

archivedir和BaseDir也已设置,实际上,如果没有要覆盖的文件,它就可以工作。问题恰好在出现时出现。

archivedir and BaseDir are set as well, in fact it works if there are no files to overwrite. The problem comes exactly when there are.

如何在不使用第三方库的情况下覆盖提取文件?

How can I overwrite files in extraction without use thirdy-part libraries?

(请注意,我正在使用System.IO.Compression和System.IO.Compression.Filesystem作为参考)

(Note I'm using as Reference System.IO.Compression and System.IO.Compression.Filesystem)

由于文件位于多个已经存在文件的文件夹中我会避免手动

Since the files go in multiple folders having already existent files I'd avoid manual

IO.File.Delete(..)


推荐答案

使用,如果覆盖为true,则将覆盖与目标文件同名的现有文件

Use ExtractToFile with overwrite as true to overwrite an existing file that has the same name as the destination file

    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

这篇关于System.IO.Compression和ZipFile-提取并覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:27