压缩解压目录和文件--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();
}

你可能感兴趣的:(压缩解压目录和文件--ICSharpCode.SharpZipLib.dll 算法 (转))