实现过程
)先引用ICSharpCodeSharpZipLibdll
)cs文件引入
usingSystemIO;
usingSystemText;
usingICSharpCodeSharpZipLibChecksums;
usingICSharpCodeSharpZipLibZip;
usingICSharpCodeSharpZipLibGZip;
)代码
#region下列代码为压缩并下载代码
ZipOutputStreamzos=null;
StringstrBaseDir="";
voiddlZipDir(stringstrPathstringstrFileName)
{
MemoryStreamms=null;
ResponseContentType="application/octetstream";
strFileName=HttpUtilityUrlEncode(strFileName)Replace(’+’’’);
ResponseAddHeader("ContentDisposition""attachment;filename="+strFileName+"zip");
ms=newMemoryStream();
zos=newZipOutputStream(ms);
strBaseDir=strPath+"";
addZipEntry(strBaseDir);
zosFinish();
zosClose();
ResponseClear();
ResponseBinaryWrite(msToArray());
ResponseEnd();
}
voidaddZipEntry(stringPathStr)
{
DirectoryInfodi=newDirectoryInfo(PathStr);
foreach(DirectoryInfoitemindiGetDirectories())
{
addZipEntry(itemFullName);
}
foreach(FileInfoitemindiGetFiles())
{
FileStreamfs=FileOpenRead(itemFullName);
byte[]buffer=newbyte[fsLength];
fsRead(bufferbufferLength);
stringstrEntryName=itemFullNameReplace(strBaseDir"");
ZipEntryentry=newZipEntry(strEntryName);
zosPutNextEntry(entry);
zosWrite(bufferbufferLength);
fsClose();
}
}
PRotectedvoidButton_Click(objectsenderEventArgse)
{
stringuserPath="D:华海实训(hohisoftcom)各级账号和密码";
dlZipDir(userPath"华海实训(hohisoftcom)服务账号");
}
#endregion