在解压文件时,GZipStream 中 报一个“GZip 头中的幻数不正确。请确保正在传入 GZip 流。” 这样的错误
  改怎么解决????

解决方案 »

  1.   

    显然你的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
      

  2.   

    先确保你的GZip流是否正确,如果你是使用了DecompressionObject(byte[] bytes)方法还是出现这问题,那问题就是GZip不完整。原因有很多,如果你是通过网络传输的,那就是没有接收完整或者发送方没有发送完整,最好给出相关代码。
      

  3.   

    你的复制功夫真了得,把我之前在这个帖子http://topic.csdn.net/u/20091215/16/6D341F17-7C8E-4613-9ABC-EA628CD9BA2D.html中的回复原封不动的复制过来了,一个字不差啊。
      

  4.   

    你复制之前怎么不看看,那个帖子的最后一个回复就是hong320本人,他也看到了,但是却还是没法解决他的问题,你认为你复制过来能解决问题吗?
      

  5.   

    楼主你是使用了Zip.DecompressFile()来解压标准的ZIP文件的??
    建议使用第三方的ICSharpZipLib来处理ZIP文件http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
     Zip.DecompressFile()这种方法存在很多问题 不可以多文件压缩到一个ZIP包中吧??
      

  6.   


      /**/
            /// <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();
                    }
                }
            }  这样文件夹下的子文件夹解压不了怎么办,急求,谢谢各位高手
      

  7.   

    好奇怪啊,你的DeCompress方法内部怎么还有一个DeCompress,而且传递的参数不变,这不是无限死循环吗?你先把那个改下看看。
      

  8.   

    另外,从你那个报错的信息看来,可喜可贺,你还没有执行到那个无限递归的地方就出错了,不然你的内存......将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;
            }
      

  9.   

    GZIP格式不对。
    用GZIPSTREAM类似乎不能解压rar格式和zip格式的压缩包吧。