步骤
)先引用 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)