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.

ArchiveTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (C) 2012 Google Inc.
  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;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.fail;
  46. import static org.junit.Assume.assumeNoException;
  47. import java.io.BufferedInputStream;
  48. import java.io.BufferedReader;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.FileOutputStream;
  53. import java.io.InputStreamReader;
  54. import java.io.IOException;
  55. import java.io.OutputStream;
  56. import java.lang.Object;
  57. import java.lang.String;
  58. import java.util.ArrayList;
  59. import java.util.Arrays;
  60. import java.util.List;
  61. import java.util.concurrent.Callable;
  62. import java.util.concurrent.Executors;
  63. import java.util.concurrent.ExecutorService;
  64. import java.util.concurrent.Future;
  65. import java.util.zip.ZipEntry;
  66. import java.util.zip.ZipInputStream;
  67. import org.eclipse.jgit.api.Git;
  68. import org.eclipse.jgit.dircache.DirCache;
  69. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  70. import org.eclipse.jgit.lib.FileMode;
  71. import org.eclipse.jgit.pgm.CLIGitCommand;
  72. import org.junit.Before;
  73. import org.junit.Ignore;
  74. import org.junit.Test;
  75. public class ArchiveTest extends CLIRepositoryTestCase {
  76. private Git git;
  77. private String emptyTree;
  78. @Override
  79. @Before
  80. public void setUp() throws Exception {
  81. super.setUp();
  82. git = new Git(db);
  83. git.commit().setMessage("initial commit").call();
  84. emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name();
  85. }
  86. @Ignore("Some versions of java.util.zip refuse to write an empty ZIP")
  87. @Test
  88. public void testEmptyArchive() throws Exception {
  89. final byte[] result = CLIGitCommand.rawExecute( //
  90. "git archive --format=zip " + emptyTree, db);
  91. assertArrayEquals(new String[0], listZipEntries(result));
  92. }
  93. @Test
  94. public void testEmptyTar() throws Exception {
  95. final byte[] result = CLIGitCommand.rawExecute( //
  96. "git archive --format=tar " + emptyTree, db);
  97. assertArrayEquals(new String[0], listTarEntries(result));
  98. }
  99. @Test
  100. public void testUnrecognizedFormat() throws Exception {
  101. final String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" };
  102. final String[] actual = execute("git archive --format=nonsense " + emptyTree);
  103. assertArrayEquals(expect, actual);
  104. }
  105. @Test
  106. public void testArchiveWithFiles() throws Exception {
  107. writeTrashFile("a", "a file with content!");
  108. writeTrashFile("c", ""); // empty file
  109. writeTrashFile("unrelated", "another file, just for kicks");
  110. git.add().addFilepattern("a").call();
  111. git.add().addFilepattern("c").call();
  112. git.commit().setMessage("populate toplevel").call();
  113. final byte[] result = CLIGitCommand.rawExecute( //
  114. "git archive --format=zip HEAD", db);
  115. assertArrayEquals(new String[] { "a", "c" }, //
  116. listZipEntries(result));
  117. }
  118. private void commitGreeting() throws Exception {
  119. writeTrashFile("greeting", "hello, world!");
  120. git.add().addFilepattern("greeting").call();
  121. git.commit().setMessage("a commit with a file").call();
  122. }
  123. @Test
  124. public void testDefaultFormatIsTar() throws Exception {
  125. commitGreeting();
  126. final byte[] result = CLIGitCommand.rawExecute( //
  127. "git archive HEAD", db);
  128. assertArrayEquals(new String[] { "greeting" }, //
  129. listTarEntries(result));
  130. }
  131. private static String shellQuote(String s) {
  132. return "'" + s.replace("'", "'\\''") + "'";
  133. }
  134. @Test
  135. public void testFormatOverridesFilename() throws Exception {
  136. final File archive = new File(db.getWorkTree(), "format-overrides-name.tar");
  137. final String path = archive.getAbsolutePath();
  138. commitGreeting();
  139. assertArrayEquals(new String[] { "" },
  140. execute("git archive " +
  141. "--format=zip " +
  142. shellQuote("--output=" + path) + " " +
  143. "HEAD"));
  144. assertContainsEntryWithMode(path, "", "greeting");
  145. assertIsZip(archive);
  146. }
  147. @Test
  148. public void testUnrecognizedExtensionMeansTar() throws Exception {
  149. final File archive = new File(db.getWorkTree(), "example.txt");
  150. final String path = archive.getAbsolutePath();
  151. commitGreeting();
  152. assertArrayEquals(new String[] { "" },
  153. execute("git archive " +
  154. shellQuote("--output=" + path) + " " +
  155. "HEAD"));
  156. assertTarContainsEntry(path, "", "greeting");
  157. assertIsTar(archive);
  158. }
  159. @Test
  160. public void testNoExtensionMeansTar() throws Exception {
  161. final File archive = new File(db.getWorkTree(), "example");
  162. final String path = archive.getAbsolutePath();
  163. commitGreeting();
  164. assertArrayEquals(new String[] { "" },
  165. execute("git archive " +
  166. shellQuote("--output=" + path) + " " +
  167. "HEAD"));
  168. assertIsTar(archive);
  169. }
  170. @Test
  171. public void testExtensionMatchIsAnchored() throws Exception {
  172. final File archive = new File(db.getWorkTree(), "two-extensions.zip.bak");
  173. final String path = archive.getAbsolutePath();
  174. commitGreeting();
  175. assertArrayEquals(new String[] { "" },
  176. execute("git archive " +
  177. shellQuote("--output=" + path) + " " +
  178. "HEAD"));
  179. assertIsTar(archive);
  180. }
  181. @Test
  182. public void testZipExtension() throws Exception {
  183. final File archiveWithDot = new File(db.getWorkTree(), "greeting.zip");
  184. final File archiveNoDot = new File(db.getWorkTree(), "greetingzip");
  185. commitGreeting();
  186. execute("git archive " +
  187. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  188. "HEAD");
  189. execute("git archive " +
  190. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  191. "HEAD");
  192. assertIsZip(archiveWithDot);
  193. assertIsTar(archiveNoDot);
  194. }
  195. @Test
  196. public void testTarExtension() throws Exception {
  197. final File archive = new File(db.getWorkTree(), "tarball.tar");
  198. final String path = archive.getAbsolutePath();
  199. commitGreeting();
  200. assertArrayEquals(new String[] { "" },
  201. execute("git archive " +
  202. shellQuote("--output=" + path) + " " +
  203. "HEAD"));
  204. assertIsTar(archive);
  205. }
  206. @Test
  207. public void testTgzExtensions() throws Exception {
  208. commitGreeting();
  209. for (String ext : Arrays.asList("tar.gz", "tgz")) {
  210. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  211. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  212. execute("git archive " +
  213. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  214. "HEAD");
  215. execute("git archive " +
  216. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  217. "HEAD");
  218. assertIsGzip(archiveWithDot);
  219. assertIsTar(archiveNoDot);
  220. }
  221. }
  222. @Test
  223. public void testTbz2Extension() throws Exception {
  224. commitGreeting();
  225. for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) {
  226. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  227. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  228. execute("git archive " +
  229. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  230. "HEAD");
  231. execute("git archive " +
  232. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  233. "HEAD");
  234. assertIsBzip2(archiveWithDot);
  235. assertIsTar(archiveNoDot);
  236. }
  237. }
  238. @Test
  239. public void testTxzExtension() throws Exception {
  240. commitGreeting();
  241. for (String ext : Arrays.asList("tar.xz", "txz")) {
  242. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  243. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  244. execute("git archive " +
  245. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  246. "HEAD");
  247. execute("git archive " +
  248. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  249. "HEAD");
  250. assertIsXz(archiveWithDot);
  251. assertIsTar(archiveNoDot);
  252. }
  253. }
  254. @Test
  255. public void testArchiveWithSubdir() throws Exception {
  256. writeTrashFile("a", "a file with content!");
  257. writeTrashFile("b.c", "before subdir in git sort order");
  258. writeTrashFile("b0c", "after subdir in git sort order");
  259. writeTrashFile("c", "");
  260. git.add().addFilepattern("a").call();
  261. git.add().addFilepattern("b.c").call();
  262. git.add().addFilepattern("b0c").call();
  263. git.add().addFilepattern("c").call();
  264. git.commit().setMessage("populate toplevel").call();
  265. writeTrashFile("b/b", "file in subdirectory");
  266. writeTrashFile("b/a", "another file in subdirectory");
  267. git.add().addFilepattern("b").call();
  268. git.commit().setMessage("add subdir").call();
  269. final byte[] result = CLIGitCommand.rawExecute( //
  270. "git archive --format=zip master", db);
  271. String[] expect = { "a", "b.c", "b0c", "b/a", "b/b", "c" };
  272. String[] actual = listZipEntries(result);
  273. Arrays.sort(expect);
  274. Arrays.sort(actual);
  275. assertArrayEquals(expect, actual);
  276. }
  277. @Test
  278. public void testTarWithSubdir() throws Exception {
  279. writeTrashFile("a", "a file with content!");
  280. writeTrashFile("b.c", "before subdir in git sort order");
  281. writeTrashFile("b0c", "after subdir in git sort order");
  282. writeTrashFile("c", "");
  283. git.add().addFilepattern("a").call();
  284. git.add().addFilepattern("b.c").call();
  285. git.add().addFilepattern("b0c").call();
  286. git.add().addFilepattern("c").call();
  287. git.commit().setMessage("populate toplevel").call();
  288. writeTrashFile("b/b", "file in subdirectory");
  289. writeTrashFile("b/a", "another file in subdirectory");
  290. git.add().addFilepattern("b").call();
  291. git.commit().setMessage("add subdir").call();
  292. final byte[] result = CLIGitCommand.rawExecute( //
  293. "git archive --format=tar master", db);
  294. String[] expect = { "a", "b.c", "b0c", "b/a", "b/b", "c" };
  295. String[] actual = listTarEntries(result);
  296. Arrays.sort(expect);
  297. Arrays.sort(actual);
  298. assertArrayEquals(expect, actual);
  299. }
  300. @Test
  301. public void testArchivePreservesMode() throws Exception {
  302. writeTrashFile("plain", "a file with content");
  303. writeTrashFile("executable", "an executable file");
  304. writeTrashFile("symlink", "plain");
  305. git.add().addFilepattern("plain").call();
  306. git.add().addFilepattern("executable").call();
  307. git.add().addFilepattern("symlink").call();
  308. DirCache cache = db.lockDirCache();
  309. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  310. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  311. cache.write();
  312. cache.commit();
  313. cache.unlock();
  314. git.commit().setMessage("three files with different modes").call();
  315. final byte[] zipData = CLIGitCommand.rawExecute( //
  316. "git archive --format=zip master", db);
  317. writeRaw("zip-with-modes.zip", zipData);
  318. assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain");
  319. assertContainsEntryWithMode("zip-with-modes.zip", "-rwx", "executable");
  320. assertContainsEntryWithMode("zip-with-modes.zip", "l", "symlink");
  321. }
  322. @Test
  323. public void testTarPreservesMode() throws Exception {
  324. writeTrashFile("plain", "a file with content");
  325. writeTrashFile("executable", "an executable file");
  326. writeTrashFile("symlink", "plain");
  327. git.add().addFilepattern("plain").call();
  328. git.add().addFilepattern("executable").call();
  329. git.add().addFilepattern("symlink").call();
  330. DirCache cache = db.lockDirCache();
  331. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  332. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  333. cache.write();
  334. cache.commit();
  335. cache.unlock();
  336. git.commit().setMessage("three files with different modes").call();
  337. final byte[] archive = CLIGitCommand.rawExecute( //
  338. "git archive --format=tar master", db);
  339. writeRaw("with-modes.tar", archive);
  340. assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain");
  341. assertTarContainsEntry("with-modes.tar", "-rwxr-xr-x", "executable");
  342. assertTarContainsEntry("with-modes.tar", "l", "symlink -> plain");
  343. }
  344. @Test
  345. public void testArchiveWithLongFilename() throws Exception {
  346. String filename = "1234567890";
  347. for (int i = 0; i < 20; i++)
  348. filename = filename + "/1234567890";
  349. writeTrashFile(filename, "file with long path");
  350. git.add().addFilepattern("1234567890").call();
  351. git.commit().setMessage("file with long name").call();
  352. final byte[] result = CLIGitCommand.rawExecute( //
  353. "git archive --format=zip HEAD", db);
  354. assertArrayEquals(new String[] { filename },
  355. listZipEntries(result));
  356. }
  357. @Test
  358. public void testTarWithLongFilename() throws Exception {
  359. String filename = "1234567890";
  360. for (int i = 0; i < 20; i++)
  361. filename = filename + "/1234567890";
  362. writeTrashFile(filename, "file with long path");
  363. git.add().addFilepattern("1234567890").call();
  364. git.commit().setMessage("file with long name").call();
  365. final byte[] result = CLIGitCommand.rawExecute( //
  366. "git archive --format=tar HEAD", db);
  367. assertArrayEquals(new String[] { filename },
  368. listTarEntries(result));
  369. }
  370. @Test
  371. public void testArchivePreservesContent() throws Exception {
  372. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  373. writeTrashFile("xyzzy", payload);
  374. git.add().addFilepattern("xyzzy").call();
  375. git.commit().setMessage("add file with content").call();
  376. final byte[] result = CLIGitCommand.rawExecute( //
  377. "git archive --format=zip HEAD", db);
  378. assertArrayEquals(new String[] { payload }, //
  379. zipEntryContent(result, "xyzzy"));
  380. }
  381. @Test
  382. public void testTarPreservesContent() throws Exception {
  383. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  384. writeTrashFile("xyzzy", payload);
  385. git.add().addFilepattern("xyzzy").call();
  386. git.commit().setMessage("add file with content").call();
  387. final byte[] result = CLIGitCommand.rawExecute( //
  388. "git archive --format=tar HEAD", db);
  389. assertArrayEquals(new String[] { payload }, //
  390. tarEntryContent(result, "xyzzy"));
  391. }
  392. private Process spawnAssumingCommandPresent(String... cmdline) {
  393. final File cwd = db.getWorkTree();
  394. final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) //
  395. .directory(cwd) //
  396. .redirectErrorStream(true);
  397. Process proc = null;
  398. try {
  399. proc = procBuilder.start();
  400. } catch (IOException e) {
  401. // On machines without `cmdline[0]`, let the test pass.
  402. assumeNoException(e);
  403. }
  404. return proc;
  405. }
  406. private BufferedReader readFromProcess(Process proc) throws Exception {
  407. return new BufferedReader( //
  408. new InputStreamReader(proc.getInputStream(), "UTF-8"));
  409. }
  410. private void grepForEntry(String name, String mode, String... cmdline) //
  411. throws Exception {
  412. final Process proc = spawnAssumingCommandPresent(cmdline);
  413. proc.getOutputStream().close();
  414. final BufferedReader reader = readFromProcess(proc);
  415. try {
  416. String line;
  417. while ((line = reader.readLine()) != null)
  418. if (line.startsWith(mode) && line.endsWith(name))
  419. // found it!
  420. return;
  421. fail("expected entry " + name + " with mode " + mode + " but found none");
  422. } finally {
  423. proc.getOutputStream().close();
  424. proc.destroy();
  425. }
  426. }
  427. private void assertMagic(long offset, byte[] magicBytes, File file) throws Exception {
  428. BufferedInputStream in = new BufferedInputStream(
  429. new FileInputStream(file));
  430. try {
  431. in.skip(offset);
  432. byte[] actual = new byte[magicBytes.length];
  433. in.read(actual);
  434. assertArrayEquals(magicBytes, actual);
  435. } finally {
  436. in.close();
  437. }
  438. }
  439. private void assertMagic(byte[] magicBytes, File file) throws Exception {
  440. assertMagic(0, magicBytes, file);
  441. }
  442. private void assertIsTar(File file) throws Exception {
  443. assertMagic(257, new byte[] { 'u', 's', 't', 'a', 'r', 0 }, file);
  444. }
  445. private void assertIsZip(File file) throws Exception {
  446. assertMagic(new byte[] { 'P', 'K', 3, 4 }, file);
  447. }
  448. private void assertIsGzip(File file) throws Exception {
  449. assertMagic(new byte[] { 037, (byte) 0213 }, file);
  450. }
  451. private void assertIsBzip2(File file) throws Exception {
  452. assertMagic(new byte[] { 'B', 'Z', 'h' }, file);
  453. }
  454. private void assertIsXz(File file) throws Exception {
  455. assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file);
  456. }
  457. private void assertContainsEntryWithMode(String zipFilename, String mode, String name) //
  458. throws Exception {
  459. grepForEntry(name, mode, "zipinfo", zipFilename);
  460. }
  461. private void assertTarContainsEntry(String tarfile, String mode, String name) //
  462. throws Exception {
  463. grepForEntry(name, mode, "tar", "tvf", tarfile);
  464. }
  465. private void writeRaw(String filename, byte[] data) //
  466. throws IOException {
  467. final File path = new File(db.getWorkTree(), filename);
  468. final OutputStream out = new FileOutputStream(path);
  469. try {
  470. out.write(data);
  471. } finally {
  472. out.close();
  473. }
  474. }
  475. private static String[] listZipEntries(byte[] zipData) throws IOException {
  476. final List<String> l = new ArrayList<String>();
  477. final ZipInputStream in = new ZipInputStream( //
  478. new ByteArrayInputStream(zipData));
  479. ZipEntry e;
  480. while ((e = in.getNextEntry()) != null)
  481. l.add(e.getName());
  482. in.close();
  483. return l.toArray(new String[l.size()]);
  484. }
  485. private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) {
  486. final ExecutorService executor = Executors.newSingleThreadExecutor();
  487. return executor.submit(new Callable<Object>() { //
  488. public Object call() throws IOException {
  489. try {
  490. stream.write(data);
  491. return null;
  492. } finally {
  493. stream.close();
  494. }
  495. }
  496. });
  497. }
  498. private String[] listTarEntries(byte[] tarData) throws Exception {
  499. final List<String> l = new ArrayList<String>();
  500. final Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
  501. final BufferedReader reader = readFromProcess(proc);
  502. final OutputStream out = proc.getOutputStream();
  503. // Dump tarball to tar stdin in background
  504. final Future<?> writing = writeAsync(out, tarData);
  505. try {
  506. String line;
  507. while ((line = reader.readLine()) != null)
  508. l.add(line);
  509. return l.toArray(new String[l.size()]);
  510. } finally {
  511. writing.get();
  512. reader.close();
  513. proc.destroy();
  514. }
  515. }
  516. private static String[] zipEntryContent(byte[] zipData, String path) //
  517. throws IOException {
  518. final ZipInputStream in = new ZipInputStream( //
  519. new ByteArrayInputStream(zipData));
  520. ZipEntry e;
  521. while ((e = in.getNextEntry()) != null) {
  522. if (!e.getName().equals(path))
  523. continue;
  524. // found!
  525. final List<String> l = new ArrayList<String>();
  526. final BufferedReader reader = new BufferedReader( //
  527. new InputStreamReader(in, "UTF-8"));
  528. String line;
  529. while ((line = reader.readLine()) != null)
  530. l.add(line);
  531. return l.toArray(new String[l.size()]);
  532. }
  533. // not found
  534. return null;
  535. }
  536. private String[] tarEntryContent(byte[] tarData, String path) //
  537. throws Exception {
  538. final List<String> l = new ArrayList<String>();
  539. final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
  540. final BufferedReader reader = readFromProcess(proc);
  541. final OutputStream out = proc.getOutputStream();
  542. final Future<?> writing = writeAsync(out, tarData);
  543. try {
  544. String line;
  545. while ((line = reader.readLine()) != null)
  546. l.add(line);
  547. return l.toArray(new String[l.size()]);
  548. } finally {
  549. writing.get();
  550. reader.close();
  551. proc.destroy();
  552. }
  553. }
  554. }