使用Apache Commons Compress进行文件解压

使用Apache Commons Compress进行文件解压

package com.app.common.util.zip;

import java.io.*;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

/**
 * 抽象文件解压类
 * @author Administrator
 *
 */
public abstract class AbstractUnZipHandler {

	public static final String DEF_ENCODING = "GBK";

	public static final String FOLDER_SUFFIX = "/";

	private String zipFilePath;

	private File zipFile;

	private String encoding;

	private boolean useUnicodeExtraFields;

	private ZipFile zf;

	private long start = 0;

	private long end = 0;

	private boolean global_result;
	
	public AbstractUnZipHandler(String zipFilePath) throws IOException {
		this(zipFilePath, DEF_ENCODING);
	}

	public AbstractUnZipHandler(File zipFile) throws IOException {
		this(zipFile, DEF_ENCODING);
	}

	public AbstractUnZipHandler(String zipFilePath, String encoding)
			throws IOException {
		this(new File(zipFilePath), encoding);
	}

	public AbstractUnZipHandler(File zipFile, String encoding)
			throws IOException {
		this.zipFile = zipFile;
		this.encoding = encoding;
		this.zf = new ZipFile(zipFile, encoding);
	}

	public AbstractUnZipHandler(File zipFile, String encoding,
			boolean useUnicodeExtraFields) throws IOException {
		this.zipFile = zipFile;
		this.encoding = encoding;
		this.useUnicodeExtraFields = useUnicodeExtraFields;
		this.zf = new ZipFile(zipFile, encoding, useUnicodeExtraFields);
	}

	public boolean unZip() {
		boolean result = true;
		doStrart();
		Enumeration<ZipArchiveEntry> zipEntries = zf.getEntries();
		ZipArchiveEntry zipEntry = null;
		String zipEntryName = null;
		while (zipEntries.hasMoreElements()) {
			zipEntry = zipEntries.nextElement();
			zipEntryName = zipEntry.getName();
			if (zf.canReadEntryData(zipEntry)) {
				if (zipEntryName.endsWith(FOLDER_SUFFIX)) {
					// Directory
					if (handleDirectory(zipEntry) == false) {
						result = false;
					}
				} else {
					// File
					if (handleFile(zf, zipEntry) == false) {
						result = false;
					}
				}
			} else {
				// May return false if it is set up to use encryption or a
				// compression method that hasn't been implemented yet.
				result = false;
			}
			if (result == false) {
				System.out.println("can't read zipEntry:" + zipEntryName);
				System.out.println("programe will exist.");
				break;
			}
		}
		if(null!=zf){
			try {
				zf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			zf= null;
		}
		global_result = result;
		doEnd();
		return result;
	}

	public abstract boolean handleFile(ZipFile zf, ZipArchiveEntry zipEntry);

	public abstract boolean handleDirectory(ZipArchiveEntry zipEntry);

	public void doStrart() {
		System.out.println("start handle: " + zipFile.getAbsolutePath());
		start = System.currentTimeMillis();
	}

	public void doEnd() {
		System.out.println("global_result: "+global_result);
		System.out.println("handle over "+(global_result==true?"success":"failed")+": " + zipFile.getAbsolutePath());
		end = System.currentTimeMillis();
		System.out.println("total take: " + (end - start) + " ms.");
		if(false==global_result){
			handleUnZipFailed();
		}
	}

	public abstract void handleUnZipFailed();

	public String getZipFilePath() {
		return zipFilePath;
	}

	public File getZipFile() {
		return zipFile;
	}

	public String getEncoding() {
		return encoding;
	}

	public boolean isUseUnicodeExtraFields() {
		return useUnicodeExtraFields;
	}

}


package com.app.common.util.zip;

import java.io.*;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

/**
 * 抽象文件解压类
 * @author Administrator
 *
 */
public abstract class AbstractUnZipHandler {

	public static final String DEF_ENCODING = "GBK";

	public static final String FOLDER_SUFFIX = "/";

	private String zipFilePath;

	private File zipFile;

	private String encoding;

	private boolean useUnicodeExtraFields;

	private ZipFile zf;

	private long start = 0;

	private long end = 0;

	private boolean global_result;
	
	public AbstractUnZipHandler(String zipFilePath) throws IOException {
		this(zipFilePath, DEF_ENCODING);
	}

	public AbstractUnZipHandler(File zipFile) throws IOException {
		this(zipFile, DEF_ENCODING);
	}

	public AbstractUnZipHandler(String zipFilePath, String encoding)
			throws IOException {
		this(new File(zipFilePath), encoding);
	}

	public AbstractUnZipHandler(File zipFile, String encoding)
			throws IOException {
		this.zipFile = zipFile;
		this.encoding = encoding;
		this.zf = new ZipFile(zipFile, encoding);
	}

	public AbstractUnZipHandler(File zipFile, String encoding,
			boolean useUnicodeExtraFields) throws IOException {
		this.zipFile = zipFile;
		this.encoding = encoding;
		this.useUnicodeExtraFields = useUnicodeExtraFields;
		this.zf = new ZipFile(zipFile, encoding, useUnicodeExtraFields);
	}

	public boolean unZip() {
		boolean result = true;
		doStrart();
		Enumeration<ZipArchiveEntry> zipEntries = zf.getEntries();
		ZipArchiveEntry zipEntry = null;
		String zipEntryName = null;
		while (zipEntries.hasMoreElements()) {
			zipEntry = zipEntries.nextElement();
			zipEntryName = zipEntry.getName();
			if (zf.canReadEntryData(zipEntry)) {
				if (zipEntryName.endsWith(FOLDER_SUFFIX)) {
					// Directory
					if (handleDirectory(zipEntry) == false) {
						result = false;
					}
				} else {
					// File
					if (handleFile(zf, zipEntry) == false) {
						result = false;
					}
				}
			} else {
				// May return false if it is set up to use encryption or a
				// compression method that hasn't been implemented yet.
				result = false;
			}
			if (result == false) {
				System.out.println("can't read zipEntry:" + zipEntryName);
				System.out.println("programe will exist.");
				break;
			}
		}
		if(null!=zf){
			try {
				zf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			zf= null;
		}
		global_result = result;
		doEnd();
		return result;
	}

	public abstract boolean handleFile(ZipFile zf, ZipArchiveEntry zipEntry);

	public abstract boolean handleDirectory(ZipArchiveEntry zipEntry);

	public void doStrart() {
		System.out.println("start handle: " + zipFile.getAbsolutePath());
		start = System.currentTimeMillis();
	}

	public void doEnd() {
		System.out.println("global_result: "+global_result);
		System.out.println("handle over "+(global_result==true?"success":"failed")+": " + zipFile.getAbsolutePath());
		end = System.currentTimeMillis();
		System.out.println("total take: " + (end - start) + " ms.");
		if(false==global_result){
			handleUnZipFailed();
		}
	}

	public abstract void handleUnZipFailed();

	public String getZipFilePath() {
		return zipFilePath;
	}

	public File getZipFile() {
		return zipFile;
	}

	public String getEncoding() {
		return encoding;
	}

	public boolean isUseUnicodeExtraFields() {
		return useUnicodeExtraFields;
	}

}



package com.app.common.util.zip;

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

/**
 * 解压测试类
 * @author Administrator
 *
 */
public class TestDefUnZipHandler {

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

		File zipFile = new File("c:/btv5.zip");
		
		DefUnZipHandler handler = new DefUnZipHandler(zipFile);
		
		handler.setBaseDir("c:/unzip/");
		
		handler.unZip();
	}

}

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