日期:2014-05-17 浏览次数:20471 次
using ICSharpCode.SharpZipLib.Zip;
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="dir">文件目录</param>
/// <param name="zipfilename">zip文件名</param>
public static void compressFiles(string dir, string zipfilename)
{
if (!Directory.Exists(dir))
{
//ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script defer>alert('目录不存在,请联系管理员。');</script>");
return;
}
try
{
string[] filenames = Directory.GetFiles(dir);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename)))
{
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
&nbs