System.IO.Compression and ZipFile - extract and overwrite

user1714647 picture user1714647 · Mar 17, 2013 · Viewed 38.3k times · Source

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.

Code I use

Imports:

Imports System.IO.Compression

Method I call when it crashes

ZipFile.ExtractToDirectory(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?

(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(..)

Answer

volody picture volody · Mar 18, 2013

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