GZipStream 中 报一个“GZip 头中的幻数不正确。请确保正在传入 GZip 流"这样的错误 在解压文件时,GZipStream 中 报一个“GZip 头中的幻数不正确。请确保正在传入 GZip 流。” 这样的错误 改怎么解决???? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 显然你的CompressStream用法有误,那个不是返回压缩流对象,你将要压缩的原始stream流放入了GZipStream初始化中,变成了输出流了,真正要返回的压缩流对象是GZipStream初始化中的第一个参数。 #region 压缩解压object public static byte[] CompressionObject(object DataOriginal) { if (DataOriginal == null) return null; BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(); bFormatter.Serialize(mStream, DataOriginal); byte[] bytes = mStream.ToArray(); MemoryStream oStream = new MemoryStream(); DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress); zipStream.Write(bytes, 0, bytes.Length); zipStream.Flush(); zipStream.Close(); return oStream.ToArray(); } public static object DecompressionObject(byte[] bytes) { if (bytes == null) return null; MemoryStream mStream = new MemoryStream(bytes); mStream.Seek(0, SeekOrigin.Begin); DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true); object dsResult = null; BinaryFormatter bFormatter = new BinaryFormatter(); dsResult = (object)bFormatter.Deserialize(unZipStream); return dsResult; } #endregion 先确保你的GZip流是否正确,如果你是使用了DecompressionObject(byte[] bytes)方法还是出现这问题,那问题就是GZip不完整。原因有很多,如果你是通过网络传输的,那就是没有接收完整或者发送方没有发送完整,最好给出相关代码。 你的复制功夫真了得,把我之前在这个帖子http://topic.csdn.net/u/20091215/16/6D341F17-7C8E-4613-9ABC-EA628CD9BA2D.html中的回复原封不动的复制过来了,一个字不差啊。 你复制之前怎么不看看,那个帖子的最后一个回复就是hong320本人,他也看到了,但是却还是没法解决他的问题,你认为你复制过来能解决问题吗? 楼主你是使用了Zip.DecompressFile()来解压标准的ZIP文件的??建议使用第三方的ICSharpZipLib来处理ZIP文件http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx Zip.DecompressFile()这种方法存在很多问题 不可以多文件压缩到一个ZIP包中吧?? /**/ /// <summary> /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹 /// </summary> /// <param name="fileName">压缩文件</param> /// <param name="dirPath">解压缩目录</param> public static void DeCompress(string fileName, string dirPath) { using (Stream source = File.OpenRead(fileName)) { using (Stream destination = new MemoryStream()) { using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true)) { byte[] bytes = new byte[4096]; int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } DeCompress(fileName, dirPath); } destination.Flush(); destination.Position = 0; DeSerializeFiles(destination, dirPath); } } } /// <summary> /// 解压 /// </summary> /// <param name="s"></param> /// <param name="dirPath"></param> private static void DeSerializeFiles(Stream s, string dirPath) { BinaryFormatter b = new BinaryFormatter(); ArrayList list = (ArrayList)b.Deserialize(s); foreach (SerializeFileInfo f in list) { string newName = dirPath + Path.GetFileName(f.FileName); using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write)) { fs.Write(f.FileBuffer, 0, f.FileBuffer.Length); fs.Close(); } } } 这样文件夹下的子文件夹解压不了怎么办,急求,谢谢各位高手 好奇怪啊,你的DeCompress方法内部怎么还有一个DeCompress,而且传递的参数不变,这不是无限死循环吗?你先把那个改下看看。 另外,从你那个报错的信息看来,可喜可贺,你还没有执行到那个无限递归的地方就出错了,不然你的内存......将int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } 改为: int offset = 0; while ((n = input.Read(bytes, offset, bytes.Length)) != 0) { destination.Write(bytes, 0, n); offset += n; } GZIP格式不对。用GZIPSTREAM类似乎不能解压rar格式和zip格式的压缩包吧。 我的怎么from放在body里就不能触发onsubmit事件 求代码优化。SQL高手来 在datagrid中显示不在数据库中的数据 如何实现二级域名的功能? 请高手指点:客户端不能正常访问的问题 日志操作 下载下来的题库管理系统怎么用呀 datagrid的合并问题 发送文本个是的邮件如何控制换行? 我点击上面一个“确认修改” 好像对下面的内容也有作用了啊 Extjs fckeditor 保存到数据库中是HTML源码,如何取出文本显示
public static byte[] CompressionObject(object DataOriginal)
{
if (DataOriginal == null) return null;
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
bFormatter.Serialize(mStream, DataOriginal);
byte[] bytes = mStream.ToArray();
MemoryStream oStream = new MemoryStream();
DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);
zipStream.Write(bytes, 0, bytes.Length);
zipStream.Flush();
zipStream.Close();
return oStream.ToArray();
} public static object DecompressionObject(byte[] bytes)
{
if (bytes == null) return null;
MemoryStream mStream = new MemoryStream(bytes);
mStream.Seek(0, SeekOrigin.Begin);
DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
object dsResult = null;
BinaryFormatter bFormatter = new BinaryFormatter();
dsResult = (object)bFormatter.Deserialize(unZipStream);
return dsResult;
}
#endregion
建议使用第三方的ICSharpZipLib来处理ZIP文件http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Zip.DecompressFile()这种方法存在很多问题 不可以多文件压缩到一个ZIP包中吧??
/**/
/// <summary>
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
/// </summary>
/// <param name="fileName">压缩文件</param>
/// <param name="dirPath">解压缩目录</param>
public static void DeCompress(string fileName, string dirPath)
{
using (Stream source = File.OpenRead(fileName))
{
using (Stream destination = new MemoryStream())
{
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
{
byte[] bytes = new byte[4096];
int n;
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
{
destination.Write(bytes, 0, n);
}
DeCompress(fileName, dirPath);
}
destination.Flush();
destination.Position = 0;
DeSerializeFiles(destination, dirPath);
}
}
}
/// <summary>
/// 解压
/// </summary>
/// <param name="s"></param>
/// <param name="dirPath"></param>
private static void DeSerializeFiles(Stream s, string dirPath)
{
BinaryFormatter b = new BinaryFormatter();
ArrayList list = (ArrayList)b.Deserialize(s);
foreach (SerializeFileInfo f in list)
{
string newName = dirPath + Path.GetFileName(f.FileName);
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
{
fs.Write(f.FileBuffer, 0, f.FileBuffer.Length);
fs.Close();
}
}
} 这样文件夹下的子文件夹解压不了怎么办,急求,谢谢各位高手
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
{
destination.Write(bytes, 0, n);
} 改为: int offset = 0;
while ((n = input.Read(bytes, offset, bytes.Length)) != 0)
{
destination.Write(bytes, 0, n);
offset += n;
}
用GZIPSTREAM类似乎不能解压rar格式和zip格式的压缩包吧。