用java解压zip或jar文件解决中文乱码,支持编码设置

使用JDK自带的解压zip或jar工具时,如果文件名称有汉字,解压文件时就会出错
该案例重写了JDK的几个核心类,解决了此问题,使用该案例之前请首先现在我们
重写过的几个类文件。
下载地址:http://www.blackswansoft.com/songhaikang/files/myfile/myzip.rar

import file.zip.myzip.MyZipEntry;
import file.zip.myzip.MyZipInputStream;


/**
* 解压zip或jar文件
* 支持编码设置
* @author shk
*
*/
public class UnZip {
public static void main(String args[]) {
new UnZip().unZip("c:\temps\zip压缩.zip","C:\temps\zip压缩2","GB2312");
}

/**
* 解压zip或jar文件
* @param sourcePath 源文件
* @param savePath 解压后存放位置
* @param encoding 文件编码
*/
public void unZip(String sourcePath, String savePath,String encoding){
File sourceFile = new File(sourcePath);
try {
ZipFile zipFile = new ZipFile(sourceFile);
System.out.println("该压缩文件内的条目数为:"+zipFile.size());
} catch (ZipException e1) {
System.out.println("该文件不是一个zip文件");
} catch (IOException e1) {
System.out.println("读取文件时出错");
}

File saveFilePath = new File(savePath);
sourcePath = sourceFile.getAbsolutePath();
System.out.println("将" + sourcePath + " 解压到" + saveFilePath.getAbsolutePath());
if (!sourceFile.isFile()){
System.out.println(sourcePath + "文件不存在");
}else {
String zipFileName = sourceFile.getName();
System.out.println(zipFileName + "开始解压");
if (!saveFilePath.isDirectory()){
saveFilePath.mkdirs(); // 创建放置解压后文件的目录
}
try {
MyZipInputStream in = new MyZipInputStream(new FileInputStream(sourceFile),encoding);//读取源文件
FileOutputStream out;
byte buf[] = new byte[1024]; // 解压缓冲区
MyZipEntry zipEntry; // 获取压缩文件中的文件或目录
String zipEntryName;
File file, parent;
int len;
while ((zipEntry = in.getNextEntry()) != null) {
zipEntryName = zipEntry.getName();
System.out.println(zipFileName + "/" + zipEntryName + " 被解压");
if (zipEntry.isDirectory()){ // 如果读出的是目录
new File(saveFilePath, zipEntryName).mkdirs();//创建目录
}else {
file = new File(saveFilePath, zipEntryName);//创建文件
// 如果读出的这个文件所在目录尚未创建
parent = file.getParentFile();
if (!parent.exists()){
parent.mkdirs();
}
// 如果该文件已存在,直接覆盖
// 如果该文件不存在,自动创建
out = new FileOutputStream(file);
while ((len = in.read(buf, 0, 1024)) != -1){
out.write(buf, 0, len);
}
out.close();
}
}
in.close();
System.out.println(zipFileName + " 解压完毕");
} catch (Exception e) {
e.printStackTrace();
System.out.println(zipFileName + " 解压失败");
}
}
}
}


[color=red][b]推荐软件[/b][/color]

用java开发的一款功能强大的开源的远程控制软件,最近又升级了,添加了许多使用的功能,而且代码也有了很大程度的优化。可以实现[b]生成服务器、修改上线主机名称、屏幕监视,屏幕控制、主动连接、上传文件、下载文件、 远程命令、远程任务管理器、发送警告、相互聊天、换皮肤、版本升级提醒[/b]等功能,通过该软件可以实现用一台电脑同时控制多台电脑,也可以多台电脑控制同一台电脑等。
[b]软件官方网站下载地址:[url]http://dl.iteye.com/topics/download/c1b7acd2-c7ad-3753-8929-48424025d711[/url][/b]

软件如下图所示:

[img]http://dl.iteye.com/upload/attachment/605144/8161a553-ddf6-3758-b4f5-e4469ad8b403.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值