我有一個gzip文件,其中包含需要清理的txt文件。我想從線gzip壓縮文件中的行讀,然後寫了清洗內容輸出GZIP文件都在同一個鏡頭是這樣的:帶StreamReader.ReadLine的GZipStream只讀第一行
void ExtractAndFix(string inputPath, string outputPath) {
StringBuilder sbLine = new StringBuilder();
using (GZipStream gzInput = new GZipStream(new FileStream(inputPath, FileMode.Open), System.IO.Compression.CompressionMode.Decompress)) {
using (StreamReader reader = new StreamReader(gzInput, Encoding.UTF8)) {
using (GZipOutputStream gzipWriter = new GZipOutputStream(new FileStream(outputPath, FileMode.Create))) {
string line = null;
while ((line = reader.ReadLine()) != null) {
sbLine.Clear();
sbLine.Append(line.Replace("\t", " "));
sbLine.Append("\r\n");
byte[] bytes = Encoding.UTF8.GetBytes(sbLine.ToString());
gzipWriter.Write(bytes, 0, bytes.Length);
}
}
}
}
}
但由於某些原因,打電話線= reader.ReadLine( )只在while循環中讀取一次,然後返回null(reader EOS = true)。我已經嘗試了與原生C#壓縮庫和ICSharpCode包以及我得到相同的行爲。我意識到我總是可以提取完整的文件,然後清理它,然後重新壓縮它,但我討厭不得不浪費資源,硬盤空間等。注意:這些是大文件(高達幾GB壓縮),所以任何東西MemoryStream不會是一個好的解決方案。有沒有人遇到過這種奇怪的東西?謝謝。
你確定該文件實際上只是壓縮流而不是Zip存檔? – 2014-09-18 17:30:25
@ Alexei Levenkov - 如果是Zip,將永遠無法創建GZip流,否則會失敗,因爲文件類型不正確 – bruiseruser 2014-09-18 19:23:16
[使用GZipStream解壓僅返回第一行](http:// stackoverflow的.com /問題/ 11204330 /解壓縮使用- - gzipstream-返回僅最一線) – Sam 2017-03-15 23:16:34