By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

.net 2.0 - Gzip Compression Question

P: 6
Hi, and thanks for viewing my post.

i have an odd result when trying to compress and decompress a string.
it seems that when i replace the Zero's in the input stream it works,
if not the string decompresses to the first Zero.
For some string it just cuts off the last characters.
Thank in advanced.

The Code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Text;
  3. using System.IO.Compression;
  4. using System.IO;
  5.  
  6. namespace Zipping
  7. {
  8.           class Program
  9.           {
  10.                 static void Main(string[] args)
  11.                 {
  12.                        string UnCompressed = "0123456789012345678901234567890123456789";
  13.                        Console.WriteLine(UnCompressed);
  14.                        byte[] Compressed = compress(stringToByte (UnCompressed));
  15.                        Console.WriteLine(decompress(Compressed));
  16.                        Console.ReadKey();
  17.                 }
  18.                 static public byte[] stringToByte(string input){return(Encoding.UTF8.GetBytes(input));}
  19.                 static public string byteToString(byte[] input){return(Encoding.UTF8.GetString(input));}
  20.                 static public byte[] compress(byte[] input)
  21.                 {
  22.                        MemoryStream memoryStream = new MemoryStream();
  23.                        GZipStream gzip = new GZipStream (memoryStream, CompressionMode.Compress);
  24.                        gzip.Write(input,0,input.length);
  25.                        return memoryStream.ToArray();
  26.                 }
  27.                 static public string compress(byte[] input)
  28.                 {
  29.                        MemoryStream memoryStream = new MemoryStream(input);
  30.                        GZipStream gzip = new GZipStream (memoryStream, CompressionMode.Decompress);
  31.                        StreamReader strmR = new StreamReader(gzip);
  32.                        strmR.Peek();//Won't Read to End if I don't peek first, don't know why.
  33.                        return strmR.ReadToEnd();
  34.                 }
  35.           }
  36. }
  37.  
Aug 11 '08 #1
Share this Question
Share on Google+
6 Replies


P: 6
-BUMP- please help me
Aug 14 '08 #2

kenobewan
Expert 2.5K+
P: 4,871
Here is an article that may help:
More on GZip compression with ASP.NET Content
Aug 14 '08 #3

P: 6
this doesn't really help, i don't try to compress asp.net pages, i am trying to compress a string
Aug 14 '08 #4

P: 79
I suggest you use SharpZipLib instead. GZipStream is very limited.
Aug 15 '08 #5

P: 6
i have to use gzip, i can't use any dll that doesn't come with VS2005
Aug 15 '08 #6

P: 6
-bump- thanks. _
Aug 19 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.