org.apache.tools.zip这个其实就在sdk你面找到:C:\Sun\SDK\lib\ant\lib里面的ant.jar。
应用里面对文件,文件夹加压解压,用gb2312编码,解决文件名称乱码问题。
先来看看这个类:
package hellowordapp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
*
* @author gdcic-zhang
*/
public class Zip {
private static Zip instance = new Zip();
public Zip() {
}
public static Zip getInstance() {
return instance;
}
public synchronized void zip(String inputFilename, String zipFilename)
throws IOException {
System.out.println(inputFilename);
zip(new File(inputFilename), zipFilename);
}
public synchronized void zip(File inputFile, String zipFilename) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
new String(zipFilename.getBytes("gb2312"))));
out.setEncoding("gb2312");
try {
zip(inputFile, out, "");
} catch (IOException e) {
throw e;
} finally {
out.close();
}
}
private synchronized void zip(File inputFile, ZipOutputStream out, String base)
throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
//解压
public synchronized void unzip(String zipFilename, String outputDirectory)
throws IOException {
File outFile = new File(outputDirectory);
if (!outFile.exists()) {
outFile.mkdirs();
}
ZipFile zipFile = new ZipFile(zipFilename);
Enumeration en = zipFile.getEntries();
ZipEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + File.separator + dirName);
f.mkdirs();
} else {
// unzip file
File f = new File(outFile.getPath() + File.separator
+ zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
try {
int c;
byte[] by = new byte[BUFFEREDSIZE];
//while ((c = in.read(by)) != -1) {
// out.write(by, 0, c);
// }
while ((c = in.read(by))>0){
out.write(by, 0 , c);
}
// out.flush();
} catch (IOException e) {
throw e;
} finally {
out.close();
in.close();
}
}
}
}
private static final int BUFFEREDSIZE = 1024;
}
这里是测试:(可以将文件变成文件夹,就加压文件夹了)
zip bean = new Zip();
try {
bean.zip("e:/见天rar.txt", "e:/test.zip");
bean.unzip("e:/test.zip", "e:/out/temp");
} catch (IOException e) {
e.printStackTrace();
}
分享到:
相关推荐
ant的配置文件ant的配置文件ant的配置文件ant的配置文件
Ant打包和Hibernate文件生成实例,Ant打包和Hibernate文件生成实例
一个普通java程序用ant打包的列子,完整的,里面采用的是eclipse自带的编译插件进行的打包,需要结合eclipse中的插件一起使用
使用ant批量打包,替换渠道号,文件中包含所用到的所有资源。
利用ant.jar解压缩zip包的工具类,内含ant.jar包,可以解决文件为中文的问题,大家随便看看
mac下是用ant对android App的多渠道打包工具。
使用ant.jar工具通过java程序来解压tar包文件,支持中文文件名。
java项目ant打包脚本,包括环境变量设置,打包脚本等信息,共有三个文件 ant.bat、build.xml、setEnv.cmd
使用ant打包android,使用ant打包android
JAVA ANT包发布工具包
ant 打包
,ant包,在tomcat6.0源码编译时要使用的
ant打包android工程,手动打包apk文件
ant打包详细教程
NULL 博文链接:https://lkjust08.iteye.com/blog/439950
使用Ant构建批量压缩js、css文件脚本
ant 涉及第三方包问题 打包第三方包 或者添加第三方包 ant 涉及第三方包问题 打包第三方包 或者添加第三方包 ant 涉及第三方包问题 打包第三方包 或者添加第三方包 ant 涉及第三方包问题 打包第三方包 或者添加第三...
非maven项目中的ANT打包可执行JAR包脚本,包括配置文件、第三方jar打包
ant的介绍pdf文件 ant的介绍pdf文件 ant的介绍pdf文件
ANT打包命令详解api指令 ant java 命令 构建工具 api