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
System.IO;
using
System.Data;

using
ICSharpCode.SharpZipLib;
using
ICSharpCode.SharpZipLib.Zip;
using
ICSharpCode.SharpZipLib.Zip.Compression;
using
ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using
ICSharpCode.SharpZipLib.Checksums;


/**/
/// <summary>
/// Summary description for zip
/// </summary>
public
class
zip

{
public zip()

{
//
// TODO: Add constructor logic here
//
}
public void UnZip( string strFile, string strDir )

{

说明#region 说明

/**////
/// 功能:将一个压缩文件解压缩到一个目录下
///
/// 参数:
/// strFile:压缩文件
/// strDir:解压缩目录
///
/// 返回:
/// 无
///
#endregion
if (strDir == "") strDir = Directory.GetCurrentDirectory();
if (!strDir.EndsWith(@"\")) strDir = strDir + @"\";

ZipInputStream s = new ZipInputStream(File.OpenRead(strFile));
ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)

{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + @"\";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDir + directoryName);
if (fileName != "")

{
FileStream streamWriter = File.Create(strDir + directoryName + fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)

{
size = s.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
s.Close();
}

//壓縮指定目錄的多個文件夾到一個壓縮文件
public string ZipMultiFile(string ZipFromFileDictory, string ZiptoFileName)

{
string[] strFiles = Directory.GetFiles(ZipFromFileDictory);
string strPhysicalPath = ZiptoFileName;
string strZipFileName = strPhysicalPath + ".zip";

if (File.Exists(strZipFileName))

{
File.Delete(strZipFileName);
}

//需壓縮的文件個數
string[] strFilePaths = new string[strFiles.Length];

MemoryStream oMemoryStream = new MemoryStream();

ZipOutputStream oZipStream = new ZipOutputStream(File.Create(strZipFileName));

try

{

for (int i = 0; i <= strFiles.Length - 1; i++)

{

Read File Data To Stream#region Read File Data To Stream

FileStream oReadFileStream = File.OpenRead(strFiles[i]);
byte[] btFile = new byte[oReadFileStream.Length];
oReadFileStream.Read(btFile, 0, btFile.Length);

#endregion

string strCurrentFileName = Path.GetFileName(strFiles[i]);
strFilePaths[i] = strPhysicalPath + "/" + strCurrentFileName;



ZipEntry oZipEntry = new ZipEntry(strCurrentFileName);

oZipEntry.DateTime = DateTime.Now;
oZipEntry.Size = oReadFileStream.Length;

Crc32 oCrc32 = new Crc32();
oCrc32.Reset();
oCrc32.Update(btFile);


oZipEntry.Crc = oCrc32.Value;

oZipStream.PutNextEntry(oZipEntry);
oZipStream.Write(btFile, 0, btFile.Length);

//Stream Close
oReadFileStream.Close();
}

oZipStream.Finish();
oZipStream.Close();

return "A";
}
catch (Exception ex)

{
oZipStream.Finish();
oZipStream.Close();
return "Error" + ex.Message;
}

}

}
壓縮控件:sharpziplib.zip
转载于:https://www.cnblogs.com/Nina-piaoye/archive/2008/06/25/1229582.html