【问题标题】:Unable to decompress Bz2 File has Orginal file using Dotnetzip library无法使用 Dotnetzip 库解压缩 Bz2 文件具有原始文件
【发布时间】:2015-06-06 22:03:18
【问题描述】:

我有 10 行的文件,我正在压缩为 Bz2 格式,但是当我解压缩它时,我看到生成的文件只有 9 行。有1.5行的数据丢失。这是我压缩到 Bz2 的代码。我正在使用 DotNet zip 库 https://dotnetzip.codeplex.com/

以下是压缩代码,我将文件转换为 UTF-8 和 Bz2 文件。

    static string Compress(string sourceFile, bool forceOverwrite)
    {
        var outFname = fname + ".bz2";

        if (File.Exists(outFname))
        {
            if (forceOverwrite)
                File.Delete(outFname);
            else
                return null;
        }
        long rowCount = 0;
        var output = File.Create(outFname);

        try
        {
              using (StreamReader reader = new StreamReader(fname))
            {
                using (var compressor = new Ionic.BZip2.ParallelBZip2OutputStream(output))
                {
                    StreamWriter writer = new StreamWriter(compressor, System.Text.Encoding.UTF8);
                     string line = "";
                    while ((line = reader.ReadLine()) != null)
                    {
                        writer.WriteLine(line);
                        rowCount++;

                        if (rowCount % 100000 == 0)
                            Console.WriteLine("InProgress..Current Row # " + rowCount.ToString());
                    }

                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            if (output != null)
                output = null;
        }

        //     Pump(fs, compressor);

        return outFname;
    }

我厌倦了像下面这样改变阅读方法

            // int charsRead;
                    // char[] buffer = new char[2048];
                    // while ((charsRead = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
                    // {
                    //     writer.Write(buffer, 0, charsRead);
                    //     rowCount++;

                    //     if (rowCount % 100000 == 0)
                    //         Console.WriteLine("InProgress..Current Row # " + rowCount.ToString());
                    // }

解压,代码如下

    public static string Decompress(string fname, bool forceOverwrite)
    {
        var outFname = Path.GetFileNameWithoutExtension(fname);
        if (File.Exists(outFname))
        {
            if (forceOverwrite)
                File.Delete(outFname);
            else
                return null;
        }

        using (Stream fs = File.OpenRead(fname),
               output = File.Create(outFname),
               decompressor = new Ionic.BZip2.BZip2InputStream(fs))
            Pump(decompressor, output);

        return outFname;
    }

    private static void Pump(Stream src, Stream dest)
    {
        byte[] buffer = new byte[2048];
        int n;
        while ((n = src.Read(buffer, 0, buffer.Length)) > 0)
            dest.Write(buffer, 0, n);

    }

在调试过程中,我看到 readline 正在正确读取数据,不确定在将实际文件转换为 Bz2 或从 Bz2 读取时是否是此库 dll 中的错误。请告诉我这个问题的原因

【问题讨论】:

    标签: c# bzip2 gzip


    【解决方案1】:

    建议您从这里获取 DotNetZip:https://www.nuget.org/packages/DotNetZip/

    我必须在 while 循环之后直接将以下两行添加到您的 Compress 方法中。

    writer.Close();
    compressor.Close();
    

    没有它,您的代码将引发异常。之后,您的代码在我运行的 10 行文本文件中运行良好。没有丢失的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多