Android extract zip from assets directory to local SDcard

Source: Internet
Author: User

Package Com.lapel.activity.html;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException; Import Java.io.inputstream;import Java.util.zip.zipentry;import Java.util.zip.zipinputstream;import android.content.context;/** * * Extract zip from assets directory to local * */public class Unzipfromassets {/** * Unzip assets ZIP archive to specified directory * @param Context Context Object * @param assetname Compressed file name * @param outputdirectory Output directory * @param isrewrite overwrite * @throws IOException */public St atic void UnZip (context context, string assetname, String outputdirectory, Boolean isrewrite) throws IOException {//Create extract The directory File File = new file (outputdirectory), or if the target directory does not exist, create if (!file.exists ()) {file.mkdirs ();} Open the compressed file InputStream InputStream = Context.getassets (). open (Assetname); Zipinputstream Zipinputstream = new Zipinputstream (inputstream);//read one entry point zipentry ZipEntry = Zipinputstream.getnextentry ();//use 1mbufferbyte[] buffer = new byte[1024 * 1024];//extract byte count int count = 0;//If the entry point is empty the description has been traversed Files and directories in all compressed packages while (zipentry! = null) {///if yesA directory if (Zipentry.isdirectory ()) {file = new file (outputdirectory + file.separator + zipentry.getname ());// The file needs to be overwritten or the file does not exist if (Isrewrite | |!file.exists ()) {File.mkdir ();}} else {//If file = new file (outputdirectory + file.separator + zipentry.getname ());//The file needs to be overwritten or the file does not exist, unzip the file if (Isrewrite | |!file.exists ()) {file.createnewfile (); FileOutputStream FileOutputStream = new FileOutputStream (file), while ((count = zipinputstream.read (buffer)) > 0) { Fileoutputstream.write (buffer, 0, count);} Fileoutputstream.close ();}} Navigate to the next file entry ZipEntry = Zipinputstream.getnextentry ();} Zipinputstream.close ();}}

Android extract zip from assets directory to local SDcard

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.