h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Compress and Decompress in VB.Net

Thanks to MSDN

Namespace

Imports System.IO.Compression

Declare Variable
'Use GZipStream Class for Compress and Decompress Folder
    Dim dirpath As String = "E:\TestCompress"

Private Sub Compress(ByVal fi As FileInfo)

        Using inFile As FileStream = fi.OpenRead()
            If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) _
                <> FileAttributes.Hidden And fi.Extension <> ".gz" Then
                Using outFile As FileStream = File.Create(fi.FullName + ".gz")
                    Using Compress As GZipStream = New GZipStream(outFile, CompressionMode.Compress)
                        ' Copy the source file into the compression stream.
                        Dim buffer As Byte() = New Byte(4096) {}
                        Dim numRead As Integer
                        numRead = inFile.Read(buffer, 0, buffer.Length)
                        Do While numRead <> 0
                            Compress.Write(buffer, 0, numRead)
                            numRead = inFile.Read(buffer, 0, buffer.Length)
                        Loop
                        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
    Private Sub Decompress(ByVal fi As FileInfo)
        Using inFile As FileStream = fi.OpenRead()
            Dim curFile As String = fi.FullName
            Dim origName = curFile.Remove(curFile.Length - fi.Extension.Length)
            Using outFile As FileStream = File.Create(origName)
                Using Decompress As GZipStream = New GZipStream(inFile, CompressionMode.Decompress)
                    Dim buffer As Byte() = New Byte(4096) {}
                    Dim numRead As Integer
                    numRead = Decompress.Read(buffer, 0, buffer.Length)
                    Do While numRead <> 0
                        outFile.Write(buffer, 0, numRead)
                        numRead = Decompress.Read(buffer, 0, buffer.Length)
                    Loop
                    Console.WriteLine("Decompressed: {0}", fi.Name)
                End Using
            End Using
        End Using
    End Sub

    Private Sub CompressDeCompress(ByVal path As String, ByVal mode As CompressType)
        Dim di As DirectoryInfo = New DirectoryInfo(path)
        Select Case mode
            Case CompressType.Compress
                For Each fi As FileInfo In di.GetFiles()
                    Compress(fi)
                Next
            Case CompressType.Decompress
                For Each fi As FileInfo In di.GetFiles("*.gz")
                    Decompress(fi)
                Next
        End Select
    End Sub

    Enum CompressType
        Compress
        Decompress
    End Enum

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CompressDeCompress(dirpath, CompressType.Compress)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        CompressDeCompress(dirpath, CompressType.Decompress)
    End Sub

No comments:

Post a Comment