System.IO.Compression and ZipFile - extract and overwrite
Asked Answered
P

2

24

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(..)
Periphrasis answered 17/3, 2013 at 18:47 Comment(0)
R
27

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 
Rhombic answered 18/3, 2013 at 0:53 Comment(5)
Seems to be working more, but it doesn't work now for stupid system files like "Thumbs.db" which make execution and overwriting fail. Any ideas?Periphrasis
Another issue: it seems not to copy file if the folder which must contain them doesn't exist.Periphrasis
i would suggest to use custom zip logic and strip all unwanted files like "Thumbs.db"; for the second one use next code If IO.Directory.Exists(path) = False Then IO.Directory.CreateDirectory(path)Rhombic
I used try for the line of entry.ExtractToFile (so if an error occurr the program doesn't stop). And used the code of directory to check existence and correct directory missing. Now it works as perfect. Please if you consider my question a good one rate up.Periphrasis
This solution works even with C# (just needed to rewrite it in C# syntax)Morass
F
12

I found the following implementation fully worked to solve the problems described above, ran without errors and successfully overwrote existing files and created directories as needed.

        ' 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 Using
Fina answered 1/5, 2015 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.