java解压zip包出现乱码
Java  /  管理员 发布于 8年前   356
解决思路:
首先判断需要解压的文件是否存在或路径是否正确,接着判断路径是否存在,若不存在则创建路径,然后判断压缩文件是否合法,最后设置文件名称编码为“GBK”即可。
免费在线教学视频分享:java教学视频
示例代码:
package com.yunfei.fts;import java.io.File;import net.lingala.zip4j.core.ZipFile;import net.lingala.zip4j.model.ZipParameters;import net.lingala.zip4j.util.Zip4jConstants;public class ZipUtil { /** * todo zip解压缩 * @param source 压缩文件全路径 * @param target 要解压路径 * @param targetName 解压文件夹名称 */ public static void unzip (String source,String target,String targetName) throws Exception{ try {File file = new File(source);if(!file.exists() || file.isDirectory()){ throw new Exception("将要解压文件不存在或路径填写不正确!");}file = new File(target+File.separator+targetName);if(!file.exists()){ file.mkdirs(); System.out.println("路劲不存在,创建路径");}ZipFile zipfile = new ZipFile(source);if (!zipfile.isValidZipFile()) { throw new Exception("压缩文件不合法,可能被损坏."); } zipfile.setFileNameCharset("GBK");zipfile.extractAll(target+File.separator+targetName); } catch (Exception e) {e.printStackTrace();throw e; } } /** * todo 生成zip压缩 * @param source 要压缩文件全路径 * @param target 压缩文件存放路径 * @param targetName 解压文件名称 */ public static void zip (String source,String target,String targetName) throws Exception{ try { File file = new File(target);if(!file.exists()){ file.mkdirs(); System.out.println("解压存储路劲不存在,创建路径");}file = new File(source);if(!file.exists()){ throw new Exception("将要解压文件不存在或路径填写不正确!");}ZipFile zipfile = new ZipFile(target+File.separator+targetName);zipfile.setFileNameCharset("GBK");ZipParameters params = new ZipParameters();params.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式 params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别 //zipfile.crif(file.isFile()){ zipfile.addFile(file, params);}else{ zipfile.addFolder(source, params);} } catch (Exception e) {e.printStackTrace();throw e; } } public static void main(String[] args) { try {unzip("d:\\home.zip","e:\\","test");zip("D:\\home","e:\\","test.zip"); } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace(); } }}
推荐java相关文章教程:java开发入门
以上就是java解压zip包出现乱码的详细内容,更多请关注其它相关文章!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号