Android Unzip zip to local sdcard from assets directory

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 {/** * Extract assets Zip compressed file to specified directory * @param 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 target directory file = new file (outputdirectory);
		If the destination directory does not exist, create an if (!file.exists ()) {file.mkdirs ();
		//Open Compressed file InputStream InputStream = Context.getassets (). open (Assetname);
		Zipinputstream Zipinputstream = new Zipinputstream (InputStream);
		Read a entry point zipentry ZipEntry = Zipinputstream.getnextentry ();
		Use 1Mbuffer byte[] buffer = new byte[1024 * 1024]; Decompression time WordSection count int count = 0; If the entry point is empty the description has traversed all the files and directories in the package while (ZipEntry!= null) {//If it is a directory if (Zipentry.isdirectory ()) {file = new Fi
				Le (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 it is file = new filename (outputdirectory + file.separator + zipentry.getname ());
					File needs to overwrite 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 ();
 }


}

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.