超级哥哥,丁丁特价,端午节是几号
本文实例讲述了asp.net c#实现解压缩文件的方法。一共给大家介绍了三段代码,一个是简单的解压缩单个zip文件,后一个可以解压批量的大量的但需要调用icsharpcode.sharpziplib.dll类了,最后一个比较实例可压缩也可以解压缩了分享给大家供大家参考。具体如下:
解压缩单个文件:
批量解压缩(这需要调用一个解压缩类库。。 icsharpcode.sharpziplib.dll)
下载sharpziplib.dll,在http://www.icsharpcode.net/opensource/sharpziplib/download.aspx中有最新免费版本,“assemblies for .net 1.1, .net 2.0, .net cf 1.0, .net cf 2.0: download [297 kb] ”点击download可以下载,解压后里边有好多文件夹,因为不同的版本,我用的fw2.0。
或者点击此处。
引用sharpziplib.dll,在项目中点击项目右键-->添加引用-->浏览,找到要添加的dll-->确认
改写了文件压缩和解压缩的两个类,新建两个类名字为zipfloclass.cs,unzipfloclass.cs
源码如下
private void zip(string strfile, zipoutputstream s, string staticfile)
{
if (strfile[strfile.length - 1] != path.directoryseparatorchar) strfile += path.directoryseparatorchar;
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.now;
entry.size = fs.length;
fs.close();
crc.reset();
crc.update(buffer);
entry.crc = crc.value;
s.putnextentry(entry);
s.write(buffer, 0, buffer.length);
}
}
}
}
/// <summary>
/// unzipfloclass 的摘要说明
/// </summary>
public class unzipfloclass
{
public string unzipfile(string targetfile, string filedir)
{
string rootfile = " ";
try
{
//读取压缩文件(zip文件),准备解压缩
zipinputstream s = new zipinputstream(file.openread(targetfile.trim()));
zipentry theentry;
string path = filedir;
//解压出来的文件保存的路径
string rootdir = " ";
//根目录下的第一个子文件夹的名称
while ((theentry = s.getnextentry()) != null)
{
rootdir = path.getdirectoryname(theentry.name);
//得到根目录下的第一级子文件夹的名称
if (rootdir.indexof("\") >= 0)
{
rootdir = rootdir.substring(0, rootdir.indexof("\") + 1);
}
string dir = path.getdirectoryname(theentry.name);
//根目录下的第一级子文件夹的下的文件夹的名称
string filename = path.getfilename(theentry.name);
//根目录下的文件名称
if (dir != " " )
//创建根目录下的子文件夹,不限制级别
{
if (!directory.exists(filedir + "\" + dir))
{
path = filedir + "\" + dir;
//在指定的路径创建文件夹
directory.createdirectory(path);
}
}
else if (dir == " " && filename != "")
//根目录下的文件
{
path = filedir;
rootfile = filename;
}
else if (dir != " " && filename != "")
//根目录下的第一级子文件夹下的文件
{
if (dir.indexof("\") > 0)
//指定文件保存的路径
{
path = filedir + "\" + dir;
}
}
if (dir == rootdir)
//判断是不是需要保存在根目录下的文件
{
path = filedir + "\" + rootdir;
}
//以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (filename != string.empty)
{
filestream streamwriter = file.create(path + "\" + filename);
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();
return rootfile;
}
catch (exception ex)
{
return "1; " + ex.message;
}
}
}
希望本文所述对大家的asp.net程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论