GZipStream Class : Stream « File Directory « VB.Net






GZipStream Class

 

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

Public Class GZipTest
   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 Main(ByVal args() As String)
       Dim infile As FileStream
       infile = New FileStream("c:\\", FileMode.Open, FileAccess.Read, FileShare.Read)
       Dim buffer(infile.Length - 1) As Byte
       Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
       infile.Close()
       Dim ms As New MemoryStream()
       Dim compressedzipStream As New GZipStream(ms, CompressionMode.Compress, True)
       compressedzipStream.Write(buffer, 0, buffer.Length)
       compressedzipStream.Close()
       ms.Position = 0
       Dim zipStream As New GZipStream(ms, CompressionMode.Decompress)
       Dim decompressedBuffer(buffer.Length + 100) As Byte
       Dim totalCount As Integer = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
       zipStream.Close()
    End Sub
End Class 

   
  








Related examples in the same category

1.Stream.CanRead Property tells whether the current stream supports reading.
2.Stream.CanWrite Property tells whether the current stream supports writing.
3.Stream.CopyTo Method reads all the bytes from the current stream and writes them to the destination stream.
4.Reads a maximum of count characters from the current stream into buffer
5.Stream.Read Method reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
6.Open a stream as ASCII
7.Compress streams
8.Decompress streams.
9.Stream.CanWrite Property tells whether the current stream supports writing.