java实现文件及目录压缩

时间:2022-03-15 15:31:37
package org.alfresco.repo.bom.util;

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 org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream; /**
* Compressor Util
* @author HJ
*
*/
public class CompressorUtil { private static final String source = "F:/test"; // wait compressor source path
private static final String zipSource = "F:/chiang.zip"; // after compressor zip file path
private static long startTime;// compressor start system time
private static long endTime;// compressor end system time public void compressor() throws Exception{
startTime = System.currentTimeMillis();//record start compressor system time ,
boolean flag = false;// flag :true->compressor success
String baseDir = "";//defalut relative Dir , "" is gen Dir File s = new File(source);
File zs = new File(zipSource);//create zip file
if (zs.exists()) {// if this dir exists this zip file
zs.delete(); // delete this zip file ,
}
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zs));
zos.setEncoding("GBK"); // solve Chinese garbled
startCompressor(baseDir, zos, s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (zos!=null)
zos.close();
endTime = System.currentTimeMillis();
System.out.println("compressor success,use time:"+(endTime-startTime)+"ms");
}
} public void startCompressor(String baseDir,ZipOutputStream zos,File source) throws Exception{
if (source.isFile()) {// is file
toCompressedFile(baseDir, zos, source);
}
if (source.isDirectory()) { //is dir
File[] sources = source.listFiles(); // get dir all files ( file or dir)
for(File f:sources){
if (f.isFile()) {// is file
toCompressedFile(baseDir, zos, f);
}
if (f.isDirectory()) {//is dir
// if is dir , update baseDir value .
String newBaseDir = baseDir + f.getName() + "/";
createCompressedDir(baseDir, zos, f);//create dir and entry
startCompressor(newBaseDir, zos, f); // Re
}
}
}
}
/**
* add entry to zip file by stream way
* @param baseDir
* @param zos
* @param f
* @throws Exception
*/
public void toCompressedFile(String baseDir,ZipOutputStream zos,File f) throws Exception{
InputStream input = null;
ZipEntry z = new ZipEntry(baseDir+f.getName());
try {
zos.putNextEntry(z); // add entry to zip file
input = new FileInputStream(f);
int data = 0;
while ((data=input.read())!=-1) {
zos.write(data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(input!=null)
input.close();
zos.closeEntry();
}
} /**
* create compressed file dir and ZipEntry
* @param baseDir
* @param zos zip file's ZipOutputStream
* @param f
*/
public void createCompressedDir(String baseDir,ZipOutputStream zos,File f){
ZipEntry z = new ZipEntry(baseDir+f.getName()+"/");
try {
zos.putNextEntry(z);
zos.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //test main method
public static void main(String[] args) throws Exception{
CompressorUtil cu = new CompressorUtil();
cu.compressor();
}
}