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

 

复制代码
ExpandedBlockStart.gif 代码
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();
    }
}
复制代码



本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2011/02/16/1955899.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值