2016-09-15 104 views
2

我正在编写database备份功能,从System.Diagnostics.Process object,从StandardOutput (StreamReader)属性读取。我已成功写入纯文件。C#缓冲GZipStream压缩

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

读取缓冲为多线程,用于处理大文件。那么......为什么最后的字节会丢失?我在哪里做错了?

+3

有什么可疑的是,你从来没有关闭/处置你的'zipStream'。这可能没有必要,但这是我第一次尝试。 – hvd

+0

是的,你正在关闭一些未显示的'fs',但可能是底层文件流。如果不让GZipStream完成工作,你无法关闭它。总是关闭最后一个,而不是链中的第一个。 –

+0

fs是代码片段顶部的FileStream。 –

回答

2

试着这么做:

  • 使用的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; 
    } 
    } 
    
+0

@hvd注释(关闭zipstream)中包含'FileStream'和'GZipStream'解决了这个问题。但是这个答案也解决了这个问题。不知何故,这个代码更整洁。谢谢。 –

0

如果你想要的是拉链在同一文件夹中的文件,那么我尝试下面的代码正在工作。

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