Bharanee

Hi,

Greetings. I've to Zip a string using .Net framework.Can any one tell me how can i implement this.Is it possible to store the string in a variable and can read and zip it.

Looking for your responses. Thanks in advance.




Re: .NET Base Class Library How to Zip a String using .Net framework

claude baillargeon

Download SharpZipLib:

http://www.icsharpcode.net/OpenSource/SharpZipLib/

It is free.






Re: .NET Base Class Library How to Zip a String using .Net framework

nobugz

If you're just looking for compression and don't care about storing it in an archive for another app to use, you could use System.IO.Compression.GZipStream. For example:

using System;
using System.IO;
using System.IO.Compression;

public class CompressedString {
private MemoryStream mStore;
private int mBytes;
public CompressedString(string s) {
mStore = new MemoryStream();
using (GZipStream strm = new GZipStream(mStore, CompressionMode.Compress, true)) {
byte[] enc = System.Text.Encoding.Unicode.GetBytes(s);
strm.Write(enc, 0, enc.Length);
mBytes = enc.Length;
}
}
public string Expand() {
byte[] buf = new byte[mBytes];
mStore.Seek(0, SeekOrigin.Begin);
using (GZipStream strm = new GZipStream(mStore, CompressionMode.Decompress, true))
for (int len = 0; len < mBytes; )
len += strm.Read(buf, len, mBytes - len);
return new String(System.Text.Encoding.Unicode.GetChars(buf));
}
}

You'll of course want to avoid calling CompressedString.Expand() too often.





Re: .NET Base Class Library How to Zip a String using .Net framework

Bharanee

Hi,

Thank you for your kind response. Could you pls tell me how to unzip the string.

Thanks in advance.