我正在使用.Net GZipStream类来压缩和解压缩文件。在我进行解压缩之后,数据看起来很好,但在某个看似随意的点之后,它只会变为零。例如,在解压缩文件后,它的大小正确为19KB,但字节10,588和on都是零。
我不确定我做错了什么。
这就是我压缩的方法:
Byte[] bytes = GetFileBytes(file);
using (FileStream fileStream = new FileStream("Zipped.gz", FileMode.Create))
{
using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Compress))
{
zipStream.Write(bytes, 0, bytes.Length);
}
}
这就是我如何进行解压缩(Bytes是压缩字节数组,而OriginalSize是文件压缩之前的大小):
using (MemoryStream memoryStream = new MemoryStream(Bytes))
{
using (GZipStream zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
// Note: Since the compressed version can be larger, I use the larger of the original and the compressed size for the decompressed array's size.
Byte[] decompressedBytes = new Byte[OriginalSize > Bytes.Length ? OriginalSize : Bytes.Length];
Int32 numRead = zipStream.Read(decompressedBytes, 0, Bytes.Length);
using (FileStream fileStream = new FileStream("Decompressed.txt", Name), FileMode.Create))
{
fileStream.Write(decompressedBytes, 0, Convert.ToInt32(OriginalSize));
}
}
}