我用SharpZipLib作的压缩包,在解压缩就会出现如下错误 size mismatch: 4294967295;3218 <-> 992;3218 的错误我跟踪调试发现inf.TotalIn 与 csize 不相等 我的情况是压缩多个文件,其中有部分文件是中文名,这种情况下,我解压缩就会出现如上错误如果我用RAR软件直接压缩成ZIP,在解压就没有任何错误? 请高手指点迷津 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class ZipDAO { public string file; public ZipDAO(string dir) { Console.WriteLine("开始压缩....."); file = dir; } public void ZipFold() { //获得压缩的文件夹 //压缩文件夹的名字 string name = file + ".rar"; //压缩文件的流对象 // MessageBox.Show(name); ZipOutputStream output = new ZipOutputStream(File.Create(name)); output.SetLevel(6); string[] dir = Directory.GetFiles(file); //存放文件数据 Crc32 crc = new Crc32(); foreach (string myFile in dir) { FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read); byte[] bt = new byte[fs.Length]; fs.Read(bt, 0, bt.Length); //存储要压缩的文件 ZipEntry entry = new ZipEntry(myFile.Substring(myFile.Length - 41, 41)); entry.Size = fs.Length; entry.DateTime = DateTime.Now; fs.Close(); crc.Reset(); //清除crc内容 crc.Update(bt); //更新文件内容到crc中 entry.Crc = crc.Value; //将文件内容放到压缩文件中 output.PutNextEntry(entry); //将数据写入压缩流中 output.Write(bt, 0, bt.Length); } output.Close(); Console.WriteLine("压缩成功!"); } } 你这个压缩方法跟我的一样啊用你这个压缩完了,在解压还是报错,我把我的压缩和解压代码公布一下压缩就用你这个 public void ZipFold() { //获得压缩的文件夹 //压缩文件夹的名字 string name = file; //压缩文件的流对象 // MessageBox.Show(name); ZipOutputStream output = new ZipOutputStream(File.Create(name)); output.SetLevel(6); string[] dir = Directory.GetFiles(folder); //存放文件数据 Crc32 crc = new Crc32(); foreach (string myFile in dir) { FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read); byte[] bt = new byte[fs.Length]; fs.Read(bt, 0, bt.Length); //存储要压缩的文件 ZipEntry entry = new ZipEntry(myFile.Substring(myFile.LastIndexOf('\\')+1, myFile.Length - (myFile.LastIndexOf('\\')+1))); entry.Size = fs.Length; entry.DateTime = DateTime.Now; fs.Close(); crc.Reset(); //清除crc内容 crc.Update(bt); //更新文件内容到crc中 entry.Crc = crc.Value; //将文件内容放到压缩文件中 output.PutNextEntry(entry); //将数据写入压缩流中 output.Write(bt, 0, bt.Length); } output.Close(); Console.WriteLine("压缩成功!"); } 解压缩public static void UnZip(string[] args) { FileStream sss = File.OpenRead(args[0]); ZipInputStream s = new ZipInputStream(sss); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(args[1]); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(args[1] + fileName); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, size); if (size > 0) { streamWriter.Write(data, 0, size); } //else if (size > 0 && size < 2048) //{ // streamWriter.Write(data, 0, size); // break; //} else { break; } } streamWriter.Close(); } } s.Close(); }谁帮我看看是压缩的问题,还是解压缩有问题 压缩和解压的代码都差不多 在解压时,解压完一个文件,再到下一个文件时while ((theEntry = s.GetNextEntry()) != null) 就出异常了 可能解压的文件有SIZE=0KB,在程序里面控制一下就可以了 没有0KB的文件... 我跟楼主的情况一样..自己压缩的包 自己解压就出问题,用RAR软件压缩的包,解压就没问题. 我以为只有我遇到了这个问题,那好我把解决办法公布下打开SharpZipLib源代码,你找到ZIP文件夹下的,ZipInputStream.cs文件然后找到这段if ((flags & 8) == 0 && (inf.TotalIn != csize || inf.TotalOut != size)) { throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);把如上代码注释掉,然后编译,在从新把DLL引入到项目中替换以前的就可以了 } 怎么设计:方法根据参数决定返回一个对象还是多个对象 Excel数据导入问题?? 请教ADO.NET中SqlParameter类的用法 怎样在C#运用存储过程 HyperLink 控件怎么实现往两个页传值 如何初始化NewPostList[0]?谢谢各位 关于图片路径的问题!急呀!在线等 正则表达式5到8个字母或数字,字母和数据都不能重复 tcp调试技巧 为什么插入数据库的中文变成了乱码? Enterprise Library 3.1有人用没 ------一个简单问题,vs2005里面怎么做安装文件啊???
{
public string file;
public ZipDAO(string dir)
{
Console.WriteLine("开始压缩.....");
file = dir;
} public void ZipFold()
{
//获得压缩的文件夹
//压缩文件夹的名字
string name = file + ".rar";
//压缩文件的流对象
// MessageBox.Show(name);
ZipOutputStream output = new ZipOutputStream(File.Create(name));
output.SetLevel(6);
string[] dir = Directory.GetFiles(file);
//存放文件数据
Crc32 crc = new Crc32();
foreach (string myFile in dir)
{
FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
//存储要压缩的文件
ZipEntry entry = new ZipEntry(myFile.Substring(myFile.Length - 41, 41));
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
fs.Close();
crc.Reset(); //清除crc内容
crc.Update(bt); //更新文件内容到crc中
entry.Crc = crc.Value; //将文件内容放到压缩文件中
output.PutNextEntry(entry);
//将数据写入压缩流中
output.Write(bt, 0, bt.Length);
}
output.Close();
Console.WriteLine("压缩成功!");
}
}
用你这个压缩完了,在解压还是报错,我把我的压缩和解压代码公布一下
压缩就用你这个
public void ZipFold()
{
//获得压缩的文件夹
//压缩文件夹的名字
string name = file;
//压缩文件的流对象
// MessageBox.Show(name);
ZipOutputStream output = new ZipOutputStream(File.Create(name));
output.SetLevel(6);
string[] dir = Directory.GetFiles(folder);
//存放文件数据
Crc32 crc = new Crc32();
foreach (string myFile in dir)
{
FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
//存储要压缩的文件
ZipEntry entry = new ZipEntry(myFile.Substring(myFile.LastIndexOf('\\')+1, myFile.Length - (myFile.LastIndexOf('\\')+1)));
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
fs.Close();
crc.Reset(); //清除crc内容
crc.Update(bt); //更新文件内容到crc中
entry.Crc = crc.Value; //将文件内容放到压缩文件中
output.PutNextEntry(entry);
//将数据写入压缩流中
output.Write(bt, 0, bt.Length);
}
output.Close();
Console.WriteLine("压缩成功!");
} 解压缩
public static void UnZip(string[] args)
{
FileStream sss = File.OpenRead(args[0]);
ZipInputStream s = new ZipInputStream(sss); ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name); //生成解压目录
Directory.CreateDirectory(directoryName); if (fileName != String.Empty)
{
//解压文件到指定的目录
FileStream streamWriter = File.Create(args[1] + fileName); int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, size);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
//else if (size > 0 && size < 2048)
//{
// streamWriter.Write(data, 0, size);
// break;
//}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}谁帮我看看是压缩的问题,还是解压缩有问题
就出异常了
打开SharpZipLib源代码,你找到ZIP文件夹下的,ZipInputStream.cs文件
然后找到这段
if ((flags & 8) == 0 && (inf.TotalIn != csize || inf.TotalOut != size)) {
throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
把如上代码注释掉,然后编译,在从新把DLL引入到项目中替换以前的就可以了
}