SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压

1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

2.编写工具类ZipUtil,一般放在App_Code文件夹下

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
/// <summary>
/// ZipUtil 压缩解压工具
/// </summary>
public class ZipUtil
{
public ZipUtil()
{ }
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="filename"></param>
/// <param name="directory"></param>
public static void PackFiles(string filename, string directory)
{
try
{
FastZip fz = new FastZip();
fz.CreateEmptyDirectories = true;
fz.CreateZip(filename, directory, true, "");
fz = null;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="file">完整路径(包括文件名)</param>
/// <param name="dir">路径</param>
/// <returns></returns>
public static bool UnpackFiles(string file, string dir)
{ try
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir); ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{ string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty)
Directory.CreateDirectory(dir + directoryName); if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(dir + theEntry.Name); int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
} streamWriter.Close();
}
}
s.Close();
return true;
}
catch (Exception)
{
throw;
}
}
}

3.编写HTML页面

我们上传文件使用FileUpload控件(最大支持20M上传)和一个Button按钮。

 <asp:FileUpload ID="upZip" runat="server" Width="200px" />
<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="blue"></asp:Label>
<asp:Button ID="btn" runat="server" Text="上传" Width="68px" OnClick="btn_Click"/>

4.编写按钮点击事件

我们这里将本地制作好的一个专题压缩成zip文件,上传到服务器上,在对文件进行解压,并删除原来的压缩文件。(确保zip中包好一个根目录文件夹)

 protected void btn_Click(object sender, EventArgs e)
{
//获取上传文件名 demo.zip
string fileName = upZip.FileName;
if (fileName == null || fileName=="")
{
lblMsg.Text = "没有选择文件";
}
else
{
//截取专题目录名 demo
string dirName = fileName.Substring(, fileName.IndexOf('.'));
//获取上传目录 ~/zhuanti/2013/
string updir = "~/zhuanti/" + DateTime.Now.Year + "/";
//获取专题目录 ~/zhuanti/2013/demo/
string ztdir = updir + dirName +"/";
//转换为物理路径 E:\\root\\UI\\zhuanti\\2013\\demo\\
string abZtdir = Server.MapPath(ztdir);
//判断目录是否已经存在
if (Directory.Exists(abZtdir))
{//存在
lblMsg.Text = "专题目录已存在";
}
else
{//不存在
//判断压缩包类型
string lastName = fileName.Substring(fileName.LastIndexOf("."));
if (lastName.ToLower() == ".zip")
{
//上传压缩包完整路径 ~/zhuanti/2013/demo.zip
string fullpath = updir + fileName;
//物理路径 E:\\root\\UI\\zhuanti\\2013\\demo.zip
string abFullPath = Server.MapPath(fullpath);
try
{
//上传目录是否存在
if (!Directory.Exists(Server.MapPath(updir)))
{
Directory.CreateDirectory(Server.MapPath(updir));
}
//上传
this.upZip.SaveAs(Server.MapPath(fullpath));
//解压
ZipUtil.UnpackFiles(abFullPath, Server.MapPath(updir));
//删除压缩包
if (File.Exists(abFullPath))
{
File.Delete(abFullPath);
}
loadFile();
}
catch (Exception ex)
{
lblMsg.Text = "操作失败";
} }
else
{
lblMsg.Text = "只能上传ZIP文件";
}
}
} }

5.编写loadFile()方法,查看文件夹是否上传成功。这里用一个下拉列表控件显示目录下的所有文件夹

HTML代码

 <asp:ListBox ID="lbxFile" runat="server" CausesValidation="True" Rows="" SelectionMode="Multiple" Width="300px"></asp:ListBox>

CS文件代码

  //读取目录文件列表
public void loadFile()
{
      //要读取的目录物理路径
string abdir = Server.MapPath("~/zhuanti/"+DateTime.Now.Year+"/");
      //创建DirectoryInfo对象
DirectoryInfo theDir = new DirectoryInfo(abdir);
      //获取目录下所有子目录
DirectoryInfo[] thisOne = theDir.GetDirectories();
      //获取目录下所有子目录(带路径)
//string[] dirs = Directory.GetDirectories(abdir);
      //下拉框绑定数据
lbxFile.DataSource = thisOne;
lbxFile.DataBind();
}

.NET使用ICSharpCode.SharpZipLib压缩/解压文件的更多相关文章

  1. ICSharpCode.SharpZipLib压缩解压

    一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...

  2. C#使用SharpZipLib压缩解压文件

    #region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> // ...

  3. 通过SharpZipLib来压缩解压文件

    在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...

  4. SharpZipLib压缩解压

    一.介绍 SharpZipLib是一个完全由C#编写的ZIP,GZIP,Tar和BZIP2 Library,可以方便的支持这几种格式的压缩和解压缩. https://github.com/icshar ...

  5. huffman压缩解压文件【代码】

    距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...

  6. 【转载】.NET压缩/解压文件/夹组件

    转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...

  7. SharpZipLib压缩解压的使用

    项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...

  8. C#基础知识之SharpZipLib压缩解压的使用

    项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...

  9. linux压缩解压文件

    首先进入文件夹 cd /home/ftp2/1520/web 压缩方法一:压缩web下的888.com网站 zip -r 888.com.zip888.com 压缩方法二:将当前目录下的所有文件和文件 ...

随机推荐

  1. Win7家庭版开启Administrator管理员帐户的方法

    Win7家庭版开启Administrator管理员帐户的方法 发布时间:2014-11-17 18:30:06来源:系统盒浏览数:2786 很多用户安装好Win7系统第一步就是开启Administra ...

  2. Spring读取配置文件

    在spring中可以通过下面的方式将配置文件中的项注入到配置中 <bean class="org.springframework.beans.factory.config.Proper ...

  3. 编译 proto 文件到指定语言的代码

    由于 Protocol Buffers 3 的正式版还没有发布,在官网(https://developers.google.com/protocol-buffers/docs/downloads)目前 ...

  4. sql server 作业导出放到另外一台机器执行时报错的解决方法

    SQL Server2008脚本创建作业失败,提示: 引用内容消息 515,级别 16,状态 2,过程 sp_add_job,第 137 行不能将值 NULL 插入列 'owner_sid',表 'm ...

  5. [SQL]多列的行转列

    create table t(name varchar(),subject varchar(),mark int) insert into t union all union all union al ...

  6. 合并 hdfs 文件

     待研究,只做保存 将HDFS中不同目录下面的数据合在一起,并存放在指定的目录中,示例如: sqoop merge –new-data /test/p1/person –onto /test/p2 ...

  7. MyQL修改用户名命令、密码

    ====================================================== update user set host = '%' where user = 'root ...

  8. html+css源码之实现登录弹出框遮罩层效果

    在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...

  9. 动手学servlet(一) 第一个servlet程序

    1.文件>新建>动态WEB项目 "javaeedemo">在Java Resource的src下新建包“servletdemo”,包下新建一个类“MyServet ...

  10. OC基础(12)

    new方法实现原理 类的本质 类的启动过程 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bo ...