asp.net 文件夹压缩 并下载
asp.net 文件夹压缩 并下载,希望效率高一点的 asp.net
[解决办法]
网上找个ICSharpCode.SharpZipLib.dll
.net下引用之。
如下的是压缩方法。压缩之后,以zip格式放在你指定的目录下
最后生成超级链接给用户就可以了。
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
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch
{
}
}