使用GZipStream解压缩仅返回第一行 [英] Decompressing using GZipStream returns only the first line

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

问题描述

我一直在研究解析第三方fms日志的功能.日志位于Gzip中,因此我使用了一种解压缩功能,该功能适用​​于我们使用的任何其他Gzip文件.

I’ve been working on a function parsing 3rd party fms logs. The logs are in Gzip, so I use a decompressing function that works for any other Gzip files we use.

解压缩这些文件时,我只会得到压缩文件的第一行,这也不例外,它只是找不到剩余的字节,就像在第一行有EOF一样. 我尝试使用Ionic.Zlib而不是System.IO.Compression,但结果是相同的.这些文件似乎没有任何损坏,请使用Winrar对其进行解压缩.

When decompressing these files I only get the first line of the compressed file, there’s no exception, it just doesn’t find the rest of the bytes as if there's an EOF at the first line. I tried using Ionic.Zlib instead of System.IO.Compression but the result was the same. The files don’t seem to be corrupted in any way, decompressing them with Winrar works.

如果有人对如何解决此问题有任何想法,我们将感谢您的帮助. 谢谢

If anybody has any idea of how to solve this, I’ll appreciate your help. Thanks

您可以在此处下载示例文件: http://www.adjustyourset.tv/fms_6F9E_20120621_0001.log.gz

You can download a sample file here: http://www.adjustyourset.tv/fms_6F9E_20120621_0001.log.gz

这是我的减压功能:

    public static bool DecompressGZip(String fileRoot, String destRoot)
    {
        try
        {
            using (FileStream fileStram = new FileStream(fileRoot, FileMode.Open, FileAccess.Read))
            {
                using (FileStream fOutStream = new FileStream(destRoot, FileMode.Create, FileAccess.Write))
                {
                    using (GZipStream zipStream = new GZipStream(fileStram, CompressionMode.Decompress, true))
                    {
                        byte[] buffer = new byte[4096];
                        int numRead;

                        while ((numRead = zipStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            fOutStream.Write(buffer, 0, numRead);
                        }
                        return true;
                    }
                }

            }
        }
        catch (Exception ex)
        {
            LogUtils.SaveToLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "Eror decompressing " + fileRoot + " : " + ex.Message, Constants.systemLog, 209715200, 6);
            return false;
        }
    }

推荐答案

我花了最后45分钟来解决这个问题,但是我无法解释为什么它不起作用. DeflateStream类不知何故无法正确解码您的数据.我编写了自己的GZip解析器(如果有人想检查它,我可以共享代码),它读取所有标头并检查其有效性(以确保其中没有有趣的东西),然后使用DeflateStream来对标头进行膨胀实际数据,但对于您的文件,它仍然只是我的第一行.

I've put the last 45 minutes wrapping my head around this problem but I just can't explain why it isn't working. Somehow the DeflateStream-class isn't decoding your data properly. I wrote up my own GZip-parser (I can share the code if anyone wants to check it) which reads all the headers and checks them for validity (to make sure that there are no funny stuff there) and then use DeflateStream to inflate the actual data but with your file it still just gets me the first line.

如果我使用GZipStream使用您的日志文件重新压缩(首先使用winrar解压缩后),那么我自己的解析器和您自己的示例都可以再次进行压缩.

If I recompress using your logfile using GZipStream (after first decompressing it with winrar) then it is decompressed just fine again both my my own parser and your own sample.

关于Microsoft实施Deflate(http://www.virtualdub.org/blog/pivot/entry.php?id=335),网上似乎有些批评,因此可能是您发现其中之一怪癖.

There seems to be some critizism on the net about Microsofts implementation of Deflate (http://www.virtualdub.org/blog/pivot/entry.php?id=335) so it might be that you found one of it's quirks.

但是,解决您的问题的简单方法是切换到SharZipLib(http://www.icsharpcode.net/opensource/sharpziplib/),我尝试了一下,它可以很好地解压缩文件.

However, a simple solution to your problem is to switch to SharZipLib (http://www.icsharpcode.net/opensource/sharpziplib/), I tried it out and it can decompress your file just fine.

    public static void DecompressGZip(String fileRoot, String destRoot)
    {
        using (FileStream fileStram = new FileStream(fileRoot, FileMode.Open, FileAccess.Read))
        using (GZipInputStream zipStream = new GZipInputStream(fileStram))
        using (StreamReader sr = new StreamReader(zipStream))
        {
            string data = sr.ReadToEnd();
            File.WriteAllText(destRoot, data);
        }
    }

这篇关于使用GZipStream解压缩仅返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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