IO Stream-zip Documentation

Source: Internet
Author: User

Zipinputstream is commonly used in Java to read zip documents

A zip document (typically) stores one or more files in a compressed format, with each zip document containing a file

The header of the name and information such as the compression method used. In Java, you can use Zipinputstream to read into a zip document.

You may need to browse each individual item in the document, and the Getnextentry method can return a

An object of type ZipEntry. Zipinputstream's Read method is modified to return 1 when the end of the current item is encountered

(instead of hitting the end of the zip file), then you must call Closeentry to read the next entry. Here is a code sequence for a typical read-through ZIP file:

New Zipinputstream (new FileInputStream ("Test.zip")); ZipEntry entry; // Getnextentry () returns the ZipEntry object for the next item, or null if there are no more entries.  whilenull) {    ///  closeentry () closes the currently open item in this zip file. You can then        read the next item zip.closeentry () by using Getnextentry ()     ;} Zip.close ();    

When you want to read the contents of a zip entry, we may not want to use the native Read method, and we will typically use a more capable stream filter. For example, in order to read a text file inside a zip file, we can use the following loop:

Scanner s = new Scanner (Zip);

while (S.hasnextline ()) {

S.nextline ();

}

To write to a zip file, you can use Zipoutputstream, and for each item you want to put into a zip file,

You should create a ZipEntry object and pass the file name to the ZipEntry constructor, which will set other parameters such as the file date and decompression method. You can override these settings if you want. Then, you need to call

Zipoutputstream the Putnextentry method to start writing out the new file and send the file data to the zip stream. When

When you are finished, you need to call Closeentry:

New Zipoutputstream (new FileOutputStream ("Test.zip"new zipentry ("Test1.txt"  ) New ZipEntry ("Test2.txt"); // putnextentry (ZipEntry ze) writes out the information in the given zipentry to the stream and is defined as the stream used to write out the data, which can then be written out into the stream through write () . zip.putnextentry (ZE1); Zip.putnextentry (ze2); Zip.closeentry (); Zip.close () ;

Api:

Java.util.zip.ZipEntry

Java.util.zip.ZipFile

(1) ZipFile (String name) \ zipfile (file file)

Creates a zipfile that is used to read data from a given string or file object.

(2) enumeration entries () Returns a enumeration object that enumerates the ZipEntry objects that describe the individual items in this zipfile.

(3) ZipEntry getentry (String name) returns the item corresponding to the given name, or returns null if there is no corresponding entry.

(4) InputStream getinputstream (ZipEntry ze) returns the InputStream for the given item.

(5) String GetName () returns the path to this zip file.

IO Stream-zip Documentation

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.