Here you can find the source of unzip(String zipFile, String targetFolder)
Parameter | Description |
---|---|
zipFile | The component zip file |
targetFolder | The user folder |
Parameter | Description |
---|---|
Exception | an exception |
public static void unzip(String zipFile, String targetFolder) throws Exception
//package com.java2s; // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**//from ww w. j av a 2 s . c o m * Unzip the component file to the user folder. * * @param zipFile The component zip file * @param targetFolder The user folder * @return * @throws Exception */ public static void unzip(String zipFile, String targetFolder) throws Exception { Exception exception = null; ZipFile zip = new ZipFile(zipFile); byte[] buf = new byte[8192]; try { Enumeration<ZipEntry> enumeration = (Enumeration<ZipEntry>) zip.entries(); while (enumeration.hasMoreElements()) { ZipEntry entry = enumeration.nextElement(); File file = new File(targetFolder, entry.getName()); if (entry.isDirectory()) { if (!file.exists()) { file.mkdir(); } } else { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } InputStream zin = zip.getInputStream(entry); OutputStream fout = new FileOutputStream(file); // check if parent folder exists File dir = file.getParentFile(); if (dir.isDirectory() && !dir.exists()) { dir.mkdirs(); } try { while (true) { int bytesRead = zin.read(buf); if (bytesRead == -1) { // end of file break; } fout.write(buf, 0, bytesRead); } fout.flush(); } catch (Exception e) { exception = e; // stop looping return; } finally { zin.close(); fout.close(); } } } } catch (Exception e) { exception = e; } finally { zip.close(); if (exception != null) { // notify caller with exception throw exception; } } } }