GZipStream压缩和解压缩的麻烦 [英] Trouble with GZipStream compression and decompression

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

问题描述

您好,我使用压缩器类来压缩和解压缩数据,问题是在解压缩后我只得到一部分压缩数据。要压缩的字符串如下所示 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 *,字节数组是用 Encoding.UTF8.GetBytes(text)获得的,在解压后我只得到 2.4,3#5.8,25.3,41 *。对我来说这是第一次使用压缩和解压缩,所以我不知道到底会发生什么。

有谁能帮我解决这个问题?







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?



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

推荐答案

嗯......我不太确定你的位置具体问题是,但是......你可能想看看移动线:

Well...I'm not quite sure where your specific problem is, but...you might want to look at moving the line:
memoryStream.Write(buffer, 0, bytesRead);

在循环中读取zip文件中的数据 - 或者你将丢弃除了你读取的最后一个块之外的所有内容。

哪个听起来有点像你得到的问题,但不应该在那个小的数据输入上。值得修理 - 它可以解决你的问题。

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.

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


这篇关于GZipStream压缩和解压缩的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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