IO stream zip document

Zipinputstream is usually used in Java to read zip documents

Zip documents (usually) store one or more files in a compressed format, and each zip document has a file that contains files such as

The header of information such as the name and the compression method used. In Java, zipinputstream can be used to read in zip documents.

You may need to browse each individual item in the document, and the getnextentry method can return a description of these items

An object of type zipentry. The read method of zipinputstream is modified to return - 1 when it encounters the end of the current item

(instead of touching the end of the zip file), then you must call closeentry to read in the next item. Here is a typical code sequence for reading through the zip file:

When we want to read the contents of a zip item, we may not want to use the native read method. Generally, we will use a more competent stream filter method. For example, 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 out to a zip file, you can use zipoutputstream, and for each item you want to put into the zip file,

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

The putnextentry method of zipoutputstream starts writing out a new file and sends the file data to the zip stream. When

Upon completion, you need to call closeentry:

API:

Java. util. zip. ZipEntry

Java. util. zip. ZipFile

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

Create a zipfile to read data from a given string or file object.

(2) Enumeration entries () returns an enumeration object that enumerates the zipentry objects that describe the items in the zipfile.

(3) Zipentry getentry (string name) returns the item corresponding to the given name, or null if there is no corresponding item.

(4) InputStream getinputstream (zipentry Ze) returns the InputStream for the given item.

(5) String getname() returns the path of the zip file.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>