You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JarLinkUtil.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm.build;
  44. import java.io.File;
  45. import java.io.FileInputStream;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.util.ArrayList;
  49. import java.util.Enumeration;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.zip.ZipEntry;
  54. import java.util.zip.ZipFile;
  55. import java.util.zip.ZipOutputStream;
  56. import org.kohsuke.args4j.CmdLineException;
  57. import org.kohsuke.args4j.CmdLineParser;
  58. import org.kohsuke.args4j.Option;
  59. import org.kohsuke.args4j.spi.MapOptionHandler;
  60. /**
  61. * Combines multiple JAR and directory sources into a single JAR file.
  62. * <p>
  63. * This is a crude command line utility to combine multiple JAR files into a
  64. * single JAR file, without first needing to unpack the individual JARs.
  65. * <p>
  66. * The output ZIP stream is sent to standard out and can be redirected onto the
  67. * end of a shell script which starts the JRE.
  68. */
  69. public class JarLinkUtil {
  70. /**
  71. * Combine multiple JARs.
  72. *
  73. * @param argv
  74. * the command line arguments indicating the files to pack.
  75. * @throws IOException
  76. * a source file could not be read.
  77. */
  78. public static void main(final String[] argv) throws IOException {
  79. final JarLinkUtil util = new JarLinkUtil();
  80. final CmdLineParser clp = new CmdLineParser(util);
  81. try {
  82. clp.parseArgument(argv);
  83. } catch (CmdLineException e) {
  84. clp.printSingleLineUsage(System.err);
  85. System.exit(1);
  86. }
  87. util.run();
  88. }
  89. @Option(name = "-include", required = true)
  90. private List<File> includes = new ArrayList<File>();
  91. @Option(name = "-file", handler = MapOptionHandler.class)
  92. private Map<String, String> files = new HashMap<String, String>();
  93. private final Map<String, File> chosenSources = new HashMap<String, File>();
  94. private long creationTime;
  95. private ZipOutputStream zos;
  96. private JarLinkUtil() {
  97. // Command line utility only.
  98. }
  99. private void run() throws IOException {
  100. for (final File src : includes) {
  101. if (src.isFile())
  102. scanJar(src);
  103. else
  104. scanDirectory(src, src, "");
  105. }
  106. for (final Map.Entry<String, String> e : files.entrySet())
  107. chosenSources.put(e.getKey(), new File(e.getValue()));
  108. creationTime = System.currentTimeMillis();
  109. zos = new ZipOutputStream(System.out);
  110. zos.setLevel(9);
  111. for (final File src : includes) {
  112. if (src.isFile())
  113. appendJar(src);
  114. else
  115. appendDirectory(src, src, "");
  116. }
  117. for (final String name : files.keySet())
  118. appendFile(chosenSources.get(name), name);
  119. zos.close();
  120. }
  121. private void scanJar(final File jarPath) throws IOException {
  122. final ZipFile zf = new ZipFile(jarPath);
  123. final Enumeration<? extends ZipEntry> e = zf.entries();
  124. while (e.hasMoreElements())
  125. chosenSources.put(e.nextElement().getName(), jarPath);
  126. zf.close();
  127. }
  128. private void scanDirectory(final File rootPath, final File dirPath,
  129. final String pfx) throws IOException {
  130. final File[] entries = dirPath.listFiles();
  131. if (entries == null)
  132. return;
  133. for (final File e : entries) {
  134. if (e.getName().equals(".") || e.getName().equals(".."))
  135. continue;
  136. if (e.isDirectory())
  137. scanDirectory(rootPath, e, pfx + e.getName() + "/");
  138. else
  139. chosenSources.put(pfx + e.getName(), rootPath);
  140. }
  141. }
  142. private void appendJar(final File jarPath) throws IOException {
  143. final ZipFile zf = new ZipFile(jarPath);
  144. final Enumeration<? extends ZipEntry> e = zf.entries();
  145. while (e.hasMoreElements()) {
  146. final ZipEntry ze = e.nextElement();
  147. final String name = ze.getName();
  148. if (chosenSources.get(name) == jarPath)
  149. appendEntry(name, ze.getSize(), ze.getTime(), zf
  150. .getInputStream(ze));
  151. }
  152. zf.close();
  153. }
  154. private void appendDirectory(final File rootDir, final File dirPath,
  155. final String pfx) throws IOException {
  156. final File[] entries = dirPath.listFiles();
  157. if (entries == null)
  158. return;
  159. for (final File e : entries) {
  160. if (e.getName().equals(".") || e.getName().equals(".."))
  161. continue;
  162. if (e.isDirectory())
  163. appendDirectory(rootDir, e, pfx + e.getName() + "/");
  164. else if (chosenSources.get(pfx + e.getName()) == rootDir)
  165. appendFile(e, pfx + e.getName());
  166. }
  167. }
  168. private void appendFile(final File path, final String name)
  169. throws IOException {
  170. final long len = path.length();
  171. final InputStream is = new FileInputStream(path);
  172. appendEntry(name, len, creationTime, is);
  173. }
  174. private void appendEntry(final String name, final long len,
  175. final long time, final InputStream is) throws IOException {
  176. final ZipEntry ze = new ZipEntry(name);
  177. ze.setSize(len);
  178. ze.setTime(time);
  179. zos.putNextEntry(ze);
  180. try {
  181. final byte[] buf = new byte[4096];
  182. int n;
  183. while ((n = is.read(buf)) >= 0)
  184. zos.write(buf, 0, n);
  185. } finally {
  186. is.close();
  187. }
  188. zos.closeEntry();
  189. }
  190. }