您的位置:首页 > 编程语言 > ASP

asp.net实现打包程序

2011-11-23 15:36 344 查看


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

[title3]以下是主要的代码实现 ,因为博客原因,所需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)

{

}

}

}

}[/title3]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: