c#缓冲GZipStream压缩

本文关键字:压缩 GZipStream 缓冲 | 更新日期: 2023-09-27 18:07:00

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

//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)有关。

读取缓冲多线程,处理大文件。所以…为什么最后一个字节丢失了?我哪里做错了?

c#缓冲GZipStream压缩

试试这样写:

  • 使用GZipStream的构造函数,关闭FileStreamDispose

    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;
    }
    }
    

如果你想要的是压缩文件在同一个文件夹,然后我尝试下面的代码是工作的。

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            FileInfo fi = new FileInfo(@"F:'Harris'abc.txt");
            FileStream fs = File.OpenRead(@"F:'Harris'abc.txt");
            using (FileStream compressedFileStream = File.Create(fi.FullName + ".gz"))
            {
                using (GZipStream compressionStream = new GZipStream(compressedFileStream,
                System.IO.Compression.CompressionMode.Compress, true))
                {
                    fs.CopyTo(compressionStream);
                }
            }//end using
            MessageBox.Show("Done");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally { }
    }