1.基本解压,压缩操作
package com.jason.zip;
import com.jason.j2ee.encode.ChangeCharset;
import org.apache.tools.zip.ZipOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by IntelliJ IDEA.
* User: jason
* Date: 2010-6-23
* Time: 15:27:21
*/
public class CompressToZip {
public void zip(String inputFileName) throws Exception {
String zipFileName = "d:\\批量打包.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));
out.setEncoding(ChangeCharset.UTF_8);
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();
System.out.println("新增目录元素 " +base+ "/");
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 {
String fileName = ChangeCharset.changeCharset(base, ChangeCharset.UTF_8);
System.out.println("新增普通文件元素 " +fileName);
out.putNextEntry(new org.apache.tools.zip.ZipEntry(fileName));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
public static void main(String[] args) {
CompressToZip test = new CompressToZip();
try {
test.zip("d:\\tmp");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.基于Java Zip技术压缩目录或文件
package com.jason.zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 基于Java Zip技术压缩目录或文件.
* 该压缩技术支持多级目录的压缩,使用递归算法.
* 问题是不支持中文,如果想要支持中文,请使用Ant的Zip技术.
*
* Created by IntelliJ IDEA.
* User: jason
* Date: 2010-6-23
* Time: 15:22:46
*/
public class ZipMultiDirectoryCompress {
/**
* @param args
*/
public static void main(String[] args) {
//初始化支持多级目录压缩的ZipMultiDirectoryCompress
ZipMultiDirectoryCompress zipCompress = new ZipMultiDirectoryCompress();
//压缩目录,可以指向一个文件
String directory = "C:/computer";
//生成的压缩文件
String destFile = "c:/computer.zip";
//默认的相对地址,为根路径
String defaultParentPath = "";
ZipOutputStream zos = null;
try {
//创建一个Zip输出流
zos = new ZipOutputStream(new FileOutputStream(destFile));
//启动压缩进程
zipCompress.startCompress(zos,defaultParentPath,directory);
} catch (FileNotFoundException e){
e.printStackTrace();
} finally{
try {
if(zos != null)zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 开始压缩.
*
* @param zos ZipOutputStream
* @param oppositePath 相对路径,开始时相对路径为空
* @param directory 压缩目录,也可能是文件
*/
public void startCompress(ZipOutputStream zos, String oppositePath, String directory){
File file = new File(directory);
if(file.isDirectory()){
//如果是压缩目录
File[] files = file.listFiles();
for(int i=0;i<files.length;i++){
File aFile = files[i];
if(aFile.isDirectory()){
//如果是目录,修改相对地址
String newOppositePath = oppositePath + aFile.getName() + "/";
//创建目录
compressDirectory(zos, oppositePath, aFile);
//进行递归调用
startCompress(zos,newOppositePath,aFile.getPath());
} else {
//如果不是目录,则进行压缩
compressFile(zos,oppositePath,aFile);
}
}
} else {
//如果是压缩文件,直接调用压缩方法进行压缩
compressFile(zos,oppositePath,file);
}
}
/**
* 压缩文件.
*
* @param zos ZipOutputStream
* @param oppositePath 相对于路路径
* @param file 压缩的文件
*/
public void compressFile(ZipOutputStream zos, String oppositePath, File file){
//创建一个Zip条目,每个Zip条目都是必须相对于根路径
ZipEntry entry = new ZipEntry(oppositePath + file.getName());
InputStream is = null;
try{
//将条目保存到Zip压缩文件当中
zos.putNextEntry(entry);
//从文件输入流当中读取数据,并将数据写到输出流当中.
is = new FileInputStream(file);
int length = 0;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while((length=is.read(buffer,0,bufferSize))>=0){
zos.write(buffer, 0, length);
}
zos.closeEntry();
}catch(IOException ex){
ex.printStackTrace();
} finally {
try{
if(is != null)is.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
/**
* 在压缩文件当中创建一个新的压缩目录,支持空目录.
*
* @param zos
* @param oppositePath
* @param file
*/
public void compressDirectory(ZipOutputStream zos, String oppositePath, File file){
//压缩目录,这是关键,创建一个目录的条目时,需要在目录名后面加多一个"/"
ZipEntry entry = new ZipEntry(oppositePath + file.getName() + "/");
try {
zos.putNextEntry(entry);
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
}