Web-side file packaging. zip download

Source: Internet
Author: User

Use the Ant.jar Package API for folder packaging. Directly on the code:

1String zipfilename = "Test.zip";2File ZipFile =NewFile (zipfilename);3Org.apache.tools.ant.types.FileSet Fileset =NewFileset ();4Org.apache.tools.ant.Project PRJ =NewProject ();5Org.apache.tools.ant.taskdefs.Zip Zip =NewZip ();6 Zip.setproject (PRJ);7 Zip.setdestfile (ZipFile);8 Fileset.setproject (PRJ);9 fileset.setdir (file);//This is the setting for the path to the files. Ten Zip.addfileset (fileset); One Zip.execute (); AHttpServletResponse response =Super. Currentresponse; -Response.setheader ("Content-type", "Application/zip"); -Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (zipfilename, "UTF-8")); theOutputStream OS =Response.getoutputstream (); -InputStream is =NewFileInputStream (ZipFile); - Downloadutil.transfer (is, OS); -Zipfile.delete ();

If you do not want to download all of the files under the file path while downloading, it will be a part of the file that fits your needs.

The following example, in addition to check the requirements, but also the remote file operation, using the connection obtained by channelsftp, file read and download.

As follows:

private com.jcraft.jsch.ChannelSftp         Channel;//This line is a class member variable
1String Zipfilename =Getserverzipfilename (inputfilename);2 3         if(Channel = =NULL|| !channel.isconnected ()) {4Channel =Sftputils.connect (Sftphost, Integer.parseint (Sftpport), Sftpusername, Sftppassword);//Get Connected. 5         }6 7HttpServletResponse response =Super. Currentresponse;8 9         Try {Ten             BooleanIsmultifilesname =Ismultifilesname (inputfilename); OneVector <com.jcraft.jsch.ChannelSftp.LsEntry> vs =channel.ls (LogFilePath); A             if(Vs.isempty ()) { -                 return false; -}Else { theResponse.setheader ("Content-type", "Application/octet-stream"); -Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (zipfilename, "UTF-8")); -OutputStream out =Response.getoutputstream (); -Java.util.zip.ZipOutputStream zipout =NewZipoutputstream (NewBufferedoutputstream (out)); +                  for(Lsentry Entry:vs) { -                     if(Entry.getattrs (). toString (). StartsWith ("-")) { +String EntryName =entry.getfilename (); A                         if(Ismultifilesname && entryname.startswith (inputfilename.substring (0, Inputfilename.length ()-1)) at&&!entryname.matches ("^.*[\\*]+.*$") | |entryname.equals (InputFileName)) {//Rules-compliant files do output processing -Java.util.zip.ZipEntry ZipEntry =NewZipEntry (entryname); - zipout.putnextentry (zipentry); -InputStream bis = channel.get (LogFilePath +entryname); Note here that the name of the file under the target directory cannot have *, otherwise the target of this step get method may be multiple files and error.  -                             if(bis! =NULL) { -                                 intCount = 0; in                                 byte[] buffer =New byte[8192]; -                                  while((count = bis.read (buffer)) > 0) { toZipout.write (buffer, 0, count); +                                 } - bis.close (); the                             } *                         } $                     }Panax Notoginseng                 } - zipout.close (); the             } +}Catch(Exception e) { A Response.reset (); the e.printstacktrace (); +             return false; -}finally { $             if(Channel! =NULL&&!channel.isclosed ()) { $ channel.getsession (). disconnect (); - Channel.disconnect (); -             } the}

Web-side file packaging. zip download

Related Article

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.