java decompress zip

package com.activiti.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

	/**
	 * Compression
	 *
	 * @param src
	 * source file or directory
	 * @param dest
	 * compressed file path
	 * @throws IOException
	 */
	public static void zip(String src, String dest) throws IOException {
		ZipOutputStream out = null;
		try {
			File outFile = new File(dest);
			out = new ZipOutputStream(outFile);
			File fileOrDirectory = new File(src);

			if (fileOrDirectory.isFile()) {
				zipFileOrDirectory(out, fileOrDirectory, "");
			} else {
				File[] entries = fileOrDirectory.listFiles();
				for (int i = 0; i < entries.length; i++) {
					// recursively compress, update curPaths
					zipFileOrDirectory(out, entries[i], "");
				}
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	/**
	 * Recursively compress files or directories
	 *
	 * @param out
	 * Compressed output stream object
	 * @param fileOrDirectory
	 * The file or directory object to compress
	 * @param curPath
	 * The path of the current compressed entry, used to specify the prefix of the entry name
	 * @throws IOException
	 */
	private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath)
			throws IOException {
		FileInputStream in = null;
		try {
			if (!fileOrDirectory.isDirectory()) {
				// Compressed file
				byte[] buffer = new byte[4096];
				int bytes_read;
				in = new FileInputStream(fileOrDirectory);

				ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());
				out.putNextEntry(entry);

				while ((bytes_read = in.read(buffer)) != -1) {
					out.write(buffer, 0, bytes_read);
				}
				out.closeEntry();
			} else {
				// zip directory
				File[] entries = fileOrDirectory.listFiles();
				for (int i = 0; i < entries.length; i++) {
					// recursively compress, update curPaths
					zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/");
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	/**
	 * unzip
	 *
	 * @param zipFileName
	 * Source File
	 * @param outputDirectory
	 * The directory where the decompressed files are stored
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	public static void unzip(String zipFileName, String outputDirectory) throws IOException {

		ZipFile zipFile = null;

		try {
			zipFile = new ZipFile(zipFileName);
			Enumeration e = zipFile.getEntries();

			ZipEntry zipEntry = null;

			File dest = new File(outputDirectory);
			dest.mkdirs ();

			while (e.hasMoreElements()) {
				zipEntry = (ZipEntry) e.nextElement();

				String entryName = zipEntry.getName ();

				InputStream in = null;
				FileOutputStream out = null;

				try {
					if (zipEntry.isDirectory()) {
						String name = zipEntry.getName();
						name = name.substring(0, name.length() - 1);

						File f = new File(outputDirectory + File.separator + name);
						f.mkdirs();
					} else {
						int index = entryName.lastIndexOf("\\");
						if (index != -1) {
							File df = new File(outputDirectory + File.separator + entryName.substring(0, index));
							df.mkdirs ();
						}
						index = entryName.lastIndexOf("/");
						if (index != -1) {
							File df = new File(outputDirectory + File.separator + entryName.substring(0, index));
							df.mkdirs ();
						}

						File f = new File(outputDirectory + File.separator + zipEntry.getName());
						// f.createNewFile();
						in = zipFile.getInputStream(zipEntry);
						out = new FileOutputStream(f);

						int c;
						byte[] by = new byte[1024];

						while ((c = in.read(by)) != -1) {
							out.write(by, 0, c);
						}
						out.flush();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
					throw new IOException("Failed to decompress: " + ex.toString());
				} finally {
					if (in != null) {
						try {
							in.close();
						} catch (IOException ex) {
						}
					}
					if (out != null) {
						try {
							out.close();
						} catch (IOException ex) {
						}
					}
				}
			}

		} catch (IOException ex) {
			ex.printStackTrace();
			throw new IOException("Failed to decompress: " + ex.toString());
		} finally {
			if (zipFile != null) {
				try {
					zipFile.close();
				} catch (IOException ex) {
				}
			}
		}

	}

	public static void main(String[] args) {
		try {
			// ZipUtil.zip("C:\\Users\\pactera\\Downloads", "F:\\test.zip");
			ZipUtil.unzip("F:\\test.zip", "F:\\test");
		} catch (Exception e) {
			e.printStackTrace ();
		}

	}

}

 The maven project needs to introduce the coordinates of the jar

 

 

		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.9.6</version>
		</dependency>

 

 Reprinted: http://www.codeweblog.com/apache-ant%E8%BF%9B%E8%A1%8Czip%E8%A7%A3%E5%8E%8B%E7%BC%A9%E6%93% 8D%E4%BD%9C%E7%A4%BA%E4%BE%8B%E5%88%86%E4%BA%AB/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780608&siteId=291194637