步骤
)先引用 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