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