정리해야 할 txt 파일이 들어있는 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 전화를 몇 가지 이유 : I 라인으로 gzip으로 압축 된 파일 라인을 읽고 모든이 같은 한 번에 출력 GZIP 파일로 청소 내용을 작성하려합니다) while 루프는 한 번만 읽은 다음 null (reader EOS = true)을 반환합니다. 나는 네이티브 C# 압축 라이브러리와 ICSharpCode 패키지를 사용하여이 작업을 시도했지만 동일한 동작을합니다. 나는 항상 전체 파일을 추출한 다음 다시 정리하고 다시 압축해야하지만 리소스, 하드 드라이브 공간 등을 낭비하지 않아도된다는 것을 알았습니다. 참고 :이 파일은 크기가 큰 (최대 몇 GB까지 압축) 파일이므로 MemoryStream은 좋은 솔루션이 될 수 없습니다. 전에 누구도 이런 이상한 일이 있었습니까? 고맙습니다.
파일이 실제로 압축 된 스트림이며 우편 아카이브가 아닌 것이 확실합니까? –
@ Alexei Levenkov - Zip 경우 GZip 스트림을 만들 수 없으므로 파일 형식이 올바르지 않으므로 실패 할 수 있습니다. – bruiseruser
[GZipStream을 사용하여 압축 풀기] 가능한 복제본은 첫 번째 줄만 반환합니다. (http : // stackoverflow .com/questions/11204330/압축 해제 - gzipstream 사용 - 첫 번째 줄만 표시) – Sam