Pack200, a good tool hidden in JDK

Source: Internet
Author: User

Java 5 contains a compression tool pack200, which can compress common jar files efficiently. The implementation principle is to merge constant pools and Remove useless information based on the special structure of Java classes to achieve efficient compression of Java classes. Because Java classes are specially compressed, the compression of common files is no different from that of common compression software, but the compression ratio of jar files can easily reach 10-40%. This is useful in Java application deployment, especially for mobile Java computing, which can greatly reduce the download of code.
Java 5 also provides API interfaces for this technology, which you can embed into your program for use. The method is simple. The following code can be used to compress and decompress the jar:
Compression
Packer packer = pack200.newpacker ();
Outputstream output = new bufferedoutputstream (New fileoutputstream (OUTFILE ));
Packer. Pack (New jarfile (jarfile), output );
Output. Close ();
Extract
Unpacker = pack200.newunpacker ();
Output = new jaroutputstream (New fileoutputstream (jarfile ));
Unpacker. Unpack (pack200file, output );
Output. Close ();
For more details, see the following URL:
Http://java.sun.com/javase/6/docs/technotes/guides/deployment/deployment-guide/pack200.html
========================================================== ========================================================== =
Currently, a swing program is released, including Java Web Start and Applet versions. However, due to the introduction of third-party packages, the number of released packages reaches 5 MB, even in the LAN, the loading speed is unbearable. After all, for most applications, network bandwidth is still the final system bottleneck.

Fortunately, JDK 5 provides support for gizp and pack200 compression technologies in Java Web Start and Java Plug-in, through compression, Java's network applications will be reduced again on the basis of jar compression.

Gzip
The jar package contains many non-Java class resource files, such as JPEG and GIF files. It is more appropriate to use gzip.

Pack200
If the vast majority of the information in the jar package is the class content, pack200 is your preferred solution, because pack200 is specially designed to optimize the Java class of the class, mainly reflected in the following aspects:

It merges and sorts the constant-pool data in the class files and co-locates them in the archive.
It removes redundant class attributes.
It stores internal data structures.
It use delta and variable length encoding.
It chooses optimum coding types for secondary compression.

The entire release process of compressing the signature

* **/Jdk5/bin/pack200 -- repack demo. Jar
-- Repack is equivalent to pack200 and unpack200 commands. Its role is to demo. the class structure in jar is organized in the pack200 format. Because the signature needs to be hashed based on the class structure of the jar package, if the pack200 format of jar is not sorted first, compress the signature with pack200, which will lead to invalid signature. Therefore, the first step is to perform the -- repack operation, which seems useless.
Jarsigner-keystore mykeystore demo. jar...
Sign demo. Jar files in pack200 format
* **/Jdk5/bin/pack200 demo.jar.pack.gz demo. Jar
Compress pack200s to demo.jar.pack.gz.

*/Jdk5/sample/JNLP/servlet jar package, mainly copy the jnlp-servlet.jar to the Web-INF/lib/directory of the web project to be released
Modify the Web. xml file of the Web release project, and locate all *. JNLP and *. Jar URLs to JNLP. sample. servlet. jnlpdownloadservlet for processing.
Copy demo.jardemo.jar.pack.gz to the path specified by JNLP or applet.
If you use the above operations, it will be okay. Maybe you want to ask what changes do JNLP and Applet need. The answer is no. Everything is transparent by jnlpdownloadservlet, in JNLP and Applet, The point is demo. jar, jnlpdownloadservlet will automatically decide whether to send demo.jar.pack.gz or demo based on the request accept-encoding. jar. In this way, if Java Web Start or Java Plug-in of JDK 5 is installed on the client, jnlpdownloadservlet will automatically issue the demo.jar.pack.gz package compressed by pack200. If the client is jdk1.4 or an older environment, then jnlpdownloadservlet will naturally issue a demo. jar.

Pack200 compression and decompression speed is relatively fast, and the compression ratio is also amazing, after I used the packet 4.46mb compression, it became 1.44 MB (0.322% ), in addition, the larger the package, the compression ratio will be obvious. It is said that if the jar package is a class, it can be compressed to a size smaller than 1/9. In fact, javawebstart has many other functions. For example, you can download lazy files and update them separately based on different jar packages. The settings can be downloaded at class granularity based on class changes in jar. It's not as exciting as action. Hurry up and lose weight on your release program.

 

Http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/downloadservletguide.html#example1

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.