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

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

步骤

)先引用 ICSharpCode SharpZipLib dll

) cs文件引入

using System IOusing System Text

using ICSharpCode SharpZipLib Checksumsusing ICSharpCode SharpZipLib Zipusing ICSharpCode SharpZipLib GZip

代码

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

       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)           strBaseDir = strPath + ""           addZipEntry(strBaseDir)           zos Finish()           zos Close()           Response Clear()           Response BinaryWrite(ms ToArray())           Response End()       }

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 buffer Length)               string strEntryName = item FullName Replace(strBaseDir "")               ZipEntry entry = new ZipEntry(strEntryName)               zos PutNextEntry(entry)               zos Write(buffer buffer Length)               fs Close()           }        }  

PRotected void Button _Click(object sender EventArgs e)         {             string userPath ="D:华海实训(qqview )各级账号和密码"                        dlZipDir(userPath "华海实训(qqview )服务账号")

}

#endregion

lishixinzhi/Article/program/net/201311/14232

将文件批量压缩,使用zipfile库。一幅厚颜无耻的漫画。

以下代码可以将文件批量压缩:importos;importzipfile;fromrandomimportrandrange;defzip_dir(path,zip_handler):forroot,dirs,filesinos.walk(path):forfileinfiles:zip_handler.write(os.path.join(root,file));if__name__=='__main__':to_zip=input("""Enterthenameofthefolderyouwantto(N.B.:Thefoldernameshouldnotcontainblankspaces)>""")to_zip=to_zip.strip()+"/"zip_file_name=f'zip{randrange(0,10000)}.zip'zip_file=zipfile.ZipFile(zip_file_name,'w',zipfile.ZIP_DEFLATED)zip_dir(to_zip,zip_file)zip_file.close()print(f'FileSavedas{zip_file_name}')。输入下面这行代码,你会跳转到一个漫画网页:>>importantigravity


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12071961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存