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
milani_lucieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

0
milani_lucieAuthor Commented:
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
0
käµfm³d 👽Commented:
Yes. Now memoryStream will contain the bytes of the compressed string and you can pass that around as you like (in my case, to the FileStream).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.