压缩文件或目录,我知道有三种方式:
1,使用能够java类ZIPOutputStream(乱码问题)
2,ant项目中包含的ZipOutputStream
3,使用ant的构建文件(推荐使用。写个bat或sh脚本执行build.xml,在程序中设置执行脚本的触发条件)
java方式(有乱码问题)
FileOutputStream f = new FileOutputStream("test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("this is a zip test");
BufferedReader re = new BufferedReader(new FileReader("zip.txt"));
zos.putNextEntry(new ZipEntry("zip.txt"));
int c;
while((c = re.read()) != -1){
out.write(c);
}
re.close();
out.flush();
out.close();
org.apache.tools.zip.ZipOutputStream。
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
/**
* 压缩文件
* @param srcPathName
*/
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32()); //不加CRC32,一样可以生成文件。关于数据如何校验,请高手指点
ZipOutputStream out = new ZipOutputStream(cos);
out.setEncoding("gbk"); //如果不加此句,压缩文件依然可以生成,只是在打开和解压的时候,会显示乱码,但是还是会解压出来
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
this.compressDirectory(file, out, basedir);
} else {
this.compressFile(file, out, basedir);
}
}
/** 压缩一个目录 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/** 压缩一个文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("E://文件夹.zip");
zc.compress("E://文件夹测试"); //压缩一个文件夹
}
}
ant(推荐使用)
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="zip" default="init" basedir=".">
<property name="src.dir" value="src" />
<property name="dist.dir" value="dist" />
<target name="init">
<zip destfile="${dist.dir}/src.zip" basedir="${src.dir}"></zip>
</target>
</project>
zip.bat
然后在java中设置脚本文件的触发条件,比如使用quartz或是UI界面上的按钮等