Imports System.IO
Imports System.Text
Imports System.IO.Compression

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub


    Private Function GetRandomString(ByVal size As Integer) As String

        Dim sb As New StringBuilder()
        Dim Rand As New Random

        For i As Integer = 0 To size - 1
            Dim ch As Char = Convert.ToChar(Rand.Next(33, 126))
            sb.Append(ch)
        Next
        Return sb.ToString()
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' First, create a file that doesn't use any compression.
        Dim fsNoCompress As New FileStream("c:\nocompress", FileMode.Create)
        Dim rNoCompress As New BinaryWriter(fsNoCompress)

        ' Now create a file that will use compression.
        Dim fsCompress As New FileStream("c:\compress", FileMode.Create)

        ' You need to create a GZipStream in Compress mode. It wraps the FileStream.
        Dim CompressStream As New GZipStream(fsCompress, CompressionMode.Compress)

        ' The BinaryWriter writes to the GZipStream, not the FileStream.
        Dim rCompress As New BinaryWriter(CompressStream)

        ' Write 1,000 random strings (each 30 characters long) to both files.
        For i As Integer = 1 To 10000
            Dim RandomString As String = GetRandomString(30)
            rNoCompress.Write(RandomString)
            rCompress.Write(RandomString)
        Next

        ' Close both files.
        rNoCompress.Close()
        fsNoCompress.Close()

        rCompress.Close()
        fsCompress.Close()

        ' Compare the file sizes.
        MessageBox.Show("Compressed data from " & _
          My.Computer.FileSystem.GetFileInfo("c:\nocompress").Length & _
          " bytes to " & _
          My.Computer.FileSystem.GetFileInfo("c:\compress").Length & _
          " bytes.")

        ' Read the decompressed data.
        Dim fsRead As New FileStream("c:\compress", FileMode.Open)
        Dim DecompressStream As New GZipStream(fsRead, CompressionMode.Decompress)
        Dim r As New BinaryReader(DecompressStream)
        ' Read data here.
        ' Show the first string.
        MessageBox.Show("The compressed data looks like this: " & r.ReadString())
        r.Close()
        fsRead.Close()

    End Sub
End Class
