List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getLinkName
public String getLinkName()
From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java
@Test public void testSymbolicLinkAbsoluteTargetConvertedToRelative() throws Exception { // use absolute path as symlink target Path absoluteLinkTarget = new File(archiveRoot, "dir2/dir3/test.sh").getAbsoluteFile().toPath(); createSymbolicLink(new File(archiveRoot, "dir2/testSymLink"), absoluteLinkTarget); archiver.createArchive();/* w w w . j av a 2 s . co m*/ TarArchiveEntry symLinkEntry = getTarEntries().get("dir2/testSymLink"); assertTrue(symLinkEntry.isSymbolicLink()); final String relativeLinkTarget = "dir3/test.sh"; assertEquals(relativeLinkTarget, symLinkEntry.getLinkName()); }
From source file:org.moe.cli.utils.ArchiveUtils.java
public static void untarArchive(ArchiveInputStream archStrim, File destination) throws IOException { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) archStrim.getNextEntry()) != null) { if (entry.isDirectory()) { String dest = entry.getName(); File destFolder = new File(destination, dest); if (!destFolder.exists()) { destFolder.mkdirs();/* w ww .j av a 2s .c om*/ } } else { int count; byte[] data = new byte[2048]; File d = new File(destination, entry.getName()); if (!d.getParentFile().exists()) { d.getParentFile().mkdirs(); } if (entry.isSymbolicLink()) { String link = entry.getLinkName(); String entryName = entry.getName(); int parentIdx = entryName.lastIndexOf("/"); String newLink = entryName.substring(0, parentIdx) + "/" + link; File destFile = new File(destination, newLink); File linkFile = new File(destination, entryName); Files.createSymbolicLink(Paths.get(linkFile.getPath()), Paths.get(destFile.getPath())); } else { FileOutputStream fos = new FileOutputStream(d); BufferedOutputStream dest = new BufferedOutputStream(fos, 2048); while ((count = archStrim.read(data, 0, 2048)) != -1) { dest.write(data, 0, count); } dest.close(); } } } }
From source file:org.phoenicis.tools.archive.Tar.java
/** * Uncompress a tar//from w ww . jav a2 s . co m * * @param countingInputStream * to count the number of byte extracted * @param outputDir * The directory where files should be extracted * @return A list of extracted files * @throws ArchiveException * if the process fails */ private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) { final List<File> uncompressedFiles = new LinkedList<>(); try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream)) { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath())); Files.createDirectories(outputFile.toPath()); } } else { LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(), entry.getMode())); if (entry.isSymbolicLink()) { Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()), Paths.get(entry.getLinkName())); } else { try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) { IOUtils.copy(debInputStream, outputFileStream); Files.setPosixFilePermissions(Paths.get(outputFile.getPath()), fileUtilities.octToPosixFilePermission(entry.getMode())); } } } uncompressedFiles.add(outputFile); stateCallback.accept(new ProgressEntity.Builder() .withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100) .withProgressText("Extracting " + outputFile.getName()).build()); } return uncompressedFiles; } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) { throw new ArchiveException("Unable to extract the file", e); } }
From source file:org.vafer.jdeb.DataBuilder.java
/** * Build the data archive of the deb from the provided DataProducers * * @param producers/* ww w. j ava 2 s . c o m*/ * @param output * @param checksums * @param compression the compression method used for the data file * @return * @throws java.security.NoSuchAlgorithmException * @throws java.io.IOException * @throws org.apache.commons.compress.compressors.CompressorException */ BigInteger buildData(Collection<DataProducer> producers, File output, final StringBuilder checksums, Compression compression) throws NoSuchAlgorithmException, IOException, CompressorException { final File dir = output.getParentFile(); if (dir != null && (!dir.exists() || !dir.isDirectory())) { throw new IOException("Cannot write data file at '" + output.getAbsolutePath() + "'"); } final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream( compression.toCompressedOutputStream(new FileOutputStream(output))); tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); final MessageDigest digest = MessageDigest.getInstance("MD5"); final Total dataSize = new Total(); final List<String> addedDirectories = new ArrayList<String>(); final DataConsumer receiver = new DataConsumer() { public void onEachDir(String dirname, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { dirname = fixPath(dirname); createParentDirectories(dirname, user, uid, group, gid); // The directory passed in explicitly by the caller also gets the passed-in mode. (Unlike // the parent directories for now. See related comments at "int mode =" in // createParentDirectories, including about a possible bug.) createDirectory(dirname, user, uid, group, gid, mode, 0); console.info("dir: " + dirname); } public void onEachFile(InputStream inputStream, String filename, String linkname, String user, int uid, String group, int gid, int mode, long size) throws IOException { filename = fixPath(filename); createParentDirectories(filename, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(filename, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); dataSize.add(size); digest.reset(); Utils.copy(inputStream, new DigestOutputStream(tarOutputStream, digest)); final String md5 = Utils.toHex(digest.digest()); tarOutputStream.closeArchiveEntry(); console.info("file:" + entry.getName() + " size:" + entry.getSize() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId() + " modtime:" + entry.getModTime() + " md5: " + md5); // append to file md5 list checksums.append(md5).append(" ").append(entry.getName()).append('\n'); } public void onEachLink(String path, String linkName, boolean symlink, String user, int uid, String group, int gid, int mode) throws IOException { path = fixPath(path); createParentDirectories(path, user, uid, group, gid); final TarArchiveEntry entry = new TarArchiveEntry(path, symlink ? TarArchiveEntry.LF_SYMLINK : TarArchiveEntry.LF_LINK); entry.setLinkName(linkName); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); console.info("link:" + entry.getName() + " mode:" + entry.getMode() + " linkname:" + entry.getLinkName() + " username:" + entry.getUserName() + " userid:" + entry.getUserId() + " groupname:" + entry.getGroupName() + " groupid:" + entry.getGroupId()); } private void createDirectory(String directory, String user, int uid, String group, int gid, int mode, long size) throws IOException { // All dirs should end with "/" when created, or the test DebAndTaskTestCase.testTarFileSet() thinks its a file // and so thinks it has the wrong permission. // This consistency also helps when checking if a directory already exists in addedDirectories. if (!directory.endsWith("/")) { directory += "/"; } if (!addedDirectories.contains(directory)) { TarArchiveEntry entry = new TarArchiveEntry(directory, true); entry.setUserName(user); entry.setUserId(uid); entry.setGroupName(group); entry.setGroupId(gid); entry.setMode(mode); entry.setSize(size); tarOutputStream.putArchiveEntry(entry); tarOutputStream.closeArchiveEntry(); addedDirectories.add(directory); // so addedDirectories consistently have "/" for finding duplicates. } } private void createParentDirectories(String filename, String user, int uid, String group, int gid) throws IOException { String dirname = fixPath(new File(filename).getParent()); // Debian packages must have parent directories created // before sub-directories or files can be installed. // For example, if an entry of ./usr/lib/foo/bar existed // in a .deb package, but the ./usr/lib/foo directory didn't // exist, the package installation would fail. The .deb must // then have an entry for ./usr/lib/foo and then ./usr/lib/foo/bar if (dirname == null) { return; } // The loop below will create entries for all parent directories // to ensure that .deb packages will install correctly. String[] pathParts = dirname.split("/"); String parentDir = "./"; for (int i = 1; i < pathParts.length; i++) { parentDir += pathParts[i] + "/"; // Make it so the dirs can be traversed by users. // We could instead try something more granular, like setting the directory // permission to 'rx' for each of the 3 user/group/other read permissions // found on the file being added (ie, only if "other" has read // permission on the main node, then add o+rx permission on all the containing // directories, same w/ user & group), and then also we'd have to // check the parentDirs collection of those already added to // see if those permissions need to be similarly updated. (Note, it hasn't // been demonstrated, but there might be a bug if a user specifically // requests a directory with certain permissions, // that has already been auto-created because it was a parent, and if so, go set // the user-requested mode on that directory instead of this automatic one.) // But for now, keeping it simple by making every dir a+rx. Examples are: // drw-r----- fs/fs # what you get with setMode(mode) // drwxr-xr-x fs/fs # Usable. Too loose? int mode = TarArchiveEntry.DEFAULT_DIR_MODE; createDirectory(parentDir, user, uid, group, gid, mode, 0); } } }; try { for (DataProducer data : producers) { data.produce(receiver); } } finally { tarOutputStream.close(); } console.info("Total size: " + dataSize); return dataSize.count; }
From source file:org.vafer.jdeb.producers.DataProducerDirectory.java
public void produce(final DataConsumer pReceiver) throws IOException { scanner.scan();/*from w ww. j ava2 s .co m*/ final File baseDir = scanner.getBasedir(); for (String dir : scanner.getIncludedDirectories()) { final File file = new File(baseDir, dir); String dirname = getFilename(baseDir, file); if ("".equals(dirname)) { continue; } if (!isIncluded(dirname)) { continue; } if ('/' != File.separatorChar) { dirname = dirname.replace(File.separatorChar, '/'); } if (!dirname.endsWith("/")) { dirname += "/"; } TarArchiveEntry entry = new TarArchiveEntry(dirname, true); entry.setUserId(0); entry.setUserName("root"); entry.setGroupId(0); entry.setGroupName("root"); entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); entry = map(entry); entry.setSize(0); pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); } for (String f : scanner.getIncludedFiles()) { final File file = new File(baseDir, f); String filename = getFilename(baseDir, file); if (!isIncluded(filename)) { continue; } if ('/' != File.separatorChar) { filename = filename.replace(File.separatorChar, '/'); } TarArchiveEntry entry = new TarArchiveEntry(filename, true); entry.setUserId(0); entry.setUserName("root"); entry.setGroupId(0); entry.setGroupName("root"); entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE); entry = map(entry); entry.setSize(file.length()); final InputStream inputStream = new FileInputStream(file); try { pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); } finally { inputStream.close(); } } }
From source file:org.vafer.jdeb.producers.DataProducerFile.java
public void produce(final DataConsumer pReceiver) throws IOException { String fileName;//from ww w. j a va 2 s.c o m if (destinationName != null && destinationName.trim().length() > 0) { fileName = destinationName.trim(); } else { fileName = file.getName(); } TarArchiveEntry entry = new TarArchiveEntry(fileName, true); entry.setUserId(0); entry.setUserName("root"); entry.setGroupId(0); entry.setGroupName("root"); entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE); entry = map(entry); entry.setSize(file.length()); final InputStream inputStream = new FileInputStream(file); try { pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); } finally { inputStream.close(); } }
From source file:org.vafer.jdeb.producers.DataProducerPathTemplate.java
public void produce(DataConsumer pReceiver) throws IOException { for (String literalPath : literalPaths) { TarArchiveEntry entry = new TarArchiveEntry(literalPath, true); entry.setUserId(0);/*from w w w . j av a2s . com*/ entry.setUserName("root"); entry.setGroupId(0); entry.setGroupName("root"); entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); entry = map(entry); entry.setSize(0); pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); } }
From source file:org.vafer.jdeb.producers.Producers.java
/** * Forwards tar archive entry entry to a consumer. * @param consumer the consumer/*from www . java2 s . c o m*/ * @param entry the entry to pass * @throws IOException */ static void produceDirEntry(final DataConsumer consumer, final TarArchiveEntry entry) throws IOException { consumer.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); }
From source file:org.vafer.jdeb.producers.Producers.java
/** * Feeds input stream to data consumer using metadata from tar entry. * @param consumer the consumer/* ww w.j a v a 2s . c o m*/ * @param inputStream the stream to feed * @param entry the entry to use for metadata * @throws IOException on consume error */ static void produceInputStreamWithEntry(final DataConsumer consumer, final InputStream inputStream, final TarArchiveEntry entry) throws IOException { try { consumer.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize()); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:org.xenmaster.setup.debian.Bootstrapper.java
protected boolean downloadNetbootFiles() { if (System.currentTimeMillis() - lastChecked < 50 * 60 * 1000) { return false; }/*from ww w. j av a2s .c om*/ File localVersionFile = new File(Settings.getInstance().getString("StorePath") + "/netboot/version"); int localVersion = -1; try (FileInputStream fis = new FileInputStream(localVersionFile)) { localVersion = Integer.parseInt(IOUtils.toString(fis)); } catch (IOException | NumberFormatException ex) { Logger.getLogger(getClass()).error("Failed to retrieve local version file", ex); } int remoteVersion = -1; try { remoteVersion = Integer.parseInt(IOUtils.toString(XenMasterSite.getFileAsStream("/netboot/version"))); } catch (IOException | NumberFormatException ex) { Logger.getLogger(getClass()).error("Failed to retrieve remote version file", ex); } lastChecked = System.currentTimeMillis(); if (localVersion < remoteVersion) { Logger.getLogger(getClass()) .info("New version " + remoteVersion + " found. Please hold while downloading netboot data"); try { TarArchiveInputStream tais = new TarArchiveInputStream( new GZIPInputStream(XenMasterSite.getFileAsStream("/netboot/netboot.tar.gz"))); TarArchiveEntry tae = null; FileOutputStream fos = null; while ((tae = tais.getNextTarEntry()) != null) { if (tais.canReadEntryData(tae)) { String target = Settings.getInstance().getString("StorePath") + "/" + tae.getName(); if (tae.isSymbolicLink()) { Path targetFile = FileSystems.getDefault().getPath(tae.getLinkName()); Path linkName = FileSystems.getDefault().getPath(target); // Link might already have been written as null file if (targetFile.toFile().exists()) { targetFile.toFile().delete(); } Files.createSymbolicLink(linkName, targetFile); Logger.getLogger(getClass()).info( "Created sym link " + linkName.toString() + " -> " + targetFile.toString()); } else if (tae.isDirectory()) { new File(target).mkdir(); Logger.getLogger(getClass()).info("Created dir " + target); } else if (tae.isFile()) { fos = new FileOutputStream(target); byte[] b = new byte[1024]; int curPos = 0; while (tais.available() > 0) { tais.read(b, curPos, 1024); fos.write(b, curPos, 1024); } fos.flush(); fos.close(); Logger.getLogger(getClass()).info("Wrote file " + target); } } } tais.close(); // Write local version IOUtils.write("" + remoteVersion, new FileOutputStream(localVersionFile)); } catch (IOException ex) { Logger.getLogger(getClass()).error("Failed to download netboot files", ex); } } else { return false; } return true; }