.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 压缩方法二:将当前目录下的所有文件和文件 ...
随机推荐
- IOS应用发布NSLog的如何注释
#define IOS_DEBUG //发布时注释此行不输出log日志 #ifdef IOS_DEBUG #define NSLog(...) NSLog(__VA_ARGS__) #else #d ...
- 根据职位名,自动生成jd
代码本身就是最好的解释,不赘述. 文本聚类输出: cluster.py #!/usr/bin/env python # coding=utf-8 import jieba,re from gensim ...
- .nil? .empty? .blank? .present? in Ruby on Rails
We get confused when there are many options to choose from. Same is the case when it comes to use an ...
- DXperience-11.1.5 破解
将DXPerience_11.1.5_Crack里的所有文件粘贴到DXperience-11.1.5的bin文件夹下,然后在cmd运行register.bin
- JDBC中的批量插入和乱码解决
字符集-乱码问题 用JDBC访问MySql数据库的时候,如果JDBC使用的字符集和MySql使用的字符集不一致,那么会导致乱码发生.解决办法当时是在使用JDBC的时候指定和数据库一样的字符集.我们可以 ...
- 华硕X84L无线驱动查找
打开官网:http://www.asus.com.cn/ 点击导航栏的服务与支持 产品型号识别http://www.asus.com.cn/support/Article/565/ 我的是:X84L ...
- sqlite 时间排序
select * from tb_QuantifyResult where iSamplingOrCalibration = 1 and cComponentName <> ' + Quo ...
- 用Java开发gRPC服务的例子分析
本文的代码例子来自:https://github.com/grpc/grpc-java 定义服务 这一步与其他语言完全一样,需要定义gRPC的服务.方法.request和response的类型. 完 ...
- [SQL]SQL Server数据表的基础知识与增查删改
SQL Server数据表的基础知识与增查删改 由张晨辉(学生) 于19天 前发表 | 阅读94次 一.常用数据类型 .整型:bigint.int.smallint.tinyint .小数:decim ...
- http://www.cnblogs.com/chillsrc/category/49632.html
http://www.cnblogs.com/chillsrc/category/49632.html