Example of file compression and decompression in Java [ZIP format, gzip format]

Java implements zip decompression and compression functions, basically using java polypeptide and recursive technology, which can compress and decompress a single file and any cascaded folder. It is a good example for some beginners.

Zip plays two roles: archiving and compression; Gzip does not archive files, but only compresses a single file. Therefore, on UNIX platforms, the command tar is usually used to create an archive file, and then gzip is used to compress the archive file.

The Java I / O class library also contains some classes that can read and write compressed format streams. To provide compression capabilities, just package them outside of existing I / O classes. These classes are not readers and writers, but subclasses of InputStream and outstream. This is because the compression algorithm is for bytes rather than characters. Related classes and interfaces:

Checksum interface: the interface implemented by classes adler32 and CRC32

Adler32: use alder32 algorithm to calculate the number of checksums

CRC32: use the CRC32 algorithm to calculate the number of checksums

Checkedinputstream: a derived class of InputStream, which can obtain the checksum of the input stream and is used to verify the integrity of the data

Checkedoutputstream: a derived class of OutputStream, which can obtain the checksum of the output stream and is used to verify the integrity of the data

Deflatoroutputstream: the base class of the compression class.

Zipoutputstream: a subclass of deflatoroutputstream, which compresses data into zip file format.

Gzipoutputstream: a subclass of deflatoroutputstream, which compresses data into gzip file format

InflaterInputStream: the base class of the decompression class

Zipinputstream: a subclass of InflaterInputStream, which can decompress data in ZIP format

Gzipinputstream: a subclass of InflaterInputStream, which can decompress data in ZIP format

Zipintry class: represents a zip file entry

Zipfile class: this class is used to read entries from zip files

Zip is used to compress and decompress multiple files

Java supports ZIP format class library more comprehensively. You can use it to compress multiple files into a compressed package. This class library uses the standard ZIP format, so it can be compatible with many compression tools.

Zipoutputstream class sets the compression method and the compression level used in the compression mode. Zipoutputstream Setmethod (int method) sets the default compression method for entries. As long as no compression method is specified for a single zip file entry, it is stored using the compression method set by zipoutputstream. The default value is zipoutputstream Deflated (for compressed storage) can also be set to stored (for packaged archive storage only). After zipoutputstream has set the compression method to deflated, we can further use the setlevel (int level) method to set the compression level. The compression level value is 0-9, a total of 10 levels (the larger the value, the better the compression). The default is deflator DEFAULT_ COMPRESSION=-1。 Of course, we can also set the compression method for a single condition through the setmethod method of the entry zipentry.

The zipentry class describes a compressed file stored in a zip file. Class contains a variety of methods that can be used to set and obtain information about zip entries. Class zipentry is used by zipfile [zipfile. Getinputstream (zipentry entry)] and zipinputstream to read the zip file, and zipoutputstream to write the zip file. There are the following useful methods: getname() returns the entry name, isdirectory() returns true if it is a directory entry (the directory entry is defined as the entry whose name ends with '/'), setmethod (int method) sets the compression method of the entry, which can be zipoutputstream Stored or zipoutputstream DEFLATED。

In the following example, we use Apache's Zip Toolkit (the package is ant. Jar), because the built-in Java type does not support Chinese paths, but they are used in the same way, except that the Apache compression tool has more interfaces for setting the encoding mode, and others are basically the same. In addition, if org.apache.tools.zip.zipoutputstream is used for compression, we can only use org.apache.tools.zip.zipentry to decompress You cannot use Java util. zip. Zipinputstream to extract and read. Of course, Apache does not provide zipinputstream class.

File compression:

File decompression:

Compress a single file with gzip

The interface of gzip is relatively simple, so you can use it if you only need to compress one stream. Of course, it can compress character stream and byte stream. The following is a text file in GBK encoding format.

The usage of compression class is very simple; Just wrap the output stream with gzipoutputstream or zipoutputstream, and then wrap the input stream with gzipinputstream or zipinputstream. The rest are ordinary I / O operations.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>