日期:2014-05-20  浏览次数:21114 次

解压zip文件
解压zip文件时 我需要用到ZipInputStream类,可我不知道需要引用什么命名空间,或者有好的方法可以解压zip文件,请帖码,万分感谢

------解决方案--------------------

http://www.icsharpcode.net/OpenSource/SharpZipLib/
------解决方案--------------------
//压缩

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("*.*"))