PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Zipping and unzipping

Reply

Zipping and unzipping

 
Thread Tools Rate Thread
Old 12-11-2003, 09:07 PM   #1
wl
Guest
 
Posts: n/a
Default Zipping and unzipping


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


  Reply With Quote
Old 12-11-2003, 09:15 PM   #2
Peter Foot [MVP]
Guest
 
Posts: n/a
Default Re: Zipping and unzipping

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" <no-spam@no-spam.no-spam> wrote in message
news:%23IpPolWqDHA.2404@TK2MSFTNGP12.phx.gbl...
> 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
>
>



  Reply With Quote
Old 12-11-2003, 09:39 PM   #3
wl
Guest
 
Posts: n/a
Default Re: Zipping and unzipping

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 [MVP]" <feedback@nospam-inthehand.com> wrote in message
news:OWvaHqWqDHA.3320@tk2msftngp13.phx.gbl...
> 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" <no-spam@no-spam.no-spam> wrote in message
> news:%23IpPolWqDHA.2404@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> >

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off