解GZIP文件解出来的文件比实际的小
我用sharplib和IO.compression类解GZIP文件解出来的文件比实际的小怎么回事呀,谁遇到过这个情况,因为是文本文件,所以只是少了很多文字,解压还是成功了 --------------------编程问答-------------------- lz把你解压的那段代码贴出来看看。 --------------------编程问答-------------------- GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);// int totalLength = 0;
byte[] writeData = new byte[4096];
// FileStream fs = new FileStream(@"C:\EnergyMap\TempLog\LogFile.csv", FileMode.Create, FileAccess.ReadWrite);
//StreamUtils.Copy(zip, fs, buffer);
while (true)
{
int size = zip.Read(writeData, 0, writeData.Length);
if (size > 0)
{
fs.Write(writeData, 0, size);//写入解压文件代表的文件流
}
else
{
break;//若读到压缩文件尾,则结束
}
}
zip.Close();
fs.Close(); --------------------编程问答-------------------- GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
这里ms是什么Stream? --------------------编程问答-------------------- ms是打开文件的内存流 --------------------编程问答-------------------- MemoryStream? 换成FileStream试试? --------------------编程问答-------------------- 这是我解压Zip的代码,看看有没有帮助?
Using fs As FileStream = New FileStream(zip, FileMode.Open)--------------------编程问答-------------------- 这个情况很正常。压缩的时候没有将文件长度放到里面,所以解压的时候得到的结果只是和压缩后长度一样的一小段。
Using zis As ZipInputStream = New ZipInputStream(fs)
Dim ze As ZipEntry = zis.GetNextEntry()
While ze IsNot Nothing
If ze.IsFile Then
Dim fileName As String = Path.GetFileName(ze.Name)
Dim destPath As String = Path.Combine(unzipDir, Path.GetFileName(zip).Substring(0, 3))
If Not Directory.Exists(destPath) Then
Directory.CreateDirectory(destPath)
End If
destPath = Path.Combine(destPath, fileName)
Dim writer As New FileStream( _
destPath, System.IO.FileMode.Create, _
System.IO.FileAccess.Write, System.IO.FileShare.Write)
Dim buffer(4096) As Byte
Dim len As Integer
While True
len = zis.Read(buffer, 0, buffer.Length)
If len <= 0 Then
Exit While
End If
writer.Write(buffer, 0, len)
End While
writer.Close()
End If
ze = zis.GetNextEntry()
End While
End Using
End Using
解决方法也很简单,压缩的时候将原来的长度作为第一位写入MemoryStream,解压的时候先读出这一位,然后开始解压。
参见
http://www.codeproject.com/KB/dotnet/mscompression.aspx
补充:.NET技术 , C#