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 ();
}
}