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(..)
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With