Java generates a ZIP file and exports

Source: Internet
Author: User

To summarize, about Java download zip file and export the method, browser export.

String downloadname = "Download file name. zip"; Downloadname=Browsercharcodeutils.browsercharcodefun (Request, downloadname);//download file name garbled problem solved//To Package a file for download        Try{OutputStream out=Response.getoutputstream (); byte[] data = Createzip ("/filestorage/download"));//server storage address response.reset (); Response.setheader ("Content-disposition", "attachment;filename=" +downloadname); Response.AddHeader ("Content-length", "" "+data.length); Response.setcontenttype ("Application/octet-stream;charset=utf-8");            Ioutils.write (data, out);            Out.flush ();        Out.close (); } Catch(Exception e) {e.printstacktrace (); }

Get download zip file stream

 Public byte throws exception{        new  bytearrayoutputstream ();         New Zipoutputstream (outputstream);         // Package The destination file as a Zip export        New File (srcsource);         A (Zip,file,"");        ioutils.closequietly (Zip);         return Outputstream.tobytearray ();    }

 Public voidA (zipoutputstream zip, file file, String dir)throwsException {//If the current folder is present, further processing            if(File.isdirectory ()) {//Get file list informationfile[] Files =File.listfiles (); //Add a folder to the next level package directoryZip.putnextentry (NewZipEntry (dir + "/")); Dir= Dir.length () = = 0? "": Dir + "/"; //loops package files in a folder                 for(inti = 0; i < files.length; i++) {A (zip, files[i], dir+ Files[i].getname ());//Recursive processing                }            } Else{//current is file, package processing//file input streamBufferedinputstream bis =NewBufferedinputstream (Newfileinputstream (file)); ZipEntry entry=NewZipEntry (dir);               Zip.putnextentry (entry);               Zip.write (Fileutils.readfiletobytearray (file));               ioutils.closequietly (bis);               Zip.flush ();            Zip.closeentry (); }    }

Java generates a ZIP file and exports

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.