using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using System.Collections;
namespace ZipSharpLibray.Common.Control
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
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: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 压缩后的文件的全名称
/// </summary>
public string ZipFileCreateName
{
set
{
this.zipfilecreatename=value;
}
get
{
return this.zipfilecreatename;
}
}
/// <summary>
/// 待压缩文件目录
/// </summary>
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;
}
/// <summary>
/// 压缩文件的方法
/// </summary>
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();
}
/// <summary>
/// 获取所有文件
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 获取一个文件夹下的所有文件夹里的文件
/// </summary>
/// <param name="dirs"></param>
/// <param name="filesList"></param>
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);
}
}
/// <summary>
/// 获取一个文件夹下的文件
/// </summary>
/// <param name="strDirName">目录名称</param>
/// <param name="filesList">文件列表HastTable</param>
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
{
/// <summary>
/// UZipFilesCreate 的摘要说明。
/// </summary>
public class UZipFilesCreate
{
private string zipfilename;
private string filescreatepath;
private static UZipFilesCreate uzipfilescreate=null;
private UZipFilesCreate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// Zip文件目录
/// </summary>
public string ZipFileName
{
set
{
this.zipfilename=value;
}
get
{
return this.zipfilename;
}
}
/// <summary>
/// 解压文件目录
/// </summary>
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<dirlevelname.Length-1;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();
}