asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net实现文件夹及文件压缩,并实现下载


发布日期:2023年04月18日
 
asp.net实现文件夹及文件压缩,并实现下载

步骤

)先引用 ICSharpCodeSharpZipLibdll

cs文件引入

using SystemIO;
using SystemText;

using ICSharpCodeSharpZipLibChecksums;
using ICSharpCodeSharpZipLibZip;
using ICSharpCodeSharpZipLibGZip;

)代码

#region 下列代码为压缩并下载代码
ZipOutputStream zos = null;
String strBaseDir = "";


void dlZipDir(string strPath string strFileName)
{
MemoryStream ms = null;
ResponseContentType = "application/octetstream";
strFileName = HttpUtilityUrlEncode(strFileName)Replace(+ );
ResponseAddHeader("ContentDisposition" "attachment; filename=" + strFileName + "zip");
ms = new MemoryStream();
zos = new ZipOutputStream(ms);
strBaseDir = strPath + "";
addZipEntry(strBaseDir);
zosFinish();
zosClose();
ResponseClear();
ResponseBinaryWrite(msToArray());
ResponseEnd();
}

void addZipEntry(string PathStr)
{
DirectoryInfo di = new DirectoryInfo(PathStr);
foreach (DirectoryInfo item in diGetDirectories())
{
addZipEntry(itemFullName);
}
foreach (FileInfo item in diGetFiles())
{
FileStream fs = FileOpenRead(itemFullName);
byte[] buffer = new byte[fsLength];
fsRead(buffer bufferLength);
string strEntryName = itemFullNameReplace(strBaseDir "");
ZipEntry entry = new ZipEntry(strEntryName);
zosPutNextEntry(entry);
zosWrite(buffer bufferLength);
fsClose();
}
}

PRotected void Button_Click(object sender EventArgs e)
{
string userPath ="D:华海实训(qqviewcom)各级账号和密码";

dlZipDir(userPath "华海实训(qqviewcom)服务账号");

}

#endregion

               

上一篇:asp.net使用代码从文件夹里读取图片

下一篇:在ASP.NET中实现弹出日历的具体方法