用ICSharpCode.SharpZipLib的ZipInputStream解压文件报错

风中飘过一行代码 2013-10-31 03:40:59
我的解压语句如下:

FileStream fs = new FileStream(filepath, FileMode.Create);
byte[] buffer = new byte[1024];
int size = InpStream.Read(buffer, 0, buffer.Length);
while (size > 0)
{
fs.Write(buffer, 0, size);
size = InpStream.Read(buffer, 0, buffer.Length);
}

我拿断点跟,到int size...这句话时,size被赋值395,然后进入while循环,写入fs流还没有问题,但再一次读InpStream时就会报错,如下:


我想问下这是怎么回事。谢谢~
...全文
299 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
请问哪位大神知道这是怎么回事吗?是zip头中size不对吗?对方是php压缩的文件,怎么才能把zip的头size匹配正确了?
  • 打赏
  • 举报
回复
引用 8 楼 feiyun0112 的回复:
http://www.icsharpcode.net/OpenSource/SharpZipLib/DownloadLatestVersion.asp?what=BinDoc
谢谢。我也是从这下的。但是要用哪个版本的呢?我使用的是vs 2012,但程序框架是3.5的,我之前用的是net-20里面的dll 对吗?
  • 打赏
  • 举报
回复
引用 2 楼 huanggreat 的回复:
是.zip文件,你的这个方法在读压缩流和写文件流的逻辑和我的没什么区别,是一样的,你有没有最新的ICSharpCode.SharpZipLib.dll文件,有可能是我的这个dll版本比较老,有的话给我传一下吧,谢谢。我的qq 2652770247
feiyun0112 2013-10-31
  • 打赏
  • 举报
回复
http://www.icsharpcode.net/OpenSource/SharpZipLib/DownloadLatestVersion.asp?what=BinDoc
  • 打赏
  • 举报
回复
引用 6 楼 guyuekkk11 的回复:
[quote=引用 5 楼 huanggreat 的回复:] [quote=引用 4 楼 guyuekkk11 的回复:] [quote=引用 2 楼 huanggreat 的回复:]
这个我懂,就是在第二次s.read时报错。。。[/quote] 是不是当有两个 zip 压缩包的时候就出错?[/quote] 不是 在解压一个文件的时候,第一次走到read不报错,size被赋值成395,第二次read就报错了。[/quote] 按照我提供的这个方法也是一样? 你的文件是 zip 格式吗 不要只改扩展名哦
  • 打赏
  • 举报
回复
引用 5 楼 huanggreat 的回复:
[quote=引用 4 楼 guyuekkk11 的回复:] [quote=引用 2 楼 huanggreat 的回复:]
这个我懂,就是在第二次s.read时报错。。。[/quote] 是不是当有两个 zip 压缩包的时候就出错?[/quote] 不是 在解压一个文件的时候,第一次走到read不报错,size被赋值成395,第二次read就报错了。
  • 打赏
  • 举报
回复
引用 4 楼 guyuekkk11 的回复:
[quote=引用 2 楼 huanggreat 的回复:]
这个我懂,就是在第二次s.read时报错。。。[/quote] 是不是当有两个 zip 压缩包的时候就出错?
  • 打赏
  • 举报
回复
引用 2 楼 huanggreat 的回复:
这个我懂,就是在第二次s.read时报错。。。
  • 打赏
  • 举报
回复
引用 1 楼 feiyun0112 的回复:
下最新版 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx ***************************************************************************** 签名档: http://feiyun0112.cnblogs.com/
你好 这个页面哪里下最新版的 我下的是237KB那个 对吗?
  • 打赏
  • 举报
回复
    #region 解压
        /// <summary>  
        /// 功能:解压zip格式的文件。  
        /// </summary>  
        /// <param name="zipFilePath">压缩文件路径,全路径格式</param>  
        /// <param name="unZipDir">解压文件存放路径,全路径格式,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  
        /// <param name="err">出错信息</param>  
        /// <returns>解压是否成功</returns>  
        public static bool UnZip(string zipFilePath, string unZipDir, int maximum, SetProgressDelegate setProgressDelegate)
        {
            if (zipFilePath == string.Empty)
            {
                throw new System.IO.FileNotFoundException("压缩文件不不能为空!");
            }
            if (!File.Exists(zipFilePath))
            {
                throw new System.IO.FileNotFoundException("压缩文件: " + zipFilePath + " 不存在!");
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), "");
            if (!unZipDir.EndsWith("//"))
                unZipDir += "//";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);
            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("//"))
                            directoryName += "//";
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                            {
                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        setProgressDelegate(maximum, theEntry.Name);
                                        break;
                                    }
                                }
                            }
                        }
                    }//while  
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return true;
        }//解压结束 
        #endregion
feiyun0112 2013-10-31
  • 打赏
  • 举报
回复
下最新版
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

110,536

社区成员

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

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

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