Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.Compression and ZipFile - extract and overwrite

Tags:

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(..) 
like image 456
user1714647 Avatar asked Mar 17 '13 18:03

user1714647


People also ask

How do I create and extract a compressed ZIP file?

The following example shows how to create and extract a compressed .zip file by using the ZipFile class. The example compresses the contents of a folder into a new .zip file, and then extracts the zip to a new folder. To run the sample, create a start folder in your program folder and populate it with files to zip.

What is System Io compression namespace?

The System.IO.Compression namespace contains the following types for compressing and decompressing files and streams. You can also use these types to read and modify the contents of a compressed file. The following examples show some of the operations you can perform with compressed files.

Does your solution read or write to the ZIP file format?

Your solution does not read or write to the ZIP file format, but simply to the raw deflate compression, or GZip simple header-footer extension around the deflate compression.

What is the use of zipfile?

It compresses the contents of a folder into a zip archive and extracts that content to a new folder. To use the ZipFile class, you must reference the System.IO.Compression.FileSystem assembly in your project.


1 Answers

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  
like image 174
volody Avatar answered Sep 28 '22 07:09

volody