Extract code for Android zip file

Source: Internet
Author: User

 

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.

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.