.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 压缩方法二:将当前目录下的所有文件和文件 ...
随机推荐
- java网络编程之TCP实例
Dgram类 package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgr ...
- Centos下搭建ftp服务器
完全不用那么麻烦,直接可以用xshell中自带的传输文件功能,下载客户端xftp安装就行,不用配置,可以在windows系统向Linux系统的任何文件地方上传下载文件,简单方便,大大节约时间, vsf ...
- [ActionScript 3.0] AS3.0 Loader加载子swf时是否需要指定新的应用程序域ApplicationDomain
实际应用中, Loader加载子swf时是否需要指定新的应用程序域ApplicationDomain,需要择情况而定. 1.如果在本地将项目位置添加到flashplayer受信任位置(上一篇文章所述) ...
- HOST ip is not allowed to connect to this MySql server
报错:1130-host ... is not allowed to connect to this MySql server 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...
- InnoSetup打包exe安装应用程序,并添加卸载图标 转
http://blog.csdn.net/guoquanyou/article/details/7445773 InnoSetup真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...
- C# Winform常见的Editor
常见Editor: 1)ArrayEditor,继承自CollectionEditor 2)BinaryEditor 3)CollectionEditor 4)DateTimeEditor 5)Mul ...
- (medium)LeetCode 240.Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Android--输入自动提示AutoCompleteTextView
布局文件: <TextView android:id="@+id/title" android:layout_width="wrap_content" a ...
- OC基础笔记目录
OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...
- MFC学习 序列化
void CArchiveView::OnWrite() { // Archive就是可序列化的类, 要头文件中DECLARE_DYNCREATE(CArchiveDoc) // 重写 virtual ...