DeflateStream Class is for compressing and decompressing streams using the Deflate algorithm. : DeflateStream « File Directory « VB.Net






DeflateStream Class is for compressing and decompressing streams using the Deflate algorithm.

 

Imports System.IO
Imports System.IO.Compression
Module Module1
    Sub Main()
        Dim dirpath As String = "c:\"
        Dim di As DirectoryInfo = New DirectoryInfo(dirpath)
        For Each fi As FileInfo In di.GetFiles()
            Compress(fi)
        Next
        For Each fi As FileInfo In di.GetFiles("*.cmp")
            Decompress(fi)
        Next
    End Sub
    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 <> ".cmp" Then
                Using outFile As FileStream = File.Create(fi.FullName + ".cmp")
                    Using Compress As DeflateStream = New DeflateStream(outFile, 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
    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 DeflateStream = New DeflateStream(inFile,CompressionMode.Decompress)
                Decompress.CopyTo(outFile)
                    Console.WriteLine("Decompressed: {0}", fi.Name)
                End Using
            End Using
        End Using
    End Sub
End Module

   
  








Related examples in the same category

1.DeflateStream Class