/** * Extract entries from an archive. * * <p>TODO Need to parse Path for choosing specific files/directories either by direct naming or * by wildcard patterns. TODO Read list of entries to extract from FileList if its set. */ private void extract() throws IOException { TarEntry entry; InputStream in = null; OutputStream out; TarInputStream tin; File file; if (archive != null) { if ((in = openFileRead(archive)) == null) { fatal(" ", 1); } } else { in = stdin; } if (decompress != 0) { in = wrapInputStream(in); } tin = new TarInputStream(in); if (use_stdout) { out = stdout; } while ((entry = tin.getNextEntry()) != null) { notice(entry.getName()); file = new File(entry.getName()); if (entry.isDirectory()) { if (!file.exists()) { file.mkdirs(); } continue; } else { if (file.exists()) { if (keepOld || (keepNew && (file.lastModified() >= entry.getModTime().getTime()))) { continue; } if (backup) { file.renameTo(new File(file.getPath() + suffix)); } } } if ((out = openFileWrite(file, true, true)) == null) { continue; } tin.copyEntryContents(out); out.close(); } tin.close(); }
public void doit(String archiveFilename) { f.clear(); try { tar = new TarInputStream(new GZIPInputStream(new FileInputStream(archiveFilename))); while ((current = tar.getNextEntry()) != null) { System.err.println("ok:" + current.getName()); long s = current.getSize(); if (!current.isDirectory()) { // System.err.println(current.getFile()); f.prepareForBulkInsert(tar, current.getName()); } } } catch (Exception e) { e.printStackTrace(); } f.flushFiles(); }
/** * untar for any archive, overwrites existing data * * @param in use getInputStream() for convenience * @param untarDir destination path * @throws Exception (IOException or FileNotFoundException) */ public static void unTar(final InputStream in, final String untarDir) throws Exception { ConcurrentLog.info("UNTAR", "starting"); if (new File(untarDir).exists()) { final TarInputStream tin = new TarInputStream(in); TarEntry tarEntry = tin.getNextEntry(); while (tarEntry != null) { final File destPath = new File(untarDir + File.separator + tarEntry.getName()); if (!tarEntry.isDirectory()) { new File(destPath.getParent()).mkdirs(); // create missing subdirectories final FileOutputStream fout = new FileOutputStream(destPath); tin.copyEntryContents(fout); fout.close(); } else { destPath.mkdir(); } tarEntry = tin.getNextEntry(); } tin.close(); } else { // untarDir doesn't exist ConcurrentLog.warn("UNTAR", "destination " + untarDir + " doesn't exist."); } ConcurrentLog.info("UNTAR", "finished"); }
public void testTarFileSet() throws Exception { project.executeTarget("tarfileset"); File deb = new File("target/test-classes/test.deb"); assertTrue("package not build", deb.exists()); ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(deb)); ArArchiveEntry entry; while ((entry = in.getNextArEntry()) != null) { if (entry.getName().equals("data.tar.gz")) { TarInputStream tar = new TarInputStream(new GZIPInputStream(new NonClosingInputStream(in))); TarEntry tarentry; while ((tarentry = tar.getNextEntry()) != null) { assertTrue("prefix", tarentry.getName().startsWith("./foo/")); if (tarentry.isDirectory()) { assertEquals("directory mode (" + tarentry.getName() + ")", 040700, tarentry.getMode()); } else { assertEquals("file mode (" + tarentry.getName() + ")", 0100600, tarentry.getMode()); } assertEquals("user", "ebourg", tarentry.getUserName()); assertEquals("group", "ebourg", tarentry.getGroupName()); } tar.close(); } else { // skip to the next entry long skip = entry.getLength(); while (skip > 0) { long skipped = in.skip(skip); if (skipped == -1) { throw new IOException("Failed to skip"); } skip -= skipped; } } } in.close(); }