Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i use the compressor class to compress and decompress data, the problem is that after the decompressing i only get a portion of the compressed data. The to be compressed string looks like this string text = "3.15,23.6,10.9#0.1,2.3,5.6#56.8,10,11*4.25,2,0.9#0.2,2.4,3#5.8,25.3,41*" and the byte array is obtained with Encoding.UTF8.GetBytes(text) and after decompression i get only "2.4,3#5.8,25.3,41*". For me its the first time using the compression and decompression so i don't know exactly what to expect.
Can anyone help me solve this problem?



C#
public static class Compressor
   {

       public static void Compress(string filename, byte[] bytesToWrite)
       {

           using (FileStream destinationStream = File.Open(filename, FileMode.Create))
           {
               if (destinationStream == null)
               {
                   throw new ArgumentException();
               }

               if (bytesToWrite == null)
               {
                   throw new ArgumentException();
               }

               if (!destinationStream.CanWrite)
               {
                   throw new ArgumentException();
               }

               using (GZipStream gzipStream = new GZipStream(destinationStream, CompressionMode.Compress))
               {
                   gzipStream.Write(bytesToWrite, 0, bytesToWrite.Length);
               }
           }
       }
       public static string Decompress(string filename)
       {
           string text = "";
           using (FileStream sourceStream = File.OpenRead(filename))
           {

               if (sourceStream == null)
               {
                   throw new ArgumentException();
               }

               if (!sourceStream.CanRead)
               {
                   throw new ArgumentException();
               }

               MemoryStream memoryStream = new MemoryStream();
               FileInfo f = new FileInfo(filename);

               const int bufferSize = 65536;

               using (GZipStream gzipStream = new GZipStream(sourceStream, CompressionMode.Decompress))
               {
                   byte[] buffer = new byte[bufferSize];

                   int bytesRead = 0;

                   do
                   {
                       bytesRead = gzipStream.Read(buffer, 0, bufferSize);
                   }
                   while (bytesRead == bufferSize);

                   memoryStream.Write(buffer, 0, bytesRead);
               }

               text = Encoding.UTF8.GetString(memoryStream.ToArray());
           }
           return text;
       }
   }
Posted
Updated 4-May-14 4:31am
v4

1 solution

Well...I'm not quite sure where your specific problem is, but...you might want to look at moving the line:
C#
memoryStream.Write(buffer, 0, bytesRead);
inside the loop which reads data from the zip file - or you will just throw away everything except the last block you read.
Which does kinda sound like the problem you are getting, but shouldn't on that small a data input. Worth fixing though - it might solve your problem.
C#
do
{
    bytesRead = gzipStream.Read(buffer, 0, bufferSize);
    memoryStream.Write(buffer, 0, bytesRead);
} while (bytesRead == bufferSize);
 
Share this answer
 
Comments
aryx123 4-May-14 12:08pm    
Thank you very much! that was the problem
OriginalGriff 4-May-14 12:29pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900