Java Basics Primer-zipoutputstream Package Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.