首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> C#/CSHARP教程
在C#中利用SharpZipLib进行文件的压缩和解压缩-.NET教程,C#语言
作者:网友供稿 点击:14
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手。只好耐下心来,慢慢的研究,总算找到了门路。针对自己的需要改写了文件压缩和解压缩的两个类,分别为zipclass和unzipclass。其中碰到了不少困难,就决定写出来压缩和解压的程序后,一定把源码贴出来共享,让首次接触压缩和解压缩的朋友可以少走些弯路。下面就来解释如何在c#里用http://www.icsharpcode.net下载的sharpziplib进行文件的压缩和解压缩。

首先需要在项目里引用sharpziplib.dll。然后修改其中的关于压缩和解压缩的类。实现源码如下:

/// <summary>
/// 压缩文件
/// </summary>

using system;
using system.io;

using icsharpcode.sharpziplib.checksums;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.gzip;

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 < streamtozip.length)
{
int sizeread =streamtozip.read(buffer,0,buffer.length);
zipstream.write(buffer,0,sizeread);
size += sizeread;
}
}
catch(system.exception ex)
{
throw ex;
}
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.now;

// 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 dont store
// the size and crc in the header.
entry.size = fs.length;
fs.close();

crc.reset();
crc.update(buffer);

entry.crc = crc.value;

s.putnextentry(entry);

s.write(buffer, 0, buffer.length);

}

s.finish();
s.close();
}
}
}

现在再来看看解压文件类的源码

/// <summary>
/// 解压文件
/// </summary>

using system;
using system.text;
using system.collections;
using system.io;
using system.diagnostics;
using system.runtime.serialization.formatters.binary;
using system.data;

using icsharpcode.sharpziplib.bzip2;
using icsharpcode.sharpziplib.zip;
using icsharpcode.sharpziplib.zip.compression;
using icsharpcode.sharpziplib.zip.compression.streams;
using icsharpcode.sharpziplib.gzip;

namespace decompression
{
public class unzipclass
{
public void unzip(string[] args)
{
zipinputstream s = new zipinputstream(file.openread(args[0]));

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]+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
{
break;
}
}

streamwriter.close();
}
}
s.close();
}
}
}

有了压缩和解压缩的类以后,就要在窗体里调用了。怎么?是新手,不会调用?ok,接着往下看如何在窗体里调用。

首先在窗体里放置两个命令按钮(不要告诉我你不会放啊~),然后编写以下源码

/// <summary>
/// 调用源码
/// </summary>

private void button2_click_1(object sender, system.eventargs e)
{
string []fileproperties=new string[2];
fileproperties[0]="c:\\unzipped\\";//待压缩文件目录
fileproperties[1]="c:\\zip\\a.zip"; //压缩后的目标文件
zipclass zc=new zipclass();
zc.zipfilemain(fileproperties);
}

private void button2_click(object sender, system.eventargs e)
{
string []fileproperties=new string[2];
fileproperties[0]="c:\\zip\\test.zip";//待解压的文件
fileproperties[1]="c:\\unzipped\\";//解压后放置的目标目录
unzipclass unzc=new unzipclass();
unzc.unzip(fileproperties);
}

好了,到此为止,如何压缩和解压缩的类都已经完成了,需要的朋友直接拿走调吧。

文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·使用C#编写Windows Forms应用程序(转)-.NET教程,Windows开发
·C#学习杂记-.NET教程,C#语言
·将文本格式的文章转换为html/xml格式文本的功能封装到Javabean-JSP教程,Java技巧及代码
·用java小程序applet实现无限级树结构-JSP教程,Java技巧及代码
·《.net编程先锋C#》第一章 C#简介-.NET教程,C#语言
·企业人事信息管理系统1.0-ASP教程,数据库相关
·《.net编程先锋C#》第三章 第一个C#应用程序-.NET教程,C#语言
·《.net编程先锋C#》第二章 理论基础-公用语言 运行环境-.NET教程,C#语言
·Windows应用程序调试必备的--符号文件(Symbols)-.NET教程,评论及其它
·c#反编译微软msdn2003的帮助文档,并将反编译结果保存到一个sqlserver数据库中
最新文章
·vista的新特性:懒人的安全感_windows vista
·photoshop为情侣照片打造韩式梦幻相框_photoshop教程
·专访seobbs站长乐思蜀_站长访谈
·嘟嘟网络陈艺光:诚信是一种信仰_站长访谈
·俺自己黄修源:只问耕耘,不问收获_站长访谈
·李向华:如何提高论坛用户粘性_站长访谈
·给windows vista系统网络和共享中心“换脸”_windows vista
·胡宪东谈搜索优化的实战_站长心得
·网站快速成功的九个步骤_站长心得
·google pr数字背后的含意_google推广
相关主题
  • 在c#中利用keep-alive处理socket网络异常断开的方法_c#应用
  • 在C#中利用SharpZipLib进行文件的压缩和解压缩-.NET教程,C#语言
  • 在c#中利用sharpziplib进行文件的压缩和解压缩
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭