C#中怎么将文件夹或文件打包下载

a4562834 2011-07-22 06:11:22
我想实现一个下载功能,包含文件夹或文件(jpg、txt格式之类的),请问怎么将他们打包下载啊?


先谢谢了!
...全文
450 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
a4550718 2011-12-28
  • 打赏
  • 举报
回复
/// <summary>
/// 文件夹打包方法
/// </summary>
/// <param name="strPath"></param>
/// <param name="strFileName"></param>
protected void dlZipDir(string strPath, string strFileName)
{
MemoryStream ms = null;
Response.ContentType = "application/octet-stream";
strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
string strBaseDir = Request.PhysicalApplicationPath + strPath;
addZipEntry(strBaseDir);

zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}

/// <summary>
/// 文件夹打包方法
/// </summary>
/// <param name="PathStr"></param>
protected void addZipEntry(string PathStr)
{
DirectoryInfo di = new DirectoryInfo(PathStr);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName);
}
foreach (FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = item.FullName.Replace(PathStr, "");
string fileName = strEntryName.Substring(strEntryName.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
a4550718 2011-12-28
  • 打赏
  • 举报
回复
/// <summary>
/// 文件夹打包方法
/// </summary>
/// <param name="strPath"></param>
/// <param name="strFileName"></param>
protected void dlZipDir(string strPath, string strFileName)
{
MemoryStream ms = null;
Response.ContentType = "application/octet-stream";
strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
string strBaseDir = Request.PhysicalApplicationPath + strPath;
addZipEntry(strBaseDir);

zos.Finish();
zos.Close();
Response.Clear();
Response.BinaryWrite(ms.ToArray());
Response.End();
}

/// <summary>
/// 文件夹打包方法
/// </summary>
/// <param name="PathStr"></param>
protected void addZipEntry(string PathStr)
{
DirectoryInfo di = new DirectoryInfo(PathStr);
foreach (DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName);
}
foreach (FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strEntryName = item.FullName.Replace(PathStr, "");
string fileName = strEntryName.Substring(strEntryName.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
fs.Close();
}
}
小阳 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 weike021996 的回复:]
gogogo
[/Quote]
这个好像还不错!
AK47 2011-07-29
  • 打赏
  • 举报
回复
good good study,day day up!
viki117 2011-07-29
  • 打赏
  • 举报
回复
关键看服务段,ZIP网上大把类库..
  • 打赏
  • 举报
回复
mark
weike021996 2011-07-24
  • 打赏
  • 举报
回复
zjq8888882009 2011-07-24
  • 打赏
  • 举报
回复
百度搜,压缩,解压类,一搜一大堆。
ljjhw320 2011-07-23
  • 打赏
  • 举报
回复
你先进行打包了,再进行下载就可以啊!
a4562834 2011-07-23
  • 打赏
  • 举报
回复
具体的给点代码吧,谢谢了!
极地_雪狼 2011-07-22
  • 打赏
  • 举报
回复
调用winrar或zip的api接口
sugarbelle 2011-07-22
  • 打赏
  • 举报
回复
服务器端要支持压缩包功能...
你搜zip 命令压缩..或者rar压缩..很简单的,就和ping 网址一样.
xixihaha_2011_098 2011-07-22
  • 打赏
  • 举报
回复
用winrar或者winzip 命令行。打包,然后通过地址就可以下载吧

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧