Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi Guys,
 
I'm facing the following problem while compressing and decompressing a string. To make it understand better, I'm including the code in step by step manner.
 
1. I've a JSON string something like the following one
 
[{"FirstName":"First Name 0","LastName":"Last Name 0","Department":{"DepartmentID":0,"DepartmentName":"Department Name 0"}},{"FirstName":"First Name 1","LastName":"Last Name 1","Department":{"DepartmentID":1,"DepartmentName":"Department Name 1"}},{"FirstName":"First Name 2","LastName":"Last Name 2","Department":{"DepartmentID":2,"DepartmentName":"Department Name 2"}},{"FirstName":"First Name 3","LastName":"Last Name 3","Department":{"DepartmentID":3,"DepartmentName":"Department Name 3"}},{"FirstName":"First Name 4","LastName":"Last Name 4","Department":{"DepartmentID":4,"DepartmentName":"Department Name 4"}},{"FirstName":"First Name 5","LastName":"Last Name 5","Department":{"DepartmentID":5,"DepartmentName":"Department Name 5"}}]
 
2. I'm using ICSharpCode.SharpZipLib to compress the string using the following routine
 
public static string CompressString(string inflatedString)
        {
            string result = string.Empty;
            byte[] buffer = new byte[4096];
            byte[] stringButes = new System.Text.UTF8Encoding().GetBytes(inflatedString);
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (ZipOutputStream zs = new ZipOutputStream(ms))
                    {
                        zs.SetLevel (9);
                        ZipEntry entry = new ZipEntry("Compress.txt");
                        zs.PutNextEntry(entry);
                        using (MemoryStream msSource = new MemoryStream(stringButes))
                        {
                            StreamUtils.Copy(msSource, zs, buffer);
                        }
                    }
                    byte[] compressedStringBytes = ms.ToArray();
                    
                    StringBuilder compressedStringBuilder = new StringBuilder(compressedStringBytes.Length);
                    foreach (byte charItem in compressedStringBytes)
                    {
                        compressedStringBuilder.Append((char)charItem);
                    }
 
                    result = compressedStringBuilder.ToString();
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
 
3. And using the following routine for decompression
 
public static string DeCompressString(string compressedString)
        {
            string result = string.Empty;
            StringBuilder defaltedString = new StringBuilder();
            byte[] compressedStringBytes = new byte[compressedString.Length];
            for (int foo = 0; foo < compressedString.Length; foo++)
            {
                compressedStringBytes[foo] = (byte)compressedString[foo];
            }
 
            byte[] data = null;
            try
            {
                using (MemoryStream ms = new MemoryStream(compressedStringBytes))
                {
                    using (ZipInputStream zs = new ZipInputStream(ms))
                    {
                        ZipEntry entry = null;
                        while((entry = zs.GetNextEntry()) != null)
                        {
                            data = new byte[entry.Size];
                            using (MemoryStream msDest = new MemoryStream(data))
                            {
                                using (StreamWriter writer = new StreamWriter(msDest))
                                {
                                    if (zs.Read(data, 0, data.Length) > 0)
                                    {
                                        writer.Write(data);
                                    }
                                }
                            }
                        }
                    }
                    result = new System.Text.UTF8Encoding().GetString(data, 0, data.Length);                    
 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }

 
Now the problem is when the above stated JSON string gets compressed (using the above stated routine)and using the same compressed string when I decompress (using the above stated routine) I'm getting the following decompressed string, which clearly are not same..
 
System.Byte[]:"First Name 0","LastName":"Last Name 0","Department":{"DepartmentID":0,"DepartmentName":"Department Name 0"}},{"FirstName":"First Name 1","LastName":"Last Name 1","Department":{"DepartmentID":1,"DepartmentName":"Department Name 1"}},{"FirstName":"First Name 2","LastName":"Last Name 2","Department":{"DepartmentID":2,"DepartmentName":"Department Name 2"}},{"FirstName":"First Name 3","LastName":"Last Name 3","Department":{"DepartmentID":3,"DepartmentName":"Department Name 3"}},{"FirstName":"First Name 4","LastName":"Last Name 4","Department":{"DepartmentID":4,"DepartmentName":"Department Name 4"}},{"FirstName":"First Name 5","LastName":"Last Name 5","Department":{"DepartmentID":5,"DepartmentName":"Department Name 5"}}]
 
You can see the first few characters of the string is missing and a new System.Byte[] is included instead.
 
I'm constantly trying to figure it out, but till not without any luck..
It will be great, if someone puts some light, whether Im missing something or not...
Posted 26-Sep-12 22:21pm
Comments
Timberbird at 27-Sep-12 4:40am
   
If you dump your msSource stream content to a text file while compressing, does that text file still contain the original string, without prefix?
senguptaamlan at 27-Sep-12 5:23am
   
I used the same logic with FileStream, its working perfect, but with MemoryStream, I'm facing this error :(
Timberbird at 27-Sep-12 6:30am
   
Some general recommendation is to set current position at the beginning of the stream, but I do believe it's not the reason :). Try saving content of the msSource to the file and then check it to make sure it's correct just before encoding
senguptaamlan at 27-Sep-12 7:18am
   
Yeah, I've also seen that, the actual length to put in the first 4 bytes of the compressed string....but, don't know why to do that. Does't its getting handled by the library itself? Please, let me know if you have any other idea...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Documentation says ...
 
My decompressed files are invalid whats wrong?
 
When reading from streams you should keep reading until the end of the stream has been reached. Do not assume that because you provide a buffer big enough that it will be filled in a single read. When a read returns 0 the end of the stream has been reached.
 
{{ StreamUtils.Copy(sourceStream, destinationStream, new byte&#0091;2048&#0093); }}
 

Maybe that helps , meaning you should not predefine the length of resulting stream ...
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100