Compressing and decompressing files with GZIPStream or Deflatestream

These samples whow how to compress and decompress files with the build in framework methods. For real world applications consider using something like .netzip from codeplex, since the compression quality from the build in methods is limited. Also, the build in methods will allow you to just compress and decompress a single file.

Compressing a file

Imports System.IO
Imports System.IO.Compression
  
Module packtool
  
    ''' <summary>
    ''' Example on using a deflatestream to compress a file into a target file
    ''' </summary>
    ''' <param name="args">args() will automagicaly hold the command line args
    ''' passed to the application</param>
    ''' <remarks>Error checking on the source and target files is left out to
    ''' keep the example to it's core. In a live deployment you should always
    ''' check to see if the source file is present and accessible and the target
    ''' file writable</remarks>
    Sub Main(ByVal args() As String)
  
        If args.Count <> 2 Then
            Console.WriteLine("format: pack <source> <target>")
            Return
        End If
  
        Dim sSource As String = args(0)
        Dim sTarget As String = args(1)
        Console.WriteLine("packing {0} to {1}", sSource, sTarget)
        Dim oSourcefile As FileStream = File.OpenRead(sSource)
        Dim oDestfile As FileStream = File.Create(sTarget)
        Using oCompressed As New System.IO.Compression.DeflateStream(oDestfile, CompressionMode.Compress)
            Dim dBuffer(8192) As Byte  ' --- 8 KB data buffer
            Dim nProcessed As Int64 = 0
            Dim nRead As Integer = 0
            Do
                nRead = oSourcefile.Read(dBuffer, 0, 8192)
                nProcessed += nRead
                oCompressed.Write(dBuffer, 0, nRead)
                Console.Write("{0} Kb processed{1}", Int(nProcessed / 1024), vbCr)
            Loop Until nRead = 0
            Console.WriteLine()
        End Using
        oDestfile.Close()
        oSourcefile.Close()
  
    End Sub
  
End Module

Decompressing a file

Imports System.IO
Imports System.IO.Compression
  
Module unpacktool
  
    ''' <summary>
    ''' Example on uncompressing a compressed file with the standard .net methods
    ''' </summary>
    ''' <param name="args">Command line arguments passed by the user</param>
    ''' <remarks>Error checking left out on the source and target file</remarks>
    Sub Main(ByVal args() As String)
  
        If args.Count <> 2 Then
            Console.WriteLine("format: unpack <source> <target>")
            Return
        End If
  
        Dim sSource As String = args(0)
        Dim sTarget As String = args(1)
        Console.Write("packing {0} to {1}", sSource, sTarget)
        Dim oSourcefile As FileStream = File.OpenRead(sSource)
        Dim oDestfile As FileStream = File.Create(sTarget)
        Using oCompressed As New System.IO.Compression.DeflateStream(oSourcefile, CompressionMode.Decompress)
            ' --- We could use a larger buffer here like in the compression example, but there
            '     is no real performance gain to be made since the framework handles the buffering
            '     quite efficiently.
            Dim dByte As Integer = oCompressed.ReadByte
            While dByte <> -1
                oDestfile.WriteByte(CType(dByte, Byte))
                dByte = oCompressed.ReadByte()
            End While
        End Using
        oDestfile.Close()
        oSourcefile.Close()
  
    End Sub
  
End Modul