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
- /**
- * Creates a new ZIP output stream.
- *
- * @param out the actual output stream
- *
- * @param charset the {@linkplain Java.nio.charset.Charset charset}
- * To is used to encode the entry names and comments
- *
- * @since 1.7
- */
- Public Zipoutputstream (OutputStream out, Charset Charset) {
- super (out, new Deflater (Deflater.default_compression, true));
- if (charset = = null)
- throw New NullPointerException ("CharSet is null");
- this.zc = Zipcoder.get (charset);
- Usesdefaultdeflater = true;
- }
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