服务器多个文件打包压缩下载

引用:ICSharpCode.SharpZipLib.dll

 

 

复制代码
代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using zhan.File;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;

public partial class downfile : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (Request.QueryString["path"!= null && Request.QueryString["path"].ToString() != "")
        {
            package(Request.QueryString[
"path"].ToString());
        }
        
    }
    
private void package(string filename)
    {
        
string file = filename.Substring(0, filename.LastIndexOf('.'));
        
string suffix = filename.Substring(filename.LastIndexOf('.'), filename.Length - filename.LastIndexOf('.'));
        
        
string[] files ={ filename, file + "_2" + suffix, file + "_3" + suffix };
        
        
string zipname = DateTime.Now.ToString("yyyyMMddHHmmss"+ ".zip";
        
string zippath = "~/upload/temp/" + zipname;
        ZipFileDictory(files, zippath);

        FileStream fs 
= File.OpenRead(Server.MapPath(zippath));
        
byte[] buffer = new byte[102400];
        
long dataLengthToRead = fs.Length;//获取下载的文件总大小 

        fs.Position 
= 0;
        Response.ContentType 
= "application/octet-stream";
        Response.AddHeader(
"Content-Length", fs.Length.ToString());
        Response.AddHeader(
"content-disposition""attachment;filename=" +HttpUtility.UrlEncode(zipname));
        
while (dataLengthToRead > 0 && Response.IsClientConnected)
        {
            
int lengthRead = fs.Read(buffer, 0, buffer.Length);//读取的大小 
            Response.OutputStream.Write(buffer, 0, lengthRead);
            Response.Flush();
            dataLengthToRead 
= dataLengthToRead - lengthRead;
        }
        fs.Close();
        fs.Dispose();
        File.Delete(Server.MapPath(zippath));
        Response.End();
    }
    
public void ZipFileDictory(string[] filenames, string filename)
    {
        Crc32 crc 
= new Crc32();
        ZipOutputStream s 
= new ZipOutputStream(System.IO.File.Create(System.Web.HttpContext.Current.Server.MapPath(filename)));
        s.SetLevel(
6);

        
foreach (string file in filenames)
        {
            
if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(file)))
            {
                FileStream fs 
= System.IO.File.OpenRead(System.Web.HttpContext.Current.Server.MapPath(file));

                
byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 
0, buffer.Length);
                ZipEntry entry 
= new ZipEntry(file.Substring(file.LastIndexOf('/'+ 1, file.Length - file.LastIndexOf('/'- 1));

                entry.DateTime 
= DateTime.Now;

                entry.Size 
= fs.Length;
                fs.Close();

                crc.Reset();
                crc.Update(buffer);

                entry.Crc 
= crc.Value;

                s.PutNextEntry(entry);

                s.Write(buffer, 
0, buffer.Length);
            }
        }

        s.Finish();
        s.Close();
    }
}
复制代码

 

 

posted @   94cool  阅读(408)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2011年2月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 1 2 3 4 5
6 7 8 9 10 11 12
点击右上角即可分享
微信分享提示