2011-04-01 17:58:52| Category: Android | Report | Font size Subscription
How does the unzip function of the zip file be implemented in the Android platform? Because Android has integrated the Zlib library, for the English and non-password zip file decompression is relatively simple, the following Android123 to everyone a zip code to extract Java, can be used in any version of Android, Unzip this static method is relatively simple, parameter one is the full path of the source zip file, parameter two is the folder after decompression.
Private static void Unzip(String zipfile, String targetDir) {
int BUFFER = 4096; Here buffer we use 4KB,
String strentry; Save the entry name for each zip
try {
Bufferedoutputstream dest = null; Buffered output stream
FileInputStream fis = new FileInputStream (ZipFile);
Zipinputstream ZiS = new Zipinputstream (new Bufferedinputstream (FIS));
ZipEntry entry; An instance of each zip entry
while ((entry = Zis.getnextentry ()) = null) {
try {
LOG.I ("Unzip:", "=" + entry);
int count;
byte data[] = new Byte[buffer];
Strentry = Entry.getname ();
File Entryfile = new file (TargetDir + strentry);
File Entrydir = new file (Entryfile.getparent ());
if (!entrydir.exists ()) {
Entrydir.mkdirs ();
}
FileOutputStream fos = new FileOutputStream (entryfile);
Dest = new Bufferedoutputstream (FOS, BUFFER);
while ((count = zis.read (data, 0, BUFFER))! =-1) {
Dest.write (data, 0, count);
}
Dest.flush ();
Dest.close ();
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
Zis.close ();
} catch (Exception cwj) {
Cwj.printstacktrace ();
}
}
Above is the Android Development Network summary ZIP file Decompression code, I hope you all useful, it is important to note that the parameters are filled with the complete path, such as/mnt/sdcard/xxx.zip type.