压缩解压目录和文件--ICSharpCode.SharpZipLib.dll 算法 (转)

ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录

 

//压缩

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using System.Collections;
namespace ZipSharpLibray.Common.Control
{
///
/// Class1 的摘要说明。
///
public class FileZipCreate
{
private static FileZipCreate filezipcreate=null;
private string zipfilecreatename;
private string filesdirectorypath;
private int dirnamelength = 0;
private int ziplevel = 6;
private FileZipCreate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// 压缩后的文件的全名称
///
public string ZipFileCreateName
{
set
{
this.zipfilecreatename=value;
}
get
{
return this.zipfilecreatename;
}
}
///
/// 待压缩文件目录
///
public string FileDirectoryPath
{
set
{
this.filesdirectorypath=value;
}
get
{
return this.filesdirectorypath;
}
}
public int ZipLevel
{
set
{
this.ziplevel=value;
}
get
{
return this.ziplevel;
}
}
public static FileZipCreate ZipFileInstance()
{

if(filezipcreate==null)
{
filezipcreate=new FileZipCreate();
}
return filezipcreate;
}
///
/// 压缩文件的方法
///
public void ZipFileCreate()
{
ZipOutputStream zipoutputstream= new ZipOutputStream(File.Create(this.zipfilecreatename));
zipoutputstream.SetLevel(this.ziplevel);
Crc32 crc = new Crc32();
Hashtable fileList=this.getAllFies();
foreach (DictionaryEntry item in fileList)
{
FileStream fs = File.OpenRead(item.Key.ToString());
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(this.filesdirectorypath.Length-this.dirnamelength));
entry.DateTime = (DateTime)item.Value;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
zipoutputstream.Finish();
zipoutputstream.Close();
}
///
/// 获取所有文件
///
///
private Hashtable getAllFies()
{
Hashtable FilesList = new Hashtable();
DirectoryInfo fileDire = new DirectoryInfo(this.filesdirectorypath);
if(!fileDire.Exists)
{
throw new System.IO.FileNotFoundException("目录:"+ fileDire.FullName + "没有找到!");
}
this.dirnamelength=fileDire.Name.Length;
this.getAllDirFiles(fileDire,FilesList);
this.getAllDirsFiles(fileDire.GetDirectories(),FilesList);
return FilesList;
}
///
/// 获取一个文件夹下的所有文件夹里的文件
///
/// /// private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)
{
foreach (DirectoryInfo dir in dirs)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName,file.LastWriteTime);
}
this.getAllDirsFiles(dir.GetDirectories(),filesList);
}
}
///
/// 获取一个文件夹下的文件
///
/// 目录名称 /// 文件列表HastTable private void getAllDirFiles(DirectoryInfo dir,Hashtable filesList)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
}
}
}


//解压

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace ZipSharpLibray.Common.Control
{
///
/// UZipFilesCreate 的摘要说明。
///
public class UZipFilesCreate
{
private string zipfilename;
private string filescreatepath;
private static UZipFilesCreate uzipfilescreate=null;
private UZipFilesCreate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// Zip文件目录
///
public string ZipFileName
{
set
{
this.zipfilename=value;
}
get
{
return this.zipfilename;
}
}
///
/// 解压文件目录
///
public string filesCreatePath
{
set
{
this.filescreatepath=value;
}
get
{
return this.filescreatepath;
}
}
public static UZipFilesCreate UZipFilesInstance()
{
if(uzipfilescreate==null)
{
uzipfilescreate=new UZipFilesCreate();
}
return uzipfilescreate;
}
public void UZipCreateFiles()
{
ZipEntry entry;
ZipInputStream zipinputstream = new ZipInputStream(File.OpenRead(this.zipfilename));
while ((entry = zipinputstream.GetNextEntry()) != null)
{
this.CreateDirList(entry.Name);
string strPath=this.filescreatepath+"//"+entry.Name;
FileStream streamWriter =File.Create(strPath);
byte[] data = new byte[entry.Size];
zipinputstream.Read(data, 0, data.Length);
streamWriter.Write(data, 0, data.Length);
streamWriter.Close();
File.SetLastWriteTime(strPath,entry.DateTime);
}
zipinputstream.Close();
}
private void CreateDirList(string filename)
{
string dirName=this.filescreatepath;
string[] dirlevelname=filename.Split('//');
for(int i=0;i {
dirName+="//"+dirlevelname[i];
if(Directory.Exists(dirName))
{
continue;
}
Directory.CreateDirectory(dirName);
}
}
}
}

//调用

using ZipSharpLibray.Common.Control;

......

private void Button1_Click(object sender, System.EventArgs e)
{

//压缩
FileZipCreate filezipcreate=FileZipCreate.ZipFileInstance();
filezipcreate.ZipFileCreateName=Server.MapPath(DateTime.Now.ToString("yyyyMMddmmss")+".zip");
filezipcreate.FileDirectoryPath=Server.MapPath("../Admin/Css");
filezipcreate.ZipFileCreate();
}

private void Button2_Click(object sender, System.EventArgs e)
{

//解压
UZipFilesCreate uzipfilescreate=UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.ZipFileName=this.File1.PostedFile.FileName.Trim();
uzipfilescreate.filesCreatePath=Server.MapPath("");
uzipfilescreate.UZipCreateFiles();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值