JAVA解压zip文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author zhaoliangyuan
 * @E-mail [email protected]
 * @dateTime 2010/7/20 上午 10:24:42 类说明:
 */
public class Test23 {
	/**
	 * * 解压zip文件 *
	 * 
	 * @author Michael sun
	 * 
	 */

	public class UnzipFile {
	}

	/**
	 * 
	 * 解压zip文件 * *
	 * 
	 * @param targetPath *
	 * @param zipFilePath
	 * 
	 */

	public void unzipFile(String targetPath, String zipFilePath) {

		try {

			File zipFile = new File(zipFilePath);

			InputStream is = new FileInputStream(zipFile);

			ZipInputStream zis = new ZipInputStream(is);

			ZipEntry entry = null;

			System.out.println("開始解壓:" + zipFile.getName() + "...");

			while ((entry = zis.getNextEntry()) != null) {

				String zipPath = entry.getName();

				try {

					if (entry.isDirectory()) {

						File zipFolder = new File(targetPath + File.separator

						+ zipPath);

						if (!zipFolder.exists()) {

							zipFolder.mkdirs();

						}

					} else {

						File file = new File(targetPath + File.separator + zipPath);

						if (!file.exists()) {

							File pathDir = file.getParentFile();

							pathDir.mkdirs();

							file.createNewFile();

						}

						FileOutputStream fos = new FileOutputStream(file);

						int bread;

						while ((bread = zis.read()) != -1) {

							fos.write(bread);

						}

						fos.close();

					}

					System.out.println("成功解壓:" + zipPath);

				} catch (Exception e) {

					System.out.println("解壓" + zipPath + "失敗");

					continue;

				}

			}

			zis.close();

			is.close();

			System.out.println("解壓結束");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		//不用建立解壓后的文件夾
		String targetPath = "D:\\R000072";
		String zipFile = "D:\\R000072.zip";
		Test23 unzip = new Test23();
		unzip.unzipFile(targetPath, zipFile);
	}
}
 

你可能感兴趣的:(java,sun)