자바 zip 도구 클래스

7652 단어 zip 도구
jar: apache - ant - 1.9.2 - bin. zip 의존
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
 * zip  
 *            
 * @author Sun Qinbo
 * @version 1.0, 2013-7-26
 */
public class ZipUtils {
	private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);
	private ZipOutputStream zipOut;
	private static byte[] buf = new byte[1024];

	/**
	 *      
	 */
	public static void zip(OutputStream out, List<FileEntry> fileEntrys, String encoding) {
		new ZipUtils(out, fileEntrys, encoding);
	}

	/**
	 *      
	 */
	public static void zip(OutputStream out, List<FileEntry> fileEntrys) {
		new ZipUtils(out, fileEntrys, null);
	}

	/**
	 *            
	 * @param srcFiles      
	 * @param out      
	 * @param filter     ,      null。
	 * @param parent        ,      null。
	 * @param prefix     ,      null。
	 * @param encoding    ,        。
	 */

	public static void zip(File[] srcFiles, OutputStream out, FilenameFilter filter, String parent, String prefix, String encoding) {
		Assert.notEmpty(srcFiles);
		List<FileEntry> fileEntrys = new ArrayList<FileEntry>();
		for (int i = 0; i < srcFiles.length; i++) {
			FileEntry fileEntry = new FileEntry(parent, prefix, srcFiles[i], filter);
			fileEntrys.add(fileEntry);
		}
		new ZipUtils(out, fileEntrys, encoding);
	}

	/**
	 *   ZipUtils  
	 * @param out    
	 * @param filter     ,      null。
	 * @param fileEntrys     。        ,        ,            。
	 */
	protected ZipUtils(OutputStream out, List<FileEntry> fileEntrys, String encoding) {
		Assert.notEmpty(fileEntrys);
		long begin = System.currentTimeMillis();
		log.debug("       ");
		try {
			try {
				zipOut = new ZipOutputStream(out);
				if (!StringUtils.isBlank(encoding)) {
					log.debug("using encoding: {}", encoding);
					zipOut.setEncoding(encoding);
				} else {
					log.debug("using default encoding");
				}
				for (FileEntry fe : fileEntrys) {
					zip(fe.getFile(), fe.getFilter(), fe.getZipEntry(), fe.getPrefix());
				}
			} finally {
				zipOut.close();
			}
		} catch (IOException e) {
			throw new RuntimeException("      ,  IO  !", e);
		}
		long end = System.currentTimeMillis();
		log.info("       。  :{}ms。", end - begin);
	}

	/**
	 *     
	 * @param srcFile    
	 * @param pentry        ,      null。
	 * @param prefix     
	 * @throws IOException
	 */
	private void zip(File srcFile, FilenameFilter filter, ZipEntry pentry, String prefix) throws IOException {
		ZipEntry entry;
		if (srcFile.isDirectory()) {
			if (pentry == null) {
				entry = new ZipEntry(srcFile.getName());
			} else {
				entry = new ZipEntry(pentry.getName() + "/" + srcFile.getName());
			}
			File[] files = srcFile.listFiles(filter);
			for (File f : files) {
				zip(f, filter, entry, prefix);
			}
		} else {
			if (pentry == null) {
				entry = new ZipEntry(prefix + srcFile.getName());
			} else {
				entry = new ZipEntry(pentry.getName() + "/" + prefix + srcFile.getName());
			}
			FileInputStream in;
			try {
				log.debug("    :{}", srcFile.getAbsolutePath());
				in = new FileInputStream(srcFile);
				try {
					zipOut.putNextEntry(entry);
					int len;
					while ((len = in.read(buf)) > 0) {
						zipOut.write(buf, 0, len);
					}
					zipOut.closeEntry();
				} finally {
					in.close();
				}
			} catch (FileNotFoundException e) {
				throw new RuntimeException("      ,      :" + srcFile.getAbsolutePath(), e);
			}
		}
	}

	/**
	 *   
	 * @param zipFile      
	 * @param destDir     
	 * @param encoding
	 * @author Sun Qinbo
	 */
	public static void unzip(File zipFile, File destDir, String encoding) {
		long begin = System.currentTimeMillis();
		log.debug("      ");
		if (destDir.exists() && !destDir.isDirectory()) {
			throw new IllegalArgumentException("destDir is not a directory!");
		}
		ZipFile zip = null;
		InputStream is = null;
		FileOutputStream fos = null;
		File file;
		String name;
		int readed;
		ZipEntry entry;
		try {
			try {
				if (StringUtils.isNotBlank(encoding)) {
					zip = new ZipFile(zipFile, encoding);
				} else {
					zip = new ZipFile(zipFile);
				}
				Enumeration<?> en = zip.getEntries();
				while (en.hasMoreElements()) {
					entry = (ZipEntry) en.nextElement();
					name = entry.getName();
					name = name.replace('/', File.separatorChar);
					file = new File(destDir, name);
					if (entry.isDirectory()) {
						file.mkdirs();
					} else {
						//      
						file.getParentFile().mkdirs();
						is = zip.getInputStream(entry);
						fos = new FileOutputStream(file);
						while ((readed = is.read(buf)) > 0) {
							fos.write(buf, 0, readed);
						}
						fos.close();
						is.close();
					}
				}
			} finally {
				if (fos != null) {
					fos.close();
				}
				if (is != null) {
					is.close();
				}
				if (zip != null) {
					zip.close();
				}
			}
		} catch (IOException e) {
			log.error("", e);
		}
		long end = System.currentTimeMillis();
		log.info("      。  :{}ms。", end - begin);

	}

	/**    */
	public static void main(String[] args) throws IOException {
		List<FileEntry> fileEntrys = new ArrayList<FileEntry>();
		File[] listFiles = new File("d://test").listFiles();
		for (int i = 0; i < listFiles.length; i++) {
			fileEntrys.add(new FileEntry("", "", listFiles[i]));
		}
		ZipUtils.zip(new FileOutputStream("D://  _1.zip"), fileEntrys);
		ZipUtils.zip(new File("d://test").listFiles(), new FileOutputStream("D://  _2.zip"), null, "      ", "     _", "UTF-8");
		ZipUtils.unzip(new File("D://  _2.zip"), new File("D://  _2"), null);
	}

	/**
	 *         
	 */
	public static class FileEntry {
		private FilenameFilter filter;
		private String parent; //         
		private File file;
		private String prefix; //       

		public FileEntry(String parent, String prefix, File file, FilenameFilter filter) {
			this.parent = parent;
			this.prefix = prefix;
			this.file = file;
			this.filter = filter;
		}

		/**
		 * @param parent         
		 * @param file       
		 */
		public FileEntry(String parent, File file) {
			this.parent = parent;
			this.file = file;
		}

		public FileEntry(String parent, String prefix, File file) {
			this(parent, prefix, file, null);
		}

		public ZipEntry getZipEntry() {
			if (StringUtils.isBlank(parent)) {
				return null;
			} else {
				return new ZipEntry(parent);
			}
		}

		public FilenameFilter getFilter() {
			return filter;
		}

		public void setFilter(FilenameFilter filter) {
			this.filter = filter;
		}

		public String getParent() {
			return parent;
		}

		public void setParent(String parent) {
			this.parent = parent;
		}

		public File getFile() {
			return file;
		}

		public void setFile(File file) {
			this.file = file;
		}

		public String getPrefix() {
			if (prefix == null) {
				return "";
			} else {
				return prefix;
			}
		}

		public void setPrefix(String prefix) {
			this.prefix = prefix;
		}
	}
}

좋은 웹페이지 즐겨찾기