对二进制数组进行压缩?

ClsData 2007-11-21 02:29:27
看到的例子
传入的对象是二进制数组
压缩后返回

private static byte[] CompressByteData(Byte[] dataValue)
{
byte[] resultValue = null;
try
{
//压缩数据
Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
compressFile.SetInput(dataValue);
compressFile.Finish();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!compressFile.IsFinished)
{
int bufLength = compressFile.Deflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();

}
catch (Exception e)
{
System.Console.WriteLine("压缩数据失败!" + e.Message);
}

//返回压缩后的数据
return resultValue;
}

问题时怎么看压缩的效果呢?
我加上这段代码后确实运行了,也返回了。
可是在压缩前byte b.lenth和压缩后的是一样的,我怎么看压缩的效果呢?
...全文
365 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2007-12-17
  • 打赏
  • 举报
回复
Encoding gb2312 = Encoding.GetEncoding("gb2312");
FileStream fs = new FileStream("g:\\temp\\gb2312.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, gb2312);
MemoryStream ms = new MemoryStream();
string line = null;
byte[] buf;
while ((line = sr.ReadLine()) != null)
{
buf = gb2312.GetBytes(line + "\r\n");
ms.Write(buf, 0, buf.Length);
}
ms.Close();
sr.Close();
fs.Close();

int bytes;
Deflater deflater = new Deflater();
deflater.SetInput(ms.ToArray());
buf = new byte[1024];
ms = new MemoryStream();
while (!deflater.IsNeedingInput)
{
bytes = deflater.Deflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
deflater.Finish();
while (!deflater.IsFinished)
{
bytes = deflater.Deflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
ms.Close();

Inflater inflater = new Inflater();
inflater.SetInput(ms.ToArray());
ms = new MemoryStream();
while (!inflater.IsFinished)
{
bytes = inflater.Inflate(buf);
if (bytes <= 0)
break;
ms.Write(buf, 0, bytes);
}
ms.Close();

buf = ms.ToArray();
fs = new FileStream("g:\\temp\\gb2312.txt.bak", FileMode.Create);
fs.Write(buf, 0, buf.Length);
fs.Close();
humingyan 2007-11-22
  • 打赏
  • 举报
回复
关注!
ClsData 2007-11-22
  • 打赏
  • 举报
回复
上述问题已经解决
但是解压缩的时候又出现问题

private static byte[] DecompressByteData(byte[] dataValue)
{
byte[] resultValue = null;
try
{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
{
这里总是跳错
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception ex)
{
System.Console.WriteLine("解压缩数据失败!"+ex.Message);
}

//返回数据
return resultValue;
}

错误提示
Header checksum illegal

我想既然我压缩的时候没有任何问题
压缩后仍返回二进制数组
解压缩的时候也应该没有问题

不知道问题出在哪里呢
xiaojing7 2007-11-22
  • 打赏
  • 举报
回复
不会弄!帮顶~

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧