字符串解压缩不起作用 [英] Decompression of string not working

查看:104
本文介绍了字符串解压缩不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在压缩和解压缩字符串时,我面临以下问题.为了更好地理解它,我将逐步添加代码.

1.我有一个类似于以下内容的JSON字符串

[{"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.我正在使用ICSharpCode.SharpZipLib使用以下例程压缩字符串

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.并使用以下例程进行减压



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;
        }



现在的问题是,当上述JSON字符串被压缩(使用上述例程)并且在我进行解压缩(使用上述例程)时使用相同的压缩字符串时,我得到以下经解压缩的字符串,这显然是不相同的..



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"}}]



您可以看到字符串的前几个字符丢失了,而是包含了一个新的System.Byte [].

我一直在努力弄清楚,但直到没有运气..
如果有人给我一点光,不管我是否错过了某些东西,那都会很棒.



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

推荐答案

文档说...

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


这篇关于字符串解压缩不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆