使用Apache Commons Compress进行文件压缩

使用Apache Commons Compress进行文件压缩

package com.app.common.util.zip;

import java.io.*;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

/**
 * 文件压缩抽象类
 * @author Administrator
 *
 */
public abstract class AbstractZipHandler {

	public static final String ZIP_SUFFIX = ".zip";

	public static final String DEF_DECODING = "GBK";

	private File inputFile;

	private File outFile;

	private FileFilter fileFilter;

	private ZipArchiveOutputStream zipOutput;

	private String encoding;

	public AbstractZipHandler(File inputFile, File outFile) throws IOException {
		if (null == inputFile || !inputFile.exists()|| !inputFile.isDirectory() || !inputFile.canRead()) {
			throw new IllegalArgumentException("The inputFile must be directory and can read.");
		}
		if (false == (outFile.getCanonicalPath().toLowerCase().endsWith(ZIP_SUFFIX))) {
			throw new IllegalArgumentException("The outFile must be zip file.");
		}

		this.inputFile = inputFile;
		this.outFile = outFile;

		zipOutput = new ZipArchiveOutputStream(outFile);
		zipOutput.setEncoding(this.getEncoding());
	}

	public void zip() {
		File[] files = this.getFileFilter() == null ? inputFile.listFiles():inputFile.listFiles(this.getFileFilter());
		try {
			for (File file : files) {
				zipFile(file);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != zipOutput) {
				try {
					zipOutput.close();
				} catch (IOException e) {
				}
				zipOutput = null;
			}

		}
	}

	private void zipFile(File file) throws IOException {
		if (file.isDirectory()) {
			zipOutput.putArchiveEntry(new ZipArchiveEntry(this.getEntryName(
					inputFile, file) + "/"));
			zipOutput.closeArchiveEntry();
			for (File f : this.getFileFilter() == null ? file.listFiles()
					: file.listFiles(this.getFileFilter())) {
				zipFile(f);
			}
		} else {
			zipOutput.putArchiveEntry(new ZipArchiveEntry(this.getEntryName(
					inputFile, file)));
			IOUtils.copy(new FileInputStream(file), zipOutput);
			zipOutput.closeArchiveEntry();
		}
	}

	public String getEntryName(File inputFile, File entryFile)
			throws IOException {
		String result = null;
		String inputFileParentPath = inputFile.getParentFile()
				.getCanonicalPath();
		String entryFilePath = entryFile.getCanonicalPath();
		result = entryFilePath.substring(entryFilePath
				.indexOf(inputFileParentPath) + inputFileParentPath.length());
		return result;
	}

	public FileFilter getFileFilter() {
		return fileFilter;
	}

	public void setFileFilter(FileFilter fileFilter) {
		this.fileFilter = fileFilter;
	}

	public String getEncoding() {
		return null == encoding ? DEF_DECODING : encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

}



package com.app.common.util.zip;

import java.io.File;
import java.io.IOException;

/**
 * 文件压缩默认实现类
 * @author Administrator
 *
 */
public class DefZipHandler extends AbstractZipHandler {

	public DefZipHandler(File inputFile, File outFile) throws IOException {
		super(inputFile, outFile);
	}

	
}



package com.app.common.util.zip;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

/**
 * 文件压缩测试类
 * @author Administrator
 *
 */
public class TestDefZipHandler {

	public static void main(String[] args) throws IOException {

		File inputFile = new File("c:/test/");

		File outFile = new File("c:/test.zip");

		AbstractZipHandler handler = new DefZipHandler(inputFile, outFile);

		handler.setFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				return true;
			}
		});

		handler.zip();

		System.out.println("finished...");
	}
}

你可能感兴趣的:(使用Apache Commons Compress进行文件压缩)