Feb 9, 2012

Vb.net code to compress and decompress any file


Vb.net code to compress any file

Imports System.IO.Compression
Imports System.IO

Module Module1

#Region "Global variable"
    Dim GzOut As GZipStream
    Dim Sr As StreamReader
    Dim Sw As StreamWriter
#End Region

    Public Sub Main()
        'Compression code
        Try
            'Create a compressed stream using a new file
            GzOut = New GZipStream(File.Create("C:\SSIS\data.zip"), CompressionMode.Compress)

            'Create a StreamWriter object to allow writing strings to the GZipStream
            Sr = New StreamReader("C:\SSIS\data.txt")
            Sw = New StreamWriter(GzOut)
            Sw.Write(Sr.ReadToEnd)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally

            'Close the stream objects
            Sw.Close()
            Sr.Close()
            GzOut.Close()

        End Try
    End Sub
End Module

Vb.net code to decompress a zip format file

Imports System.IO.Compression
Imports System.IO

Module Module1

#Region "Global variable"
    Dim Sr As StreamReader
    Dim GzIn As GZipStream
#End Region

    Public Sub Main()       

Try
            'Open the file containing the compressed data
            GzIn = New GZipStream(File.OpenRead("C:\Documents and Settings\riteshk\My Documents\data\data.zip"), CompressionMode.Decompress)
            'Read and display the compressed data
            Sr = New StreamReader(GzIn)
            Console.WriteLine(Sr.ReadToEnd())
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            'Close the stream objects
            Sr.Close()
            GzIn.Close()
        End Try

        Console.ReadKey()

    End Sub
End Module

No comments:

Post a Comment