.NET使用ICSharpCode.SharpZipLib压缩/解压文件
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压缩/解压文件的更多相关文章
- ICSharpCode.SharpZipLib压缩解压
一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...
- C#使用SharpZipLib压缩解压文件
#region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> // ...
- 通过SharpZipLib来压缩解压文件
在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更 ...
- SharpZipLib压缩解压
一.介绍 SharpZipLib是一个完全由C#编写的ZIP,GZIP,Tar和BZIP2 Library,可以方便的支持这几种格式的压缩和解压缩. https://github.com/icshar ...
- huffman压缩解压文件【代码】
距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...
- 【转载】.NET压缩/解压文件/夹组件
转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...
- SharpZipLib压缩解压的使用
项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...
- C#基础知识之SharpZipLib压缩解压的使用
项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...
- linux压缩解压文件
首先进入文件夹 cd /home/ftp2/1520/web 压缩方法一:压缩web下的888.com网站 zip -r 888.com.zip888.com 压缩方法二:将当前目录下的所有文件和文件 ...
随机推荐
- [转]Vi/Vim查找替换使用方法
vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记录几种在此,方便以后查询. :s/vivian/sky/ 替换当前行第一个 vivian ...
- Linux 链接(转载)
来源:http://www.cnblogs.com/sonic4x/archive/2011/08/05/2128543.html 1.Linux链接概念Linux链接分两种,一种被称为硬链接(Har ...
- "undefined reference to" 问题解决方法 -链接问题
最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...
- C语言中access、_mkdir、sprintf、 fopen、fwrite函数
int access(const char *filename, int amode); amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-. 这个函数还可以检查其它文件属 ...
- [Flex] ButtonBar系列——flex3 labelFunction用户提供的函数,在每个项目上运行以确定其标签
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- 快速同步GitHub代码库
因伟大的墙的存在,github下载速度奇慢, 简单办法,在csdn code建一个账号,然后创建工程的时候选择导入模式, 填入github的项目git URL. 然后.. 从csdn的code下载就快 ...
- nyoj 73 比大小
点击打开链接 比大小 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于 ...
- 那些年,我们开发的接口之:QQ登录(OAuth2.0)
那些年,我们开发的接口之:QQ登录(OAuth2.0) 吴剑 2013-06-14 原创文章,转载必须注明出处:http://www.cnblogs.com/wu-jian 前言 开发这些年,做过很多 ...
- Android设置透明、半透明等效果
设置透明效果 大概有三种 1.用android系统的透明效果Java代码 android:background="@android:color/transparent" 例如 设 ...
- [系统] 安装Ubuntu 双系统 - 失败
因为工作原因, 所以需要装ubuntu系统. 在网络上查了一下, 一般都是使用U盘安装. 但是由于手头上既没有U盘又没有光盘,只能用硬盘安装了. 查一下, 使用wubi安装方式从硬盘安装, 非常方便. ...