Click here to Skip to main content
15,897,371 members

Decompression of string not working

senguptaamlan asked:

Open original thread
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

C#
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

C#
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..

JavaScript
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...
Tags: C#, Compression, Decompression

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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