未检测到CSharpCode.SharpZipLib.dll文件找不到入口点,请确保文件和软件在同一目录下!

中国领先的IT技术网站
51CTO旗下网站
C#使用SharpZipLib分析
这里介绍C#使用SharpZipLib进行文件的压缩和解压缩,其中碰到了不少困难,就决定写出来压缩和解压的程序后,一定把源码贴出来共享。
作者:佚名来源:cnbeta| 09:16
学习C#语言时,经常会遇到文件的压缩和解压缩问题,这里将介绍C#使用SharpZipLib进行文件的压缩和解压缩。
C#使用SharpZipLib进行文件的压缩和解压缩
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手。只好耐下心来,慢慢的研究,总算找到了门路。针对自己的需要改写了文件压缩和解压缩的两个类,分别为 ZipClass和UnZipClass。其中碰到了不少困难,就决定写出来压缩和解压的程序后,一定把源码贴出来共享,让首次接触压缩和解压缩的朋友可以少走些弯路。下面就来解释如何在C#使用SharpZipLib进行文件的压缩和解压缩。
首先需要在项目里引用sharpziplib.dll。然后修改其中的关于压缩和解压缩的类。实现源码如下: ///&&///&压缩文件 &///&&&using&S &using&System.IO; &&using&ICSharpCode.SharpZipLib.C &using&ICSharpCode.SharpZipLib.Z &using&ICSharpCode.SharpZipLib.GZ &&namespace&Compression &{ &public&class&ZipClass &{ &&public&void&ZipFile(string&FileToZip,&string&ZipedFile&,int&CompressionLevel,&int&BlockSize) &{ &//如果文件没有找到,则报错 &if&(!&System.IO.File.Exists(FileToZip)) &{ &throw&new&System.IO.FileNotFoundException("The&specified&file&"&+&FileToZip&+&"&could&not&be&found.&Zipping&aborderd"); &} &&system.io.filestream&StreamToZip&=&new&System.IO.FileStream(FileToZip,System.IO.FileMode.Open&,&System.IO.FileAccess.Read); &System.IO.FileStream&ZipFile&=&System.IO.File.Create(ZipedFile); &ZipOutputStream&ZipStream&=&new&ZipOutputStream(ZipFile); &ZipEntry&ZipEntry&=&new&ZipEntry("ZippedFile"); &ZipStream.PutNextEntry(ZipEntry); &ZipStream.SetLevel(CompressionLevel); &byte[]&buffer&=&new&byte[BlockSize]; &System.Int32&size&=StreamToZip.Read(buffer,0,buffer.Length); &ZipStream.Write(buffer,0,size); &try &{ &while&(size&&) &{ &int&sizeRead&=StreamToZip.Read(buffer,0,buffer.Length); &ZipStream.Write(buffer,0,sizeRead); &size&+=&sizeR &} &} &catch(System.Exception&ex) &{ &throw& &} &ZipStream.Finish(); &ZipStream.Close(); &StreamToZip.Close(); &} &&public&void&ZipFileMain(string[]&args) &{ &string[]&filenames&=&Directory.GetFiles(args[0]); &&crc32&crc&=&new&Crc32(); &ZipOutputStream&s&=&new&ZipOutputStream(File.Create(args[1])); &&s.setlevel(6);&//&0&-&store&only&to&9&-&means&best&compression &&foreach&(string&file&in&filenames) &{ &//打开压缩文件 &FileStream&fs&=&File.OpenRead(file); &&byte[]&buffer&=&new&byte[fs.Length]; &fs.Read(buffer,&0,&buffer.Length); &ZipEntry&entry&=&new&ZipEntry(file); &&entry.datetime&=&DateTime.N &&//&set&Size&and&the&crc,&because&the&information &//&about&the&size&and&crc&should&be&stored&in&the&header &//&if&it&is&not&set&it&is&automatically&written&in&the&footer. &//&(in&this&case&size&==&crc&==&-1&in&the&header) &//&Some&ZIP&programs&have&problems&with&zip&files&that&don't&store &//&the&size&and&crc&in&the&header. &entry.Size&=&fs.L &fs.Close(); &&crc.reset(); &crc.Update(buffer); &&entry.crc&=&crc.V &&s.putnextentry(entry); &&s.write(buffer,&0,&buffer.Length); &&} &&s.finish(); &s.Close(); &} &} &} &&现在再来看看解压文件类的源码 &&///&&///&解压文件 &///&&&using&S &using&System.T &using&System.C &using&System.IO; &using&System.D &using&System.Runtime.Serialization.Formatters.B &using&System.D &&using&ICSharpCode.SharpZipLib.BZip2; &using&ICSharpCode.SharpZipLib.Z &using&ICSharpCode. &using&ICSharpCode.pression.S &using&ICSharpCode.SharpZipLib.GZ &&namespace&DeCompression &{ &public&class&UnZipClass &{ &public&void&UnZip(string[]&args) &{ &ZipInputStream&s&=&new&ZipInputStream(File.OpenRead(args[0])); &&zipentry&theE &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]+theEntry.Name); &&int&size&=&2048; &byte[]&data&=&new&byte[2048]; &while&(true) &{ &ssize&=&s.Read(data,&0,&data.Length); &if&(size&&0) &{ &streamWriter.Write(data,&0,&size); &} &else &{ & &} &} &&streamwriter.close(); &} &} &s.Close(); &} &} &}&&
【编辑推荐】
【责任编辑: TEL:(010)】
大家都在看猜你喜欢
热点原创热点头条头条
24H热文一周话题本月最赞
讲师:218802人学习过
讲师:146174人学习过
讲师:13039人学习过
精选博文论坛热帖下载排行
本书是目前所能找到的最实用、最全面的Linux指南和参考手册,也是唯一一本提供以下全部内容的书籍:
更好更实用的示例覆盖了实际工作中需...
订阅51CTO邮刊C# 下利用ICSharpCode.SharpZipLib.dll实现文件文件夹压缩、解压缩_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C# 下利用ICSharpCode.SharpZipLib.dll实现文件文件夹压缩、解压缩
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩6页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢当前位置: →
→ 使用ICSharpCode.SharpZipLib.dll遇到的有关问题
使用ICSharpCode.SharpZipLib.dll遇到的有关问题
& 作者:佚名 & 来源: 互联网 & 热度:
&收藏到→_→:
摘要: 使用ICSharpCode.SharpZipLib.dll遇到的问题想用C#写一个压缩文件夹的功能,对文件夹进行压缩。找了一段代码试了一下。能生成z...
"使用ICSharpCode.SharpZipLib.dll遇到的有关问题"::
使用icsharpcode.sharpziplib.dll遇到的问题想用c#写一个压缩文件夹的功能,对文件夹进行压缩。找了一段代码试了一下。能生成zip文件,但是用winrar解压的时候出现错误:crc失败,文件被破坏。放到linux上用unzip命令也打不开。请用过icsharpcode.sharpziplib.dll的朋友帮忙看看呢?或发段有用的代码学习一下。谢谢c# code
public void zipfile(string strfile, string strzip)
if (strfile[strfile.length - 1] != path.directoryseparatorchar)
strfile += path.di
zipoutputstream s = new zipoutputstream(file.create(strzip));
s.setlevel(6); // 0 - store only to 9 - means best compression
zip(strfile, s, strfile);
s.finish();
s.close();
private void zip(string strfile, zipoutputstream s, string staticfile)
if (strfile[strfile.length - 1] != path.directoryseparatorchar) strfile += path.di
crc32 crc = new crc32();
string[] filenames = directory.getfilesystementries(strfile);
foreach (string file in filenames)
if (directory.exists(file))
zip(file, s, staticfile);
else // 否则直接压缩文件
//打开压缩文件
filestream fs = file.openread(file);
byte[] buffer = new byte[fs.length];
fs.read(buffer, 0, buffer.length);
string tempfile = file.substring(staticfile.lastindexof(&\\&) + 1);
zipentry entry = new zipentry(tempfile);
entry.datetime = datetime.
entry.size = fs.
fs.close();
crc.reset();
crc.update(buffer);
entry.crc = crc.
s.putnextentry(entry);
s.write(buffer, 0, buffer.length);
private void button1_click(object sender, eventargs e)
string[] fileproperties = new string[2];
fileproperties[0] = &f:\\a\\&;//待压缩文件目录
fileproperties[1] = &f:\\a.zip&;
//压缩后的目标文件
zipfile(fileproperties[0], fileproperties[1]);
------解决方案--------------------压缩文件内的 路径分隔符 你用的不对吧, '\' 还是 '/' 倒腾着试试。
------解决方案--------------------
zipentry 有这个函数, 注释中有说这个事情,/// &summary&
/// cleans a name making it conform to zip file conventions.
/// devices names ('c:\') and unc share names ('\\server\share') are removed
/// and forward slashes ('\') are converted to back slashes ('/').
/// &/summary&
/// &param name=&name&&name to clean&/param&
/// &param name=&relativepath&&make names relative if true or absolute if false&/param&
static public string cleanname(string name, bool relativepath) 搜索此文相关文章:此文来自: 马开东博客
网址: 站长QQ
上一篇:没有了
使用ICSharpCode.SharpZipLib.dll遇到的有关问题_C#技术相关文章
C#技术_总排行榜
C#技术_最新
C#技术_月排行榜
C#技术_周排行榜
C#技术_日排行榜所有回答(3)
你看下你的bin下有这个名字的dll吗?如果没有,就拷贝一份到bin中
园豆:9726
园豆:9726
园豆:9726
同上,建议在添加三方控件的时候 不要放在桌面上,放到bin目录下 然后找到bin目录下的dll进行添加,这样换台机器换个环境就不会找不到dll了,,
建议别有中文目录引用。第二就是确保文件都存在。第三个错误确保引用文件的版本信息都对 就可以了。
园豆:14063
&&&您需要以后才能回答,未注册用户请先。

我要回帖

更多关于 找不到文件mgls.dll 的文章

 

随机推荐