"Original" Java implementation of compression, decompression FAQ Memo

Source: Internet
Author: User

National Day holiday, house Ah ~ accidentally saw a Java compression algorithm article, by the way Niang some related articles to see.

Would like to delve into the algorithm, but, after all, is the holiday, which has the mind Ah, the actual application of a simple memo.

1. Chinese garbled problem

In fact, this is inadvertently seen, the degree of Niang is all over the sky, it seems that you are the program ape or pragmatic application for more AH.

Degrees Niang, 99% of the posts are said to use ant in the org.apache.tools.zip.* to replace java.util.zip.*

This approach to solve the problem of Chinese garbled. The reason is that the JDK wrote dead UTF-8 encoding, and the operating system file encoding if not UTF-8, such as the Chinese windows under the GBK, the compressed, extracted filename will appear garbled.

Because, Org.apache.tools.zip.ZipOuputStream has the setencoding (String) method, it can be resolved.

    • Zos.setencoding ("GBK"); Zos is the Zipouputstream object
    • Zos.setencoding (System.getproperties ("sun.jnu.encoding"));//This is taller than the above (safe)

When compressing, (do not know what reason?) Ant also does not provide the Org.apache.tools.zip.ZipInputStream class, so it can only use the class that comes with the JDK.

    • System.setproperty ("sun.zip.encoding", System.getproperty ("sun.jnu.encoding"));//above is decompression, then before compressing, so set up, More secure, many of the posts did not mention Oh

Even in the degree Niang input keyword "1.7", also can't search out, is actually JDK1.7 already can set up the character set.

Java code
  1. /**
  2. * Creates a new ZIP output stream.
  3. *
  4. * @param out the actual output stream
  5. *
  6. * @param charset the {@linkplain Java.nio.charset.Charset charset}
  7. * To is used to encode the entry names and comments
  8. *
  9. * @since 1.7
  10. */
  11. Public Zipoutputstream (OutputStream out, Charset Charset) {
  12. super (out, new Deflater (Deflater.default_compression, true));
  13. if (charset = = null)
  14. throw New NullPointerException ("CharSet is null");
  15. this.zc = Zipcoder.get (charset);
  16. Usesdefaultdeflater = true;
  17. }

After Java1.7, we only have to construct Zos,

Zipoutputstream Zos = new Zipoutputstream (out, System.getproperties ("sun.jnu.encoding"));

Can solve the problem of Chinese garbled.

Is it possible to forget Org.apache.tools.zip? At least I didn't see that it had more advantages in other ways.

2. Compression/decompression speed and compression ratio

Speed and efficiency are contradictory, but excellent algorithms can resolve this contradiction as much as possible. Yes, the truth is brutal, no known algorithm is the fastest and the highest compression ratio. Let's borrow http://www.importnew.com/14410.html's conclusion.



(The longer the ecliptic the better, the faster, the shorter the green path, the smaller the file size)

The author of this article, preferably LZ4. Fastnative, followed by deflate (m=1)

Here, I'd like to explain two.

    • If our application system does not have enough cow file storage System (IO very fast), there is no very exaggerated pressure load, then I do not recommend choosing LZ4. Fastnative, because it relies on JNI, is more complicated to use than pure java. And our hard drive may not be able to "enjoy" this speed.
    • Deflate is the most "common" java.util.zip of the JDK, in fact its core algorithm is native, but we do not have to bother with the JNI, the JDK has been dry. Note, however, that it uses a compression ratio of-1 by default!
      We need to set him to 1, which is the fastest (in fact, the compression rate is not much smaller)

Zipoutputstream Zos = new Zipoutputstream (out, System.getproperties ("sun.jnu.encoding"));//Do you remember this property?

Zos.setlevel (Java.util.zip.Deflater.BEST_SPEED); Best_speed = = 1

The above two points, is a little note of the short study today, also is at least summed up more than two, the online comparison is not good to find, the content of more useful articles, in giving themselves a memo at the same time, if the others have a little use, presumably also excellent.

"Source Address acquisition

SPRINGMVC + mybatis Integration details, and the problems encountered, please refer to the following information:

Resources:

Http://www.springmvc,net/detail/6074493.html

http://my.spring.net/wangbiglei/blog/489583

http://my.springmvc.net/wangbiglei/blog/489604



"Original" Java implementation of compression, decompression FAQ Memo

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.