Today I'm going to talk to you. Use Zipoutputstream package download, I use Ant's Jar to pack, because he is good for Chinese support
You can also use the Java.util.zip package inside the tool class packaging, but he is not friendly to Chinese, many are garbled (including comments, file name, package name)
Import Org.apache.tools.zip.zipentry;import Org.apache.tools.zip.ZipOutputStream;
/** * Package Download * * @param response */private void Downfile (HttpServletResponse resp,arraylist& Lt String> filepathlist) throws Servletexception,ioexception {//header file Resp.setcontenttype ("Application/octet-stream") ; Resp.setheader ("Content-disposition", "attachment; Filename=" + this.getzipfilename ());//Compressed file Zipoutputstream Zos = New Zipoutputstream (Resp.getoutputstream ());//--assemble arraylist<file> fileList = new arraylist<file> (); for ( int i = 0; I < filepathlist.size (); i++) {File File = new file (Filepathlist.get (i)); Filelist.add (file);} file[] files = Filelist.toarray (new file[filelist.size ());//--pack ZipFile (Files, "", Zos); Zos.flush (); Zos.close ();}
/** * Package file * * @param subs * file array * @param baseName * Custom name * @param zos * output stream * @throws IOException */pri vate void ZipFile (file[] Subs, String baseName, Zipoutputstream zos) throws IOException {for (int i = 0; i < subs.length ; i++) {File f = subs[i];zos.putnextentry (new ZipEntry (BaseName + f.getname ())); FileInputStream fis = new FileInputStream (f); byte[] buffer = new Byte[1024];int r = 0;while ((r = fis.read (buffer))! =-1) {zos.write (buffer, 0, R);} Fis.close ();}} /** * Compressed Package name * * @return */private String getzipfilename () {Date date = new Date (); String s = date.gettime () + ". zip"; return s;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Basics Primer-zipoutputstream Package Download