asp.net实现打包程序

44 篇文章 0 订阅

最近在公司做一个系统,在其中运用到了打包技术。就是把很多文件打包成一个zip文件然后让客户端下载。

以下是主要的代码实现 ,因为博客原因,所需dll请联系我 本人个人网页
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Web.SessionState;
namespace WebApplication1
{
public class PackDownLoad
{
//打包临时文件路径
public static string _homeworkCompressedFileLocation = System.Web.Configuration.WebConfigurationManager.AppSettings["downLoadPath"].ToString();
/// <summary>
/// 代理异步download
/// </summary>
public delegate void AsynDownload(string downloadWhat, HttpSessionState session);
/// <summary>
/// 打包下载主方法
/// </summary>
public static void putPack(string zipName, HttpSessionState session)
{
AsynDownload asyn = new AsynDownload(beginDownload);
asyn.BeginInvoke(zipName, session, null, null);
}
/// <summary>
/// 开始打包下载
/// </summary>
/// <param name="downloadWhat"></param>
private static void beginDownload(string zipFileName, HttpSessionState session)
{
try
{
FileStream fileStream = File.Create(Path.Combine(_homeworkCompressedFileLocation, zipFileName));
using (ZipOutputStream s = new ZipOutputStream(fileStream))
{
s.SetLevel(9); // 0-9, 9 being the highest level of compression
foreach (string className in new string[] { "1", "2", "3" })
{
FileStream excelStream = new FileStream(_homeworkCompressedFileLocation + "/ITP1.0需求反馈.xls", FileMode.Open, FileAccess.Read);
ZipEntry excelEntry = new ZipEntry(className + ".xls");
excelEntry.DateTime = DateTime.Now;
s.PutNextEntry(excelEntry);
int fileSize = (int)excelStream.Length;
byte[] bytes = new byte[fileSize];
excelStream.Read(bytes, 0, fileSize);
excelStream.Close();
s.Write(bytes, 0, fileSize);
}
//压缩文件生成了以后,更改session中的值,说明已经转换完成了。
session[zipFileName] = true;
}
}
catch (Exception ex)
{
}
}
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值