代码
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();
}
}
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,如需转载请自行联系原作者