How to Zip a File in VB.Net

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Zip a File in VB.Net
Learn how to compress a file using Visual Basic.NET. (binary digits - computer science image by drx from Fotolia.com)

It can be useful to know how to compress files using Visual Basic.NET (VB.NET) if you often have to compress files manually. A Zip file is a format for data compression. Files are commonly compressed to save hard drive space or before being sent through e-mail. VB.NET is an object-oriented programming language engineered by Microsoft that's fairly easy to use and learn. In a few steps you can write VB.NET code to compress a file.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Start Microsoft Visual Basic Express and click "New Project..." on the left pane of your screen. Double-click "Windows Forms Application."

  2. 2

    Double-click "Button" on the "Tools" pane to create a new button. Double-click "Button1" to open the "Form1.vb" module.

    Press "Ctrl+A" and press "Delete" to delete all code.

  3. 3

    Copy and paste the following code into your "Form1.vb" module.

    Imports System.IO

    Imports System.IO.Compression

    Public Class Form 1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim dirpath As String = "C:\Bank"
    
        Dim di As DirectoryInfo = New DirectoryInfo(dirpath)
    
    
    
        For Each fi As FileInfo In di.GetFiles()
    
            Compress(fi)
    
        Next
    
    End Sub
    
    Private Sub Compress(ByVal fi As FileInfo)
    
        Using in File As FileStream = fi.OpenRead()
    
            If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) _
    
                <> FileAttributes.Hidden And fi.Extension <> ".gz" Then
    
                Using out File As FileStream = File.Create(fi.FullName + ".gz")
    
                    Using Compress As GZipStream = _
    
                     New GZipStream(out File, CompressionMode.Compress)
    
                        inFile.CopyTo(Compress)
    
                        Console.WriteLine("Compressed {0} from {1} to {2} bytes.", _
    
                                          fi.Name, fi.Length.ToString(), _
    
                                          outFile.Length.ToString())
    
                    End Using
    
                End Using
    
            End If
    
        End Using
    
    End Sub
    

    End Class

  4. 4

    Edit the following line of code and type the path of the files you want to compress.

    Dim dirpath As String = "C:\Bank"

    Press "F5" to run your program then click "Button1."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.