昨天在研究用apache的ant包来解压zip文件,把经验写下来与大家分享。
写贴上代码
package upzip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.tools.zip.*;
import java.util.Enumeration;
/**
* 压缩解压ZIP文件
* @author Administrator
*
*/
public class AntZip {
private ZipFile zipFile;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes;
/**
*
* @param bufSize 缓存大小
*/
public AntZip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
public AntZip(){
this(1024);
}
/**
* 生存目录
* @param directory 解压文件存放目录
* @param subDirectory 子目录(没有时可传入空字符串)
*/
private void createDirectory(String directory, String subDirectory) {
String dir[];
File fl = new File(directory);
try {
if(subDirectory == "" && fl.exists() != true)
fl.mkdir();
else if(subDirectory != ""){
dir = subDirectory.replace('\\', '/').split("/");
for (int i = 0; i < dir.length; i++) {
File subFile = new File(directory + File.separator + dir[i]);
if (subFile.exists() == false)
subFile.mkdir();
directory += File.separator + dir[i];
}
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 解压指定的ZIP文件
* @param unZipFileName 文件名字符串(包含路径)
* @param outputDirectory 解压后存放目录
*/
public void unZip(String unZipFileName,String outputDirectory){
FileOutputStream fileOut;
//File file;
InputStream inputStream;
try{
createDirectory(outputDirectory,"");
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){
this.zipFile = new ZipFile(unZipFileName,"GBK");
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){
this.zipFile = new ZipFile(unZipFileName,"UTF-8");
}
for(Enumeration entries = this.zipFile.getEntries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
if(entry.isDirectory()){
//是目录,则创建之
String name = entry.getName().substring(0, entry.getName().length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
//file.mkdirs();
}else{
//是文件
String fileName = entry.getName().replace('\\', '/');
if (fileName.indexOf("/") != -1){
createDirectory(outputDirectory,fileName.substring(0, fileName.lastIndexOf("/")));
fileName=fileName.substring(fileName.lastIndexOf("/") + 1,fileName.length());
}
File f = new File(outputDirectory + File.separator + entry.getName());
f.createNewFile();
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(f);
while(( this.readedBytes = inputStream.read(this.buf) ) > 0){
fileOut.write(this.buf , 0 , this.readedBytes );
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 解压指定ZIP文件
* @param unZipFile 需要解压的ZIP文件
*/
public void unZip(File unZipFile){
String outputDirectory = new String("D:/ip/"); //解压后存放目录
unZip(unZipFile.toString(),outputDirectory);
}
}
遇到的做大问题就是中文文件名。原以为ant支持中文,所以没问题,但实际做下来,发现中文文件名会出现乱码。在网上找了好久,发现原来是和系统有关。中文windows系统默认的字符集是GBK,而ant就不知道用什么字符集在读取文件名了(估计是JAVA默认的字符集)。所以导致了文件名乱码。解决的方法就是ZipFile(unZipFileName,"GBK"),后面加个字符集,可以指定用什么字符集解压zip。Linux下有个问题,Linux系统默认的字符集是UTF-8,可以参照windows下的,用UTF-8做解压的字符集。
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){
this.zipFile = new ZipFile(unZipFileName,"GBK");
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){
this.zipFile = new ZipFile(unZipFileName,"UTF-8");
}
没找到统一的决绝方法,待研究。
在Linux下还有问题,就是从windows系统中拷贝过去的文件带中文名的,在linux下应该使用GBK字符集读取,然后转码成UTF-8。不过,的确Linux下对中文的支持太有问题了。
转码代码参考如下地址的帖子
http://jlins.iteye.com/blog/574670
但是如果出现中英文一起的,那就会出现乱码,这个问题没自己看,可能转码代码还不过好。
问题这么多,最后也很无奈,想了个最简单的办法,就是转码后用正则表达式判断是否有中文,如果有,直接换个英文文件名,一切OK。
分享到:
相关推荐
apache-ant-zip.jar 解决中文乱码解压问题
apache-ant-zip.jar包及用法 用过java做压缩或解压的都知道,jdk提供的zip只能按UTF-8格式处理,所有jdk提供的zip不能支持中文文件名,可以采用Apache的zip包解决中文文件名问题。
apache压缩解压zip,支持中文
apache-ant-zip-2.3.jar maven仓库中不存在的jar,可以手动打包到项目中使用
apache 解压zip文件 ant.jar工具包
在使用Java对ZIP压缩文件进行解压的方式中有两种,一种是使用apache提供的ant.jar工具包,但是如果ZIP文件中含有中文名称的文件,在解压后,文件名将出现乱码,另一种是使用Java自身JDK中java.util.zip包下工具类,...
apache-ant-zip.jar这个包有个哥们下载需要48积分,也太不厚道了,我这个下载只需要1积分,看看能不能更加方便大家下载使用。
apache-ant-zip-1.8.0.jar下载
apache-ant-1.9.16-bin.zip
* @param targetPath 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下 * void */ public static void unzip(String zipFilePath, String targetPath) throws ...
apache-ant-1.9.2-bin.zip
apache-ant-1.8.0.zip
apache-ant 1.10.13 版本,解压柯安装apache-ant 1.10.13 版本,解压柯安装apache-ant 1.10.13 版本,解压柯安装apache-ant 1.10.13 版本,解压柯安装apache-ant 1.10.13 版本,解压柯安装apache-ant 1.10.13 版本,...
Apache Ant 1.7.0 is now available for download. Ant 1.7 introduces a resource framework. Some of the core ant tasks such as <copy/> are now able to process not only file system resources but ...
apache-ant-1.6.5-bin 具体详细信息察看readme.txt
apache ant使用例子, 一个可以运行的hello ant的myelipse工程实例。
apache ant安装包 apache ant安装包 apache ant安装包
赠送jar包:ant-1.10.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
apache的一款工具,使用ant 转换 corba的idl文件 生成Java代码。需要配合使用bulid.xml文件。下载后需要配置环境变量,使用。