Link to home
Start Free TrialLog in
Avatar of milani_lucie
milani_lucieFlag for United States of America

asked on

How to compress a string using GZIP in VB.NET ?

Hi,

I do have the following string : "<Child><Name>Ram</Name></Child>". I want to compress the string using GZIP in VB.NET.  Can you please provide me the code ?

Thanks
Avatar of kaufmed
kaufmed
Flag of United States of America image

Following is an example writing the string out to a FileStream. You can, or course, change the type of stream you want to output to (e.g. MemoryStream).
Dim bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("<Child><Name>Ram</Name></Child>")
Dim fStream As New System.IO.FileStream("C:\out.cmp", IO.FileMode.Create)
Dim zStream As New System.IO.Compression.GZipStream(fStream, IO.Compression.CompressionMode.Compress, True)
 
 
zStream.Write(bytes, 0, bytes.Length)
zStream.Close()
fStream.Close()

Open in new window

Avatar of milani_lucie

ASKER

Dim bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("<Child><Name>Ram</Name></Child>")
Dim memoryStream As New System.IO.MemoryStream()
Dim zStream As New System.IO.Compression.GZipStream(memoryStream, IO.Compression.CompressionMode.Compress, True)

zStream.Write(bytes, 0, bytes.Length)

Is this correct ? Please do let me know.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial