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