DeflateStream Class : DeflateStream « File Directory « VB.Net






DeflateStream Class

 

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

Public Class DeflateTest
   Public Shared Function ReadAllBytesFromStream(stream As Stream, buffer() As Byte) As Integer
      Dim offset As Integer = 0
      Dim totalCount As Integer = 0
      While True
         Dim bytesRead As Integer = stream.Read(buffer, offset, 100)
         If bytesRead = 0 Then
            Exit While
         End If
         offset += bytesRead
         totalCount += bytesRead
      End While
      Return totalCount
   End Function
   Public Shared Sub DeflateCompressDecompress(filename As String)
        Dim infile As FileStream
        Try
  
         infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim buffer(infile.Length - 1) As Byte
            Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
         If count <> buffer.Length Then
                infile.Close()
                Return
         End If
         infile.Close()
         Dim ms As New MemoryStream()
         Dim compressedzipStream As New DeflateStream(ms, CompressionMode.Compress, True)
         compressedzipStream.Write(buffer, 0, buffer.Length)
            compressedzipStream.Close()
         ms.Position = 0
         Dim zipStream As New DeflateStream(ms, CompressionMode.Decompress)
         Dim decompressedBuffer(buffer.Length + 100) As Byte
         Dim totalCount As Integer = DeflateTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
         zipStream.Close()
        Catch e As Exception
        End Try
   End Sub 
   
    Public Shared Sub Main(ByVal args() As String)
        DeflateCompressDecompress("c:\\a.txt")
    End Sub 'Main
End Class 'DeflateTest 

   
  








Related examples in the same category

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