0
我有10行的文件,我正在壓縮到Bz2格式,但是當我解壓縮它時,我看到生成的文件只有9行。有1.5行數據丟失。這是我的壓縮到Bz2的代碼。我使用DotNet zip庫 https://dotnetzip.codeplex.com/無法解壓縮Bz2文件使用Dotnetzip庫的原始文件
以下是壓縮代碼,我將文件轉換爲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被正確讀取數據,不能確定是否它是這個庫DLL中的錯誤,將實際文件轉換爲Bz2或從Bz2讀取。請讓我知道原因,這個問題