Android_jarzip compression and decompression files

Source: Internet
Author: User

This article is from the "Android Development Authority Guide"

Several classes and interfaces in the Java.util.jar and java.util.zip packages are provided in ANDROIDSDK.

Basic steps for compressing files:

1. Create a Jaroutputstream or Zipoutputstream object to generate a jar or zip file, respectively.

2. Create a (Jar|zip) OutputStream object, and you cannot write data directly to the output stream. Because the compressed file also needs to use the file name and path to identify the compressed file, you need to create a jarentry or ZipEntry object for each file you want to compress. each (jar|zip) entry object represents a compressed file and specifies the filename and path of the compressed file in the compressed package through the (Jar|zip) entry object.

3. Call (Jar|zip) The Outputstream.putnextentry method to set the currently open (Jar|zip) Entry object.

4. Write data to (Jar|zip) OutputStream object.

5. Call (Jar|zip) Outputstream.closeentry method to close the currently open (Jar|zip) Entry object. If you still need to compress the files, go back to the second step.


To unzip a file:

1. Create a Jarinputstream or Zipinputstream object. This object typically specifies the file to extract by using the FileInputStream object.

2, use (Jar|zip) InputStream. The Getnextentry method enumerates all the files in the compressed package. If the Getnextentry method returns NULL, it indicates that the compressed file has been processed.

3, through the (Jar|zip) Entry.getname method to obtain the file compressed path and file name (in order to ensure that the extracted file name is consistent with the pre-compression, of course, you can also use other file names), and use the FileOutputStream object to specify the extracted files.

4. Output the extracted data stream to the FileOutputStream object.

5. Call (Jar|zip) Entry.closeentry method to close the currently open (Jar|zip) Entry object. If there are files that are unzipped, return to the second step to continue.


Source code Example:

http://download.csdn.net/download/zlfxy/5098434

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.