文件夹压缩与解压:
利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径并且提供了测试类
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
/** 利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径
* @Title:
* @Description:ZipCompress
* @Version 1.2
*/
public class ZipCompress {
private static boolean isCreateSrcDir = true;//是否创建源目录
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String src = "D:/组合件级/2";//指定压缩源,可以是目录或文件
String decompressDir = "D:/test/最终目的";//解压路径
String archive = "D:/组合件级/2.zip";//压缩包路径
String comment = "无";//压缩包注释
//----压缩文件或目录
writeByApacheZipOutputStream(src,archive,comment);
/*
* 读压缩文件,注释掉,因为使用的是apache的压缩类,所以使用java类库中
* 解压类时出错,这里不能运行
*/
// readByZipInputStream(archive, decompressDir);
//----使用apace ZipFile读取压缩文件
// readByApacheZipFile(archive, decompressDir);
}
/**对文件夹或者文件进行压缩
*
* @Time 2012-3-9 上午09:32:35 create
* @param src
* @param archive
* @param comment
* @throws FileNotFoundException
* @throws IOException
* @author jiangzhenming
*/
public static void writeByApacheZipOutputStream(String src, String archive,
String comment) throws FileNotFoundException, IOException {
//----压缩文件:
FileOutputStream f = new FileOutputStream(archive);
//使用指定校验和创建输出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream zos = new