It took me awhile to figure all this out so figured I'd save you guys some work if you wanted to try to accomplish these same tasks. I'm pretty sure projects require framework 4.5.1 or greater in order to use these features but I could be wrong.
This Decompresses All .zip archives in the folder and then deletes the .zips archives afterwards.
Add Reference
Code:
System.IO.Compression.FileSystem
Imports
Code:
Imports System.IO
Imports System.IO.Compression
vb.net Code:
Try
Dim dir As New DirectoryInfo(TextBox3.Text & "\")
Dim FI As FileInfo() = dir.GetFiles()
Dim FID As FileInfo
For Each FID In FI
If FID.FullName.ToString.Contains(".zip") Then
ZipFile.ExtractToDirectory(FID.FullName, TextBox3.Text & "\")
'Deletes the archive after it's extracted it
System.IO.File.Delete(FID.FullName)
End If
Next FID
Catch ex As Exception
End Try
Just replace the textbox3's with whatever folder you want to use.
This Compresses A Folder To 1 of 3 Different Compression Levels
You will need the same imports and references as you needed for the extraction part at the top of this post.
You will need to add the following components to your forms design
- TextBox1 ' this will display the folder directory of the folder that will be added to a .zip archive.
- ComboBox1 ' this will display the 3 different compression choices
- Button1 ' will make/save and compress the folder listed in TextBox1 to a .Zip it will be saved to the desktop
- Button2 ' used for browse to pick the folder that will be compressed to the .Zip
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("No Compression")
ComboBox1.Items.Add("Fastest")
ComboBox1.Items.Add("Optimal")
ComboBox1.Text = "No Compression"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Zip Button
Dim Folder As String = TextBox1.Text
If ComboBox1.Text = "No Compression" Then
ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.NoCompression, True)
End If
If ComboBox1.Text = "Fastest" Then
ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.Fastest, True)
End If
If ComboBox1.Text = "Optimal" Then
ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.Optimal, True)
End If
MsgBox("The folder has been compressed and saved to this location " & My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Browse Button
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = Application.StartupPath
If DialogResult.OK = dialog.ShowDialog() Then
TextBox1.Text = dialog.SelectedPath
End If
End Sub