File/Folder Compression
Creating zip archive from directory
Section titled “Creating zip archive from directory”System.IO.Compression.ZipFile.CreateFromDirectory("myfolder", "archive.zip")Create archive.zip file containing files which are in myfolder. In example paths are relative to program working directory. You can specify absolute paths.
Extracting zip archive to directory
Section titled “Extracting zip archive to directory”System.IO.Compression.ZipFile.ExtractToDirectory("archive.zip", "myfolder")Extracts archive.zip to myfolder directory. In example paths are relative to program working directory. You can specify absolute paths.
Create zip archive dynamicaly
Section titled “Create zip archive dynamicaly”' Create filestream to fileUsing fileStream = New IO.FileStream("archive.zip", IO.FileMode.Create) ' open zip archive from stream Using archive = New System.IO.Compression.ZipArchive(fileStream, IO.Compression.ZipArchiveMode.Create) ' create file_in_archive.txt in archive Dim zipfile = archive.CreateEntry("file_in_archive.txt")
' write Hello world to file_in_archive.txt in archive Using sw As New IO.StreamWriter(zipfile.Open()) sw.WriteLine("Hello world") End Using
End UsingEnd UsingAdding File Compression to your project
Section titled “Adding File Compression to your project”- In Solution Explorer go to your project, right click on References then Add reference…
- Search for Compression and select System.IO.Compression.FileSystem then press OK.
- Add
Imports System.IO.Compressionto the top of your code file (before any class or module, with the otherImportsstatements).
Option Explicit OnOption Strict On
Imports System.IO.Compression
Public Class Foo
...
End ClassPlese note that this class (ZipArchive) is only available from .NET verison 4.5 onwards