Zipping and unzipping


W

wl

Hi all,

I don't know whether is the best place for posting my message, but it just
drives me crazy and didn't find a better newgroup for this.
I'm using the ICSharpCode.SharpZipLib for zipping a string to a compressed
string and the other way around.

But when I do a compress/uncompress on "Wim Lambrechts" I received "Wae
Daebrec`ts", so it seems a single bit in each byte is turned off.

I'm using the following code for compressing a string into a compressed
string


public static string CompressString (string input)

{

MemoryStream instream = new MemoryStream(Encoding.ASCII.GetBytes(input));

byte[] buffer = new byte[instream.Length];

instream.Read(buffer, 0, buffer.Length);

instream.Close();

MemoryStream outstream = new MemoryStream();

ZipOutputStream s = new ZipOutputStream(outstream);

s.SetLength(9);


ZipEntry entry = new ZipEntry("");

Crc32 crc = new Crc32();

crc.Reset();

crc.Update(buffer);

entry.Size = buffer.Length;

entry.Crc = crc.Value;


s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);

s.Finish();

s.Close();

return (new ASCIIEncoding().GetString(outstream.GetBuffer()));

}



and for uncompressing:



public static string DecompressString2 (string input)

{

MemoryStream instream = new MemoryStream(Encoding.ASCII.GetBytes(input));

MemoryStream outstream = new MemoryStream();

ZipInputStream s = new ZipInputStream(instream);

s.GetNextEntry();

int size = 2048;

byte[] data = new byte[2048];

while (true)

{

size = s.Read(data, 0, data.Length);

if (size > 0)

{

outstream.Write(data, 0, size);

}

else

{

break;

}

}

return (new ASCIIEncoding().GetString(outstream.GetBuffer()));

}


Again, it drives me crazy: any help is welcome !!!

Wim
 
Ad

Advertisements

P

Peter Foot [MVP]

Since all Windows CE strings are Unicode system-wide, you should use Unicode
encoding rather than ASCII as you have used in the code below.

Peter
 
Ad

Advertisements

W

wl

Peter,

Thanks for the info: I must admit I didn't try it yet in a compact framework
application (is the intention though).
I did change the encoding and now it works !!! Didn't know what it has to do
with the encoding though ?

Thanks again !!!

Wim

Peter Foot said:
Since all Windows CE strings are Unicode system-wide, you should use Unicode
encoding rather than ASCII as you have used in the code below.

Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

wl said:
Hi all,

I don't know whether is the best place for posting my message, but it just
drives me crazy and didn't find a better newgroup for this.
I'm using the ICSharpCode.SharpZipLib for zipping a string to a compressed
string and the other way around.

But when I do a compress/uncompress on "Wim Lambrechts" I received "Wae
Daebrec`ts", so it seems a single bit in each byte is turned off.

I'm using the following code for compressing a string into a compressed
string


public static string CompressString (string input)

{

MemoryStream instream = new MemoryStream(Encoding.ASCII.GetBytes(input));

byte[] buffer = new byte[instream.Length];

instream.Read(buffer, 0, buffer.Length);

instream.Close();

MemoryStream outstream = new MemoryStream();

ZipOutputStream s = new ZipOutputStream(outstream);

s.SetLength(9);


ZipEntry entry = new ZipEntry("");

Crc32 crc = new Crc32();

crc.Reset();

crc.Update(buffer);

entry.Size = buffer.Length;

entry.Crc = crc.Value;


s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);

s.Finish();

s.Close();

return (new ASCIIEncoding().GetString(outstream.GetBuffer()));

}



and for uncompressing:



public static string DecompressString2 (string input)

{

MemoryStream instream = new MemoryStream(Encoding.ASCII.GetBytes(input));

MemoryStream outstream = new MemoryStream();

ZipInputStream s = new ZipInputStream(instream);

s.GetNextEntry();

int size = 2048;

byte[] data = new byte[2048];

while (true)

{

size = s.Read(data, 0, data.Length);

if (size > 0)

{

outstream.Write(data, 0, size);

}

else

{

break;

}

}

return (new ASCIIEncoding().GetString(outstream.GetBuffer()));

}


Again, it drives me crazy: any help is welcome !!!

Wim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top