国庆假期,宅呀~ 无意中看到一篇java压缩算法的文章,就顺便度娘了一些相关文章来看。本来想深入研究一下算法,但是,毕竟是假期,哪有那份心思啊,就实际应用简单Memo一下吧。 1. 中文乱码问题 其实这是无意中看到的,度娘一下漫天都是,看来各位程序猿还是以务实的 ...
国庆假期,宅呀~ 无意中看到一篇java压缩算法的文章,就顺便度娘了一些相关文章来看。
本来想深入研究一下算法,但是,毕竟是假期,哪有那份心思啊,就实际应用简单Memo一下吧。
1. 中文乱码问题
其实这是无意中看到的,度娘一下漫天都是,看来各位程序猿还是以务实的应用为多啊。
度娘里,99%的帖子都是说用 Ant中的 org.apache.tools.zip.* 来代替 java.util.zip.*
这种办法来解决中文乱码问题。究其原因是,jdk中写死了UTF-8编码,而操作系统的文件编码如果不是UTF-8,如中文Windows下的GBK,则压缩、解压出的文件名就会出现乱码。
因为,org.apache.tools.zip.ZipOuputStream 有setEncoding(String) 方法,因此可以解决。
- zos.setEncoding("GBK"); // zos 是 ZipOuputStream 对象
- zos.setEncoding(System.getProperties("sun.jnu.encoding")); // 这样比上面更高大上(稳妥)吧
压缩时,(不知道什么原因?)Ant也没有提供 org.apache.tools.zip.ZipInputStream 这个类,所以只能使用JDK自带的类了。
- System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); // 上面是解压,那么在压缩之前,这样设置一下,更安全一些,很多帖子里没有提到哦
即便在度娘中输入关键字“1.7”,也搜不出来的,就是其实JDK1.7已经可以设置字符集了。
Java代码
- /**
- * Creates a new ZIP output stream.
- *
- * @param out the actual output stream
- *
- * @param charset the {@linkplain java.nio.charset.Charset charset}
- * to be 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;
- }
(黄道越长越好,速度快;绿道越短越好,文件体积小)
此文作者,优选 LZ4.FastNative,其次是Deflate(M=1)
这里我想说明两点
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。