Recently, I had to download multiple files from the server and compress them at the same time. I checked the Internet for a long time without a line. Finally, I came up with the following method for reference only.
/*** Compressed download folder * @ param namelist download file List * @ param path download path * @ param zipname compressed file name */public void zipDownloadFile (HttpServletResponse response, List
Namelist, String path, String zipname) {byte [] buf = new byte [1024]; try {// save locally set response. addHeader ("Content-Disposition", "attachment; filename =" + URLEncoder. encode (zipname, sysEncoding) + ". zip "); response. setContentType ("application/x-zip-compressed"); // write the file ServletOutputStream sos = response. getOutputStream (); ZipOutputStream zipOut = new ZipOutputStream (new BufferedOutputStream (sos); for (String n Ame: namelist) {ZipEntry entry = new ZipEntry (name); zipOut. putNextEntry (entry); InputStream bis = this. getStream (path, name); if (bis! = Null) {int readLen =-1; while (readLen = bis. read (buf, 0, 1024 ))! =-1) {zipOut. write (buf, 0, readLen);} bis. close () ;}} zipOut. close ();} catch (Exception e) {e. printStackTrace ();}}