top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to compress a file using VB.NET?

+3 votes
5,198 views

Can someone suggest me that how can I write the code for file compressing in VB.NET?

posted Nov 26, 2013 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The .NET Framework provides compression in System.IO.Compression. The GZipStream is used to compress data. We use VB.NET code to compress a string to a file.

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

Module Module1
    Sub Main()
        ' Byte array from string.
        Dim array() As Byte = Encoding.ASCII.GetBytes(New String("X"c, 10000))

        ' Call Compress.
        Dim c() As Byte = Compress(array)

        ' Write bytes.
        File.WriteAllBytes("C:\\compress.gz", c)
    End Sub


    Function Compress(ByVal raw() As Byte) As Byte()
        ' Clean up memory with Using-statements.
        Using memory As MemoryStream = New MemoryStream()
            ' Create compression stream.
            Using gzip As GZipStream = New GZipStream(memory, CompressionMode.Compress, True)
                ' Write.
                gzip.Write(raw, 0, raw.Length)
            End Using
            ' Return array.
            Return memory.ToArray()
        End Using
    End Function
End Module
answer Jan 23, 2017 by Kavyashree
Similar Questions
0 votes

I want compress the data stored in column more efficient, because mydata is timeseries of stock or future price.
Most of them is similar, so I thought I could compress the price data(float, uint64, int) more efficient than zlib,like what infobright do.

Could any one tell me how to compress the column more efficient or could I get the blocks data type when I write the compress plug.

+4 votes

Can someone provide me some details that how this conversion is possible by which it compresses the IP header (Minimum of 20 bytes) in to Token that is 1 to 4 bytes ?

+7 votes

I want to create a CAPTCHA security image for my web application.
Can anyone provide with the code for the same.

+4 votes

How to Automate Web Browser using VB.NET?

...