【发布时间】: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 中的错误。请告诉我这个问题的原因
【问题讨论】: