Java package file directory ask zip file

Source: Internet
Author: User

The Java Packaging file directory asks the zip file.

/** * Resource Pack Download class * Created by Ruan Banshu on 2015/4/13.    */public class Zipoputil {private static Logger Logger = Loggerfactory.getlogger (Zipoputil.class); /** * Package multiple files into a zip * * @param sourcefolder * @param zipfile * @return */public static Boolea        N ZipFile (String sourcefolder, File zipfile) {Boolean isOk = true;        File F = new file (Sourcefolder);        Zipoutputstream out = null;            try{if (!f.exists ()) {f.mkdirs ();            } out = new Zipoutputstream (new FileOutputStream (ZipFile));            Zip (out, F, "");            Out.flush ();        Fileutils.deletedirectory (f);            } catch (Exception e) {e.printstacktrace (); throw new Appexception ("Error compressing file!        "); } finally {if (null! = out) {try{out.close ()} catch (Exception e) {e.printstacktrace ()            ;}    }} return isOk;     }/** * Recursive compressed file * @param out* @param f * @param base * @throws Exception */private static void Zip (Zipoutputstream out, File F, String            Base) throws Exception {if (F.isdirectory ()) {file[] fl = F.listfiles ();            Out.putnextentry (new ZipEntry (base + "/")); Base = base.length () = = 0?            "": Base + "/";            for (int i = 0; i < fl.length; i++) {Zip (out, fl[i], base + fl[i].getname ());            }}else {out.putnextentry, new zipentry (base);            FileInputStream in = new FileInputStream (f);            int b;            Logger.info (base);            while ((b = In.read ())! =-1) {out.write (b);        } in.close (); }}/** * Download a single file * * @param file * @param request * @param response * @return */Publ IC Static Boolean downfile (file file, HttpServletRequest request, HttpServletResponse response) {Boolean isOk = tr        Ue OutputStreamMyout = null;        FileInputStream FIS = null;        Bufferedinputstream buff = null;        HttpSession session = Request.getsession ();        if (session! = NULL) {Session.setattribute ("state", "");            } try {response.setcontenttype ("application/x-msdownload");            Response.setcontentlength ((int) file.length ()); Response.setheader ("Content-disposition", "attachment;filename=" + New String (File.getname (). GetBytes ("Utf-8"), "            Iso8859-1 "));            FIS = new FileInputStream (file);            Buff = new Bufferedinputstream (FIS);            Byte[] B = new byte[1024 * 10];//equivalent to our cache long k = 0;//This value is used to calculate how many bytes are currently actually downloaded//from the response object to get the output stream, ready to download            Myout = Response.getoutputstream ();                while (K < File.length ()) {int J = buff.read (b, 0, b.length);                K + = J;            Writes the data in B to the client's memory Myout.write (b, 0, J);        } myout.flush (); } catch (Exception e) {e.printstacktrace ();        IsOk = false;                    } finally {try {if (null! = Myout) {myout.close ();                Myout = null;                    } if (null! = Buff) {buff.close ();                Buff = null;                    } if (null! = FIS) {fis.close ();                FIS = null;                } if (File.exists ()) {zipoputil.delfile (file);            }} catch (Exception e) {e.printstacktrace ();    }} return isOk; }/** * Delete a single file * * @param file * @return */public static Boolean delfile (file file) {Bo        Olean isOk = true;            try {if (File.isfile () && file.exists ()) {file.delete ();      }} catch (Exception e) {e.printstacktrace ();      IsOk = false;        } finally {//log ...}    return isOk; }



Java package file directory ask zip file

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.