[JAVA] jar 파일 압축해제 소스코드
혹시 JAVA 소스코드를 보러 온게 아니라 jar 파일을 압축풀고 싶으신 분을 위해서 적어둠.
jar 파일은 zip 파일과 구조가 같으므로 반디집, 알집 같은 툴로 압축해제하면 된다.
아래는 jar 파일을 압축해제하는 자바 소스코드다.
|
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.jar.JarFile; import java.util.zip.ZipEntry;
public class JarExtractUtil {
public static void extractJar(String jarFilePath, String destinationDirPath) throws NullPointerException, Exception { File f = null; JarFile jarFile = null; try {
f = new File(jarFilePath); jarFile = new JarFile(f); File destDir = new File(destinationDirPath); if (!destDir.exists()) { destDir.mkdirs(); } Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { File newDir = new File(destDir, entry.getName()); if (entry.getName().toUpperCase().indexOf(“META-INF”) > -1) { continue; } newDir.mkdirs(); } else { if (entry.getName().toUpperCase().indexOf(“META-INF”) > -1) { continue; } File targetFileObj = new File(destDir, entry.getName()); InputStream input = null; FileOutputStream fileOutput = null; BufferedOutputStream buffOutput = null; try { input = jarFile.getInputStream(entry); fileOutput = new FileOutputStream(targetFileObj); buffOutput = new BufferedOutputStream(fileOutput); byte[] buf = new byte[4096]; int read = -1; while ((read = input.read(buf)) != -1) { buffOutput.write(buf, 0, read); } buffOutput.flush(); buffOutput.close(); fileOutput.close(); input.close(); } catch (IOException e) { throw e; } catch (Exception e) { throw e; } finally { try { if (buffOutput != null) { buffOutput.close(); } } catch (IOException e) { } catch (Exception e) { } finally { buffOutput = null; } try { if (fileOutput != null) { fileOutput.close(); } } catch (IOException e) { } catch (Exception e) { } finally { fileOutput = null; } try { if (input != null) { input.close(); } } catch (IOException e) { } catch (Exception e) { } finally { input = null; } } } } } catch (IOException e) { throw e; } catch (Exception e) { throw e; } finally { try { if (jarFile != null) { jarFile.close(); } } catch (IOException e) { } catch (Exception e) { } finally { jarFile = null; } } } }
|
참고사이트 : http://www.gnujava.com/board/article_view.jsp?article_no=371&board_no=1&table_cd=EPAR01&table_no=01