| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2007 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.common.utils; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.util.jar.JarEntry; |
| import java.util.jar.JarOutputStream; |
| import java.util.jar.Manifest; |
| |
| import org.eclipse.core.runtime.IProgressMonitor; |
| |
| |
| public class JarUtil { |
| |
| /** |
| * Creates a zip file. |
| * |
| * @param jarFilePath |
| * an absolute path to the target jar file |
| * @param contentPath |
| * an absolute path to a directory containing the files and sub |
| * directories to be jared |
| * @param monitor |
| * a progress monitor |
| * @throws IOException |
| * if an error occurs while creating the zip file |
| */ |
| public static void createJarFile(String jarFilePath, String contentPath, |
| IProgressMonitor monitor) throws IOException { |
| JarOutputStream jarOutput = null; |
| try { |
| Manifest manifest = new Manifest(); |
| jarOutput = new JarOutputStream(new FileOutputStream(jarFilePath), manifest); |
| jarOutput.setMethod(JarOutputStream.DEFLATED); |
| File contentDir = new File(contentPath); |
| populateJarFile(jarOutput, contentDir.getAbsolutePath(), contentDir, |
| monitor); |
| } catch (IOException e) { |
| throw e; |
| } catch (Exception e) { |
| throw new IOException(e.getMessage()); |
| } |
| finally { |
| if (jarOutput != null) { |
| try { |
| jarOutput.flush(); |
| jarOutput.close(); |
| } |
| catch (Exception e) { |
| } |
| } |
| } |
| } |
| |
| /** |
| * Populates a jar file with directories and files. |
| * |
| * @param out |
| * a jar file output stream |
| * @param rootPath |
| * a root path that is used to compute the relative paths of the |
| * jar content |
| * @param file |
| * a file or directory |
| * @param monitor |
| * a progress monitor |
| */ |
| public static void populateJarFile(JarOutputStream out, String rootPath, |
| File file, IProgressMonitor monitor) throws IOException { |
| File[] files = file.listFiles(); |
| if (files != null) { |
| for (int i = 0; i < files.length; i++) { |
| if (files[i].isDirectory()) { |
| populateJarFile(out, rootPath, files[i], monitor); |
| } else { |
| String filePath = files[i].getAbsolutePath(); |
| FileInputStream in = new FileInputStream(filePath); |
| String relativePath = filePath |
| .substring(rootPath.length() + 1); |
| out.putNextEntry(new JarEntry(relativePath)); |
| int fileLength = (int)files[i].length(); |
| byte[] buffer = new byte[fileLength]; |
| int len; |
| while ((len = in.read(buffer)) > 0) { |
| out.write(buffer, 0, len); |
| } |
| out.closeEntry(); |
| in.close(); |
| } |
| } |
| } |
| } |
| } |