Java压缩文件/目次成ZIP包最新能力
利用java自带的类java.util.zip举办文件/目次的压缩的话,有一点不敷,不支持中文的名件/目次定名,假如有中文名,那么打包就会失败。本人颠末一段时间的探索和实践,发此刻一般的Ant.jar类库中也有zip类,import org.apache.tools.zip.*,可以或许办理不支持中文文件目次的问题,同时,Ant的得到途径也较量多,一般的应用处事器中有这个包,实在不可去下载个tomcat5.X,内里也有ant.jar,本人颠末测试,可以利用。
例子如下:
import java.io.File;
import org.apache.tools.zip.ZipOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import dianda.cwmanage.*;
import dianda.com.util.Format;
public class CompressBook {
public CompressBook() {
}
public void zip(String inputFileName) throws Exception {
String zipFileName="c:\\test.zip";//打包后文件名字
System.out.println(zipFileName);
zip(zipFileName, new File(inputFileName));
}
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("zip done");
out.close();
}
private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}
else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
}
独一遗憾,美中不敷的是,无论,java.util.zip可能org.apache.tools.zip都不能解压rar东西打成的rar包,搜了半天也找不到回收java解压rar东西打成的包,渴望哪位高人可以或许提供解压rar东西打成的包,和各人分享一下!