// TODO private void update(File[] files) throws IOException { InputStream in; TarInputStream tin; TarEntry entry; TreeMap<String, Long> entries = new TreeMap<String, Long>(); if ((in = openFileRead(archive)) == null) { fatal(" ", 1); } if (decompress != 0) { in = wrapInputStream(in); } tin = new TarInputStream(in); while ((entry = tin.getNextEntry()) != null) { entries.put(entry.getName(), entry.getModTime().getTime()); } tin.close(); long etime, ftime; ArrayList<File> list = new ArrayList<File>(); for (File file : files) { if (entries.containsKey(file.getPath())) { etime = entries.get(file.getPath()); ftime = file.lastModified(); if (etime >= ftime) { continue; } } list.add(file); } insert(list.toArray(files)); }
/** Outputs the differences found between the archive and the file system. */ private void diff() throws IOException { TarEntry entry; InputStream in = null; TarInputStream tin; File file; if ((in = openFileRead(archive)) == null) { exit(1); } if (decompress != 0) { in = wrapInputStream(in); } tin = new TarInputStream(in); while ((entry = tin.getNextEntry()) != null) { file = new File(entry.getName()); if (!file.exists()) { out(file + ": Warning: No such file or directory"); continue; } if (file.lastModified() != entry.getModTime().getTime()) { out(file + ": Mod time is different"); } if (file.length() != entry.getSize()) { out(file + ": Size is different"); } // TODO check file mode // TODO check file ownership } }
@Override public void analyze(Document doc, InputStream in) throws IOException { content.setLength(0); TarInputStream zis = new TarInputStream(in); TarEntry entry; while ((entry = zis.getNextEntry()) != null) { content.append(entry.getName()).append('\n'); } content.trimToSize(); doc.add(new TextField("full", content.toString(), Store.NO)); }
private static void addEntry( final String pName, final String pContent, final TarOutputStream pOutput) throws IOException { final byte[] data = pContent.getBytes("UTF-8"); final TarEntry entry = new TarEntry("./" + pName); entry.setSize(data.length); entry.setNames("root", "root"); pOutput.putNextEntry(entry); pOutput.write(data); pOutput.closeEntry(); }
/** * 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(); }
/** * List the contents of an archive. * * <p>TODO Need to parse Path for choosing specific files/directories either by direct naming or * by wildcard patterns. */ private void list() throws IOException { TarEntry entry; InputStream in = null; TarInputStream tin; if ((in = openFileRead(archive)) == null) { fatal(" ", 1); } if (decompress != 0) { in = wrapInputStream(in); } tin = new TarInputStream(in); while ((entry = tin.getNextEntry()) != null) { out(entry.getName()); } }
/** * 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"); }
@Test(timeout = 30000) public void testUnTar() throws IOException { setupDirs(); // make a simple tar: final File simpleTar = new File(del, FILE); OutputStream os = new FileOutputStream(simpleTar); TarOutputStream tos = new TarOutputStream(os); try { TarEntry te = new TarEntry("/bar/foo"); byte[] data = "some-content".getBytes("UTF-8"); te.setSize(data.length); tos.putNextEntry(te); tos.write(data); tos.closeEntry(); tos.flush(); tos.finish(); } finally { tos.close(); } // successfully untar it into an existing dir: FileUtil.unTar(simpleTar, tmp); // check result: assertTrue(new File(tmp, "/bar/foo").exists()); assertEquals(12, new File(tmp, "/bar/foo").length()); final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog"); regularFile.createNewFile(); assertTrue(regularFile.exists()); try { FileUtil.unTar(simpleTar, regularFile); assertTrue("An IOException expected.", false); } catch (IOException ioe) { // okay } }
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(); }
/*
/** * Build control archive of the deb * * @param pControlFiles * @param pDataSize * @param pChecksums * @param pOutput * @return * @throws FileNotFoundException * @throws IOException * @throws ParseException */ private PackageDescriptor buildControl( final File[] pControlFiles, final BigInteger pDataSize, final StringBuffer pChecksums, final File pOutput) throws IOException, ParseException { PackageDescriptor packageDescriptor = null; final TarOutputStream outputStream = new TarOutputStream(new GZIPOutputStream(new FileOutputStream(pOutput))); outputStream.setLongFileMode(TarOutputStream.LONGFILE_GNU); for (int i = 0; i < pControlFiles.length; i++) { final File file = pControlFiles[i]; if (file.isDirectory()) { continue; } final TarEntry entry = new TarEntry(file); final String name = file.getName(); entry.setName("./" + name); entry.setNames("root", "root"); entry.setMode(PermMapper.toMode("755")); if ("control".equals(name)) { packageDescriptor = new PackageDescriptor(new FileInputStream(file), resolver); if (packageDescriptor.get("Date") == null) { SimpleDateFormat fmt = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); // Mon, 26 Mar 2007 11:44:04 +0200 (RFC 2822) // FIXME Is this field allowed in package descriptors ? packageDescriptor.set("Date", fmt.format(new Date())); } if (packageDescriptor.get("Distribution") == null) { packageDescriptor.set("Distribution", "unknown"); } if (packageDescriptor.get("Urgency") == null) { packageDescriptor.set("Urgency", "low"); } final String debFullName = System.getenv("DEBFULLNAME"); final String debEmail = System.getenv("DEBEMAIL"); if (debFullName != null && debEmail != null) { packageDescriptor.set("Maintainer", debFullName + " <" + debEmail + ">"); console.println("Using maintainer from the environment variables."); } continue; } final InputStream inputStream = new FileInputStream(file); outputStream.putNextEntry(entry); Utils.copy(inputStream, outputStream); outputStream.closeEntry(); inputStream.close(); } if (packageDescriptor == null) { throw new FileNotFoundException("No control file in " + Arrays.toString(pControlFiles)); } packageDescriptor.set("Installed-Size", pDataSize.divide(BigInteger.valueOf(1024)).toString()); addEntry("control", packageDescriptor.toString(), outputStream); addEntry("md5sums", pChecksums.toString(), outputStream); outputStream.close(); return packageDescriptor; }
/** * Determine if the given entry is a descendant of this entry. Descendancy is determined by the * name of the descendant starting with this entry's name. * * @param desc Entry to be checked as a descendent of this. * @return True if entry is a descendant of this. */ public boolean isDescendent(TarEntry desc) { return desc.getName().startsWith(this.getName()); }
/** * Determine if the two entries are equal. Equality is determined by the header names being equal. * * @return it Entry to be checked for equality. * @return True if the entries are equal. */ public boolean equals(TarEntry it) { return this.getName().equals(it.getName()); }
public TarEntry map(final TarEntry pEntry) { final String name = pEntry.getName(); final TarEntry newEntry = new TarEntry(prefix + '/' + Utils.stripPath(strip, name)); newEntry.setUserId(pEntry.getUserId()); newEntry.setGroupId(pEntry.getGroupId()); newEntry.setUserName(pEntry.getUserName()); newEntry.setGroupName(pEntry.getGroupName()); newEntry.setMode(pEntry.getMode()); newEntry.setSize(pEntry.getSize()); return newEntry; }