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;
![](/Images/OutliningIndicators/None.gif)
using System.IO;
using System.Data;
![](/Images/OutliningIndicators/None.gif)
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Checksums;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// Summary description for zip
/// </summary>
public class zip
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public zip()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: Add constructor logic here
//
}
public void UnZip( string strFile, string strDir )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
说明#region 说明
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// 功能:将一个压缩文件解压缩到一个目录下
///
/// 参数:
/// strFile:压缩文件
/// strDir:解压缩目录
///
/// 返回:
/// 无
///
#endregion
if (strDir == "") strDir = Directory.GetCurrentDirectory();
if (!strDir.EndsWith(@"\")) strDir = strDir + @"\";
![](/Images/OutliningIndicators/InBlock.gif)
ZipInputStream s = new ZipInputStream(File.OpenRead(strFile));
ZipEntry theEntry;
![](/Images/OutliningIndicators/InBlock.gif)
while ((theEntry = s.GetNextEntry()) != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
![](/Images/OutliningIndicators/InBlock.gif)
if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + @"\";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDir + directoryName);
if (fileName != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream streamWriter = File.Create(strDir + directoryName + fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
s.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
//壓縮指定目錄的多個文件夾到一個壓縮文件
public string ZipMultiFile(string ZipFromFileDictory, string ZiptoFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] strFiles = Directory.GetFiles(ZipFromFileDictory);
string strPhysicalPath = ZiptoFileName;
string strZipFileName = strPhysicalPath + ".zip";
![](/Images/OutliningIndicators/InBlock.gif)
if (File.Exists(strZipFileName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(strZipFileName);
}
![](/Images/OutliningIndicators/InBlock.gif)
//需壓縮的文件個數
string[] strFilePaths = new string[strFiles.Length];
![](/Images/OutliningIndicators/InBlock.gif)
MemoryStream oMemoryStream = new MemoryStream();
![](/Images/OutliningIndicators/InBlock.gif)
ZipOutputStream oZipStream = new ZipOutputStream(File.Create(strZipFileName));
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i <= strFiles.Length - 1; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Read File Data To Stream#region Read File Data To Stream
![](/Images/OutliningIndicators/InBlock.gif)
FileStream oReadFileStream = File.OpenRead(strFiles[i]);
byte[] btFile = new byte[oReadFileStream.Length];
oReadFileStream.Read(btFile, 0, btFile.Length);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
string strCurrentFileName = Path.GetFileName(strFiles[i]);
strFilePaths[i] = strPhysicalPath + "/" + strCurrentFileName;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
ZipEntry oZipEntry = new ZipEntry(strCurrentFileName);
![](/Images/OutliningIndicators/InBlock.gif)
oZipEntry.DateTime = DateTime.Now;
oZipEntry.Size = oReadFileStream.Length;
![](/Images/OutliningIndicators/InBlock.gif)
Crc32 oCrc32 = new Crc32();
oCrc32.Reset();
oCrc32.Update(btFile);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
oZipEntry.Crc = oCrc32.Value;
![](/Images/OutliningIndicators/InBlock.gif)
oZipStream.PutNextEntry(oZipEntry);
oZipStream.Write(btFile, 0, btFile.Length);
![](/Images/OutliningIndicators/InBlock.gif)
//Stream Close
oReadFileStream.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
oZipStream.Finish();
oZipStream.Close();
![](/Images/OutliningIndicators/InBlock.gif)
return "A";
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
oZipStream.Finish();
oZipStream.Close();
return "Error" + ex.Message;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
壓縮控件:sharpziplib.zip
posted @
2008-06-25 12:30
Nina
阅读(
1187)
评论()
编辑
收藏
举报