Results 1 to 1 of 1

Thread: Extraction all .Zip Archives in a Folder & Compressing .Zips

  1. #1

    Thread Starter
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Cool Extraction all .Zip Archives in a Folder & Compressing .Zips

    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:
    1. Try
    2.             Dim dir As New DirectoryInfo(TextBox3.Text & "\")
    3.             Dim FI As FileInfo() = dir.GetFiles()
    4.             Dim FID As FileInfo
    5.             For Each FID In FI
    6.                 If FID.FullName.ToString.Contains(".zip") Then
    7.                     ZipFile.ExtractToDirectory(FID.FullName, TextBox3.Text & "\")
    8.                     'Deletes the archive after it's extracted it
    9.                     System.IO.File.Delete(FID.FullName)
    10.                 End If
    11.             Next FID
    12.         Catch ex As Exception
    13.         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:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         ComboBox1.Items.Add("No Compression")
    3.         ComboBox1.Items.Add("Fastest")
    4.         ComboBox1.Items.Add("Optimal")
    5.         ComboBox1.Text = "No Compression"
    6.     End Sub
    7.  
    8.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    9.         'Zip Button
    10.         Dim Folder As String = TextBox1.Text
    11.         If ComboBox1.Text = "No Compression" Then
    12.             ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.NoCompression, True)
    13.         End If
    14.         If ComboBox1.Text = "Fastest" Then
    15.             ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.Fastest, True)
    16.         End If
    17.         If ComboBox1.Text = "Optimal" Then
    18.             ZipFile.CreateFromDirectory(Folder, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip", CompressionLevel.Optimal, True)
    19.         End If
    20.         MsgBox("The folder has been compressed and saved to this location " & My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & Path.GetFileName(TextBox1.Text) & ".zip")
    21.     End Sub
    22.  
    23.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    24.         'Browse Button
    25.         Dim dialog = New FolderBrowserDialog()
    26.         dialog.SelectedPath = Application.StartupPath
    27.         If DialogResult.OK = dialog.ShowDialog() Then
    28.             TextBox1.Text = dialog.SelectedPath
    29.         End If
    30.     End Sub
    Last edited by Vexslasher; Apr 9th, 2016 at 11:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width