Android file or folder compression

Source: Internet
Author: User

private static final int BUFFER = 1024 * 1024;/** * Compressed directory * * @param File * dir * @param zipoutputstream * Out * @param String * basedir */private void compress2directory (File dir, Zipoutputstream out,string Bas Edir) {if (!dir.exists ()) return; file[] files = dir.listfiles (), if (files!=null && files.length!=0) {for (int i = 0; i < files.length; i++) {Comp Ress (Files[i], out, Basedir + dir.getname () + "/");}}} /** * Compressed File * * @param file * file * @param zipoutputstream * out * @param String * Based ir */private void compress2file (file file, zipoutputstream out, String basedir) {if (!file.exists ()) {return;} Bufferedinputstream bis = null;try {bis = new Bufferedinputstream (new FileInputStream (file)); ZipEntry entry = new ZipEntry (Basedir + file.getname ()); Out.putnextentry (entry); int count;byte data[] = new Byte[buffer]; while ((count = bis.read (data, 0, BUFFER))! =-1) {out.write (data, 0, count);}} catch (Exception e) {WRite.debug ("" +e.getmessage ());} Finally {try {bis.close ()} catch (IOException e) {write.debug ("" +e.getmessage ());}}} public void compress (file file, zipoutputstream out, String basedir) {if (File.isfile ())} {this.compress2file (file, out, BA Sedir);} else {this.compress2directory (file, out, Basedir);}}

Android file or folder compression

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.