使用apache的ant.jar进行压缩/解压缩文件

[color=blue]windows系统默认字符集为gbk,linux默认为utf-8,使用前视情况设定字符集。后来发现一个问题。当设定字符集为gbk后压缩一个文件在windows系统用winRAR打开只能显示非中文的文件或文件夹,但是手动解压后文件全部正常,用7zip打开全部正常。文章结尾有使用的ant.jar包。[/color]


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class CompressFile {

public final static String encoding = "gbk";
private static CompressFile instance = new CompressFile();
private CompressFile() {
}
public static CompressFile getInstance() {
return instance;
}

/**
* 压缩文件或者文件目录到指定的zip或者rar包
* @param inputFilename 要压缩的文件或者文件夹,如果是文件夹的话,会将文件夹下的所有文件包含子文件夹的内容进行压缩
* @param zipFilename 生成的zip或者rar文件的名称
* @throws IOException
*/
public synchronized void zip(String inputFilename, String zipFilename)
throws Exception {
zip(new File(inputFilename), zipFilename);
}

/**
* 压缩文件或者文件目录到指定的zip或者rar包,内部调用
* @param inputFile 参数为文件类型的要压缩的文件或者文件夹
* @param zipFilename 生成的zip或者rar文件的名称
* @throws IOException
*/
private synchronized void zip(File inputFile, String zipFilename) throws Exception {
if (!inputFile.exists())
return;
//isNewFile用于判断是否是文件压缩,当时文件压缩时压缩完毕后删除创建的临时文件夹
boolean isNewFile = false;
if (!inputFile.isDirectory()) {
//创建文件夹
File file = new File(inputFile.getAbsolutePath().substring(0, inputFile.getAbsolutePath().lastIndexOf(".")));
file.mkdirs();
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
//将文件信息拷贝到新建文件夹下
try {
File fileTmp = new File(file,inputFile.getName());
inBuff = new BufferedInputStream(new FileInputStream(inputFile));
outBuff = new BufferedOutputStream(new FileOutputStream(fileTmp));
byte[] b = new byte[1024];
int len;
while ((len=inBuff.read(b))!=-1) {
outBuff.write(b);
}
outBuff.flush();
} finally{
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
inputFile = file;
isNewFile = true;
}
//压缩文件
Project project = new Project();
Zip zip = new Zip();
zip.setProject(project);
zip.setDestFile(new File(zipFilename));
//创建文件组
FileSet fileSet = new FileSet();
fileSet.setProject(project);
fileSet.setDir(inputFile);
zip.addFileset(fileSet);
zip.setEncoding(CompressFile.encoding);
zip.execute();
if (isNewFile) {
this.deleteDirectory(inputFile);
}
}

/**
* 解压缩zip包
* @param zipFilePath zip文件路径
* @param targetPath 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
* @throws IOException
*/
public synchronized void unzip(String zipFilePath, String targetPath) throws IOException {
OutputStream os = null;
InputStream is = null;
ZipFile zipFile = null;
try {
//获取需要解压缩的文件
zipFile = new ZipFile(zipFilePath,CompressFile.encoding);
String directoryPath = "";
if (null == targetPath || "".equals(targetPath)) {
directoryPath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
} else {
directoryPath = targetPath;
}
//获取压缩文件中的子目录
Enumeration entryEnum = zipFile.getEntries();
if (null != entryEnum) {
ZipEntry zipEntry = null;
while (entryEnum.hasMoreElements()) {
zipEntry = (ZipEntry) entryEnum.nextElement();
if (zipEntry.isDirectory()) {
continue;
}
if (zipEntry.getSize() > 0) {
// 文件
File targetFile = this.buildFile(directoryPath + File.separator + zipEntry.getName(), false);
os = new BufferedOutputStream(new FileOutputStream(targetFile));
is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[4096];
int readLen = 0;
while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
os.write(buffer, 0, readLen);
}
os.flush();
os.close();
} else {
// 空目录
this.buildFile(directoryPath + File.separator+ zipEntry.getName(), true);
}
}
}
} catch (IOException ex) {
throw ex;
} finally {
if(null != zipFile){
zipFile = null;
}
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
}
}

/**
* 生产文件 如果文件所在路径不存在则生成路径
* @param fileName 文件名 带路径
* @param isDirectory 是否为路径
* @return
*/
private File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
target = new File(target.getAbsolutePath());
}
}
return target;
}

/**
* 删除文件 文件必须存在
* @param file
* @return
*/
private boolean deleteFile(File file) throws Exception {
boolean flag = false;
if (file.isFile() && file.exists()) {
flag = file.delete();
}
return flag;
}

/**
* 删除文件夹 文件夹必须存在
* @param file
* @return
*/
private boolean deleteDirectory(File file) throws Exception {
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i]);
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(files[i]);
if (!flag) break;
}
}
if (!flag)
return false;
//删除当前目录
if (file.delete())
return true;
else
return false;
}

public static void main(String[] args) {
CompressFile bean = new CompressFile();
try {
//测试压缩文件
bean.zip("D:\\新建 文本文档.txt", "d:/测试.zip");
bean.zip("D:\\新建文件夹", "d:/新建文件夹.zip");
bean.zip("D:\\新建文件夹1", "d:/新建文件夹1.zip");
//测试解压缩文件
bean.unzip("d:\\测试.zip", "d:");
bean.unzip("d:\\新建文件夹.zip", "d:");
bean.unzip("d:/新建文件夹1.zip", "d:");
} catch (Exception e) {
e.printStackTrace();
}
}

}

你可能感兴趣的:(Java,ant,压缩,解压缩)