SharpZipLib question ...

  • Thread starter Thread starter JustMe
  • Start date Start date
J

JustMe

Hi:

I'm sure this is very simple, but I can't figure it out.

I have a string value in vb.net that I want to 'compress' before
sending it over a socket connection.

How can I do this using SharpZipLib without saving the string to a
file? Is this possible?

Basically I want to do something like this:

strOld = "Long series of characters ... "
strCompressed = Compress(strOld)

Any help would be greatly appreciated.

--Terry
 
JustMe said:
Hi:

I'm sure this is very simple, but I can't figure it out.

I have a string value in vb.net that I want to 'compress' before
sending it over a socket connection.

How can I do this using SharpZipLib without saving the string to a
file? Is this possible?

Basically I want to do something like this:

strOld = "Long series of characters ... "
strCompressed = Compress(strOld)

Any help would be greatly appreciated.

--Terry

Create a method like this (sorry, it's C#, should be trivial to convert
though):

public byte[] Compress(byte[] squish_me, int offset, int count)
{
MemoryStream zipped = new MemoryStream();
Stream s = new DeflaterOutputStream(zipped);
s.Write(squish_me, offset, count);
s.Close();
return zipped.ToArray();
}

And call it like this:

strOld = "Long series of characters ... ";
byte[] buff = System.Text.Encoding.ASCII.GetBytes(strOld);
byte[] compressed = Compress(buff, 0, buff.Length);

I wouldn't recommend converting the compressed byte array back to a
string, as it's binary data. Plus, the socket Send() methods want byte
arrays anyway. You'll want some error-checking in there too.

HTH.
 
Thanks for your reply.

I searched the SharpZipLib forums and found a vb.net sample that worked
great.

Thanks again for getting me on the right track!!!

--Terry
 
Back
Top