我正在写一个database备份函数,从System.Diagnostics.Process object属性的StandardOutput (StreamReader)中读取。我已成功写入普通文件。

//This code successfully wrote text files.
StreamWriter f = new StreamWriter(BackupPath);
while (true) {
  //RaiseProgressedEvent(new DBProgressEventArgs(dbsize, progress, "Writing backup file"));

  int buffsize = 512;
  char[] buff = new char[buffsize];
  int count = p.StandardOutput.ReadBlock(buff, 0, buff.Length);
  if (count == 0) break;
  // If no more data, trim the char array
  if (p.StandardOutput.Peek() < 0) buff = (from c in buff where c > 0 select c).ToArray();

  f.Write(buff, 0, count);
  progress += buffsize;
}
f.Close();

但是当我更改为GZipStream时:
//This code yields a broken gzip file.
//*2 lines changed: StreamWriter changed into FileStream.
FileStream fs = File.Create(BackupPath);
GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, true);

while (true) {
  RaiseProgressedEvent(new DBProgressEventArgs(dbsize, progress, "Writing backup file"));

  int buffsize = 512;
  char[] buff = new char[buffsize];
  int count = p.StandardOutput.ReadBlock(buff, 0, buff.Length);
  if (count == 0) break;
  if (p.StandardOutput.Peek() < 0) buff = (from c in buff where c > 0 select c).ToArray();

  //With UTF 8 Encoding, write to gzipstream.
  //f.write changed into the following 2 lines:
  Encoding enc = Encoding.UTF8;
  zipStream.Write(enc.GetBytes(buff), 0, enc.GetByteCount(buff));

  progress += buffsize;
}
fs.Close();

生成的GZip文件不完整/损坏。当使用7zip解压缩然后使用notepad++打开时,几乎所有文本都是好的,只有文件末尾附近的一些字节丢失了。我不确定,但也许错误就在附近:zipStream.Write(enc.GetBytes(buff), 0, enc.GetByteCount(buff));也许与enc.GetByteCount(buff)有关。

读取内容被缓冲以用于多线程处理大型文件。那么...为什么最后一个字节丢失了?我在哪里做错了?

最佳答案

尝试类似的东西:

  • 使用GZipStream的构造函数,该构造函数关闭FileStream后的Dispose
    using(FileStream fs = File.Create(BackupPath))
    using(GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false))
    {
    
     while (true) {
      RaiseProgressedEvent(new DBProgressEventArgs(dbsize, progress, "Writing backup file"));
    
     int buffsize = 512;
     char[] buff = new char[buffsize];
     int count = p.StandardOutput.ReadBlock(buff, 0, buff.Length);
     if (count == 0) break;
     if (p.StandardOutput.Peek() < 0) buff = (from c in buff where c > 0 select c).ToArray();
    
     //With UTF 8 Encoding, write to gzipstream.
     //f.write changed into the following 2 lines:
     Encoding enc = Encoding.UTF8;
     zipStream.Write(enc.GetBytes(buff), 0, enc.GetByteCount(buff));
    
     progress += buffsize;
    }
    }
    
  • 关于c# - C#缓冲的GZipStream压缩,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39503393/

    10-13 06:47