List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:com.cloudera.cli.validator.components.ParcelFileRunner.java
@Override public boolean run(String target, Writer writer) throws IOException { File parcelFile = new File(target); writer.write(String.format("Validating: %s\n", parcelFile.getPath())); if (!checkExistence(parcelFile, false, writer)) { return false; }/*from w ww .j a v a 2 s. com*/ String expectedDir; String distro; Matcher parcelMatcher = PARCEL_PATTERN.matcher(parcelFile.getName()); if (parcelMatcher.find()) { expectedDir = parcelMatcher.group(1) + '-' + parcelMatcher.group(2); distro = parcelMatcher.group(3); } else { writer.write(String.format("==> %s is not a valid parcel filename\n", parcelFile.getName())); return false; } if (!KNOWN_DISTROS.contains(distro)) { writer.write(String.format("==> %s does not appear to be a distro supported by CM\n", distro)); } FileInputStream fin = null; BufferedInputStream bin = null; GzipCompressorInputStream gin = null; TarArchiveInputStream tin = null; try { InputStream in = null; fin = new FileInputStream(parcelFile); bin = new BufferedInputStream(fin); try { gin = new GzipCompressorInputStream(bin); in = gin; } catch (IOException e) { // It's not compressed. Proceed as if uncompressed tar. writer.write(String.format("==> Warning: Parcel is not compressed with gzip\n")); in = bin; } tin = new TarArchiveInputStream(in); byte[] parcelJson = null; byte[] alternativesJson = null; byte[] permissionsJson = null; Map<String, Boolean> tarEntries = Maps.newHashMap(); Set<String> unexpectedDirs = Sets.newHashSet(); for (TarArchiveEntry e = tin.getNextTarEntry(); e != null; e = tin.getNextTarEntry()) { String name = e.getName(); // Remove trailing '/' tarEntries.put(name.replaceAll("/$", ""), e.isDirectory()); if (!StringUtils.startsWith(name, expectedDir)) { unexpectedDirs.add(name.split("/")[0]); } if (e.getName().equals(expectedDir + PARCEL_JSON_PATH)) { parcelJson = new byte[(int) e.getSize()]; tin.read(parcelJson); } else if (e.getName().equals(expectedDir + ALTERNATIVES_JSON_PATH)) { alternativesJson = new byte[(int) e.getSize()]; tin.read(alternativesJson); } else if (e.getName().equals(expectedDir + PERMISSIONS_JSON_PATH)) { permissionsJson = new byte[(int) e.getSize()]; tin.read(permissionsJson); } } boolean ret = true; if (!unexpectedDirs.isEmpty()) { writer.write(String.format("==> The following unexpected top level directories were observed: %s\n", unexpectedDirs.toString())); writer.write( String.format("===> The only valid top level directory, based on parcel filename, is: %s\n", expectedDir)); ret = false; } ret &= checkParcelJson(expectedDir, parcelJson, tarEntries, writer); ret &= checkAlternatives(expectedDir, alternativesJson, tarEntries, writer); ret &= checkPermissions(expectedDir, permissionsJson, tarEntries, writer); return ret; } catch (IOException e) { writer.write(String.format("==> %s: %s\n", e.getClass().getName(), e.getMessage())); return false; } finally { IOUtils.closeQuietly(tin); IOUtils.closeQuietly(gin); IOUtils.closeQuietly(bin); IOUtils.closeQuietly(fin); } }
From source file:com.facebook.buck.artifact_cache.ArtifactUploaderTest.java
/** compressSavesExecutableBit asserts that compress()-ing an executable file stores the x bit. */ @Test/*w w w. j av a2 s . com*/ public void compressSavesExecutableBit() throws Exception { ProjectFilesystem fs = FakeProjectFilesystem.createJavaOnlyFilesystem("/"); Path out = fs.getRootPath().resolve("out"); Path file = fs.getRootPath().resolve("file"); fs.writeContentsToPath("foo", file); Files.setPosixFilePermissions(fs.getPathForRelativePath(file), ImmutableSet.of(PosixFilePermission.OWNER_EXECUTE)); // Compress ArtifactUploader.compress(fs, ImmutableList.of(file), out); // Decompress+unarchive, and check that the only file is an executable. try (TarArchiveInputStream fin = new TarArchiveInputStream( new ZstdCompressorInputStream(Files.newInputStream(out)))) { ArrayList<TarArchiveEntry> entries = new ArrayList<>(); TarArchiveEntry entry; while ((entry = fin.getNextTarEntry()) != null) { entries.add(entry); } assertThat(entries, Matchers.hasSize(1)); assertThat(MorePosixFilePermissions.fromMode(entries.get(0).getMode()), Matchers.contains(PosixFilePermission.OWNER_EXECUTE)); } }
From source file:com.opentable.db.postgres.embedded.BundledPostgresBinaryResolver.java
/** * Unpack archive compressed by tar with bzip2 compression. By default system tar is used * (faster). If not found, then the java implementation takes place. * * @param tbzPath//from w w w . j a v a2s . c o m * The archive path. * @param targetDir * The directory to extract the content to. */ private static void extractTxz(String tbzPath, String targetDir) throws IOException { try (InputStream in = Files.newInputStream(Paths.get(tbzPath)); XZInputStream xzIn = new XZInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)) { TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String individualFile = entry.getName(); final File fsObject = new File(targetDir + "/" + individualFile); if (entry.isSymbolicLink() || entry.isLink()) { Path target = FileSystems.getDefault().getPath(entry.getLinkName()); Files.createSymbolicLink(fsObject.toPath(), target); } else if (entry.isFile()) { byte[] content = new byte[(int) entry.getSize()]; int read = tarIn.read(content, 0, content.length); Verify.verify(read != -1, "could not read %s", individualFile); mkdirs(fsObject.getParentFile()); try (OutputStream outputFile = new FileOutputStream(fsObject)) { IOUtils.write(content, outputFile); } } else if (entry.isDirectory()) { mkdirs(fsObject); } else { throw new UnsupportedOperationException( String.format("Unsupported entry found: %s", individualFile)); } if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) { fsObject.setExecutable(true); } } } }
From source file:algorithm.TarPackaging.java
@Override protected List<RestoredFile> restore(File tarFile) throws IOException { List<RestoredFile> extractedFiles = new ArrayList<RestoredFile>(); FileInputStream inputStream = new FileInputStream(tarFile); if (GzipUtils.isCompressedFilename(tarFile.getName())) { TarArchiveInputStream tarInputStream = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(inputStream))); TarArchiveEntry entry = tarInputStream.getNextTarEntry(); while (entry != null) { RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { outputFile.mkdirs(); }//from w ww . j av a2s .co m } else if (entry.isFile()) { FileOutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(tarInputStream, outputFileStream); outputFileStream.close(); } extractedFiles.add(outputFile); entry = tarInputStream.getNextTarEntry(); } tarInputStream.close(); } else if (BZip2Utils.isCompressedFilename(tarFile.getName())) { TarArchiveInputStream tarInputStream = new TarArchiveInputStream( new BZip2CompressorInputStream(new BufferedInputStream(inputStream))); TarArchiveEntry entry = tarInputStream.getNextTarEntry(); while (entry != null) { RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { outputFile.mkdirs(); } } else if (entry.isFile()) { FileOutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(tarInputStream, outputFileStream); outputFileStream.close(); } extractedFiles.add(outputFile); entry = tarInputStream.getNextTarEntry(); } tarInputStream.close(); } else { TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new BufferedInputStream(inputStream)); TarArchiveEntry entry = tarInputStream.getNextTarEntry(); while (entry != null) { RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { outputFile.mkdirs(); } } else if (entry.isFile()) { FileOutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(tarInputStream, outputFileStream); outputFileStream.close(); } extractedFiles.add(outputFile); entry = tarInputStream.getNextTarEntry(); } tarInputStream.close(); } for (RestoredFile file : extractedFiles) { file.algorithm = this; file.checksumValid = true; file.restorationNote = "The algorithm doesn't alter these files, so they are brought to its original state. No checksum validation executed."; // All files in a .tar file are payload files: file.wasPayload = true; for (RestoredFile relatedFile : extractedFiles) { if (file != relatedFile) { file.relatedFiles.add(relatedFile); } } } return extractedFiles; }
From source file:com.codenvy.commons.lang.TarUtils.java
public static void untar(InputStream in, File targetDir) throws IOException { final TarArchiveInputStream tarIn = new TarArchiveInputStream(in); byte[] b = new byte[BUF_SIZE]; TarArchiveEntry tarEntry;//www .j a v a 2 s . co m while ((tarEntry = tarIn.getNextTarEntry()) != null) { final File file = new File(targetDir, tarEntry.getName()); if (tarEntry.isDirectory()) { file.mkdirs(); } else { final File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } try (FileOutputStream fos = new FileOutputStream(file)) { int r; while ((r = tarIn.read(b)) != -1) { fos.write(b, 0, r); } } } } }
From source file:gdt.jgui.base.JBasesPanel.java
static void refreshListProcedure(JMainConsole console, String entihome$) { try {// w w w . j a v a 2 s .c o m InputStream is = JProcedurePanel.class.getResourceAsStream("list.tar"); TarArchiveInputStream tis = new TarArchiveInputStream(is); ArchiveHandler.extractEntitiesFromTar(entihome$, tis); Entigrator entigrator = console.getEntigrator(entihome$); Sack procedure = entigrator.getEntityAtKey(JProcedurePanel.PROCEDURE_LIST_KEY); String procedureLocator$ = EntityHandler.getEntityLocator(entigrator, procedure); JEntityPrimaryMenu.reindexEntity(console, procedureLocator$); } catch (Exception e) { Logger.getLogger(JQueryPanel.class.getName()).severe(e.toString()); } }
From source file:at.ac.tuwien.big.testsuite.impl.task.UnzipTaskImpl.java
private void untar(File tarFile, File baseDirectory, Collection<Exception> exceptions, boolean gzipped) { try (FileInputStream fis = new FileInputStream(tarFile); TarArchiveInputStream tis = new TarArchiveInputStream( gzipped ? new GzipCompressorInputStream(fis) : fis)) { TarArchiveEntry entry;//from w ww .ja v a2 s. co m while ((entry = tis.getNextTarEntry()) != null) { String escapedEntryName = escapeFileName(entry.getName()); File entryFile = new File(baseDirectory, escapedEntryName); if (entry.isDirectory()) { if (!contains(entryFile.getAbsolutePath(), ignoredDirectories)) { entryFile.mkdir(); } } else if (!endsWith(entryFile.getName(), ignoredExtensions) && !contains(entryFile.getAbsolutePath(), ignoredDirectories)) { if (entryFile.getAbsolutePath().indexOf(File.separatorChar, baseDirectory.getAbsolutePath().length()) > -1) { // Only create dirs if file is not in the root dir entryFile.getParentFile().mkdirs(); } try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(entryFile))) { copyInputStream(tis, outputStream); } } addTotalWork(1); addDoneWork(1); } } catch (Exception ex) { exceptions.add(ex); } }
From source file:ezbake.deployer.publishers.mesos.MesosPublisher.java
/** * Gets the path including file name to the executable Jar file of the app by searching to the bin folder. * * @param artifact - the artifact to get the jar path from * @return - The jar path to the executable jar * @throws Exception/*w ww . j a v a2 s. c om*/ */ private String getJarPath(DeploymentArtifact artifact) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact()))); String jarPath = null; TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().equalsIgnoreCase("bin/")) { entry = tarIn.getNextTarEntry(); jarPath = entry.getName(); break; } entry = tarIn.getNextTarEntry(); } tarIn.close(); return jarPath; }
From source file:com.datos.vfs.provider.tar.TarFileSystem.java
protected TarArchiveInputStream createTarFile(final File file) throws FileSystemException { try {/* www . j a va 2 s .com*/ if ("tgz".equalsIgnoreCase(getRootName().getScheme())) { return new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(file))); } else if ("tbz2".equalsIgnoreCase(getRootName().getScheme())) { return new TarArchiveInputStream( Bzip2FileObject.wrapInputStream(file.getAbsolutePath(), new FileInputStream(file))); } return new TarArchiveInputStream(new FileInputStream(file)); } catch (final IOException ioe) { throw new FileSystemException("vfs.provider.tar/open-tar-file.error", file, ioe); } }
From source file:com.impetus.ankush.common.controller.listener.StartupListener.java
/** * Sets the agent version./*from w w w . j a v a 2s . c o m*/ */ private static void setAgentVersion() { // current agent version String agentBuildVersion = new String(); try { // Resource base path. String basePath = AppStoreWrapper.getResourcePath(); // Creating agent bundle path. String agentBundlePath = basePath + "scripts/agent/" + AgentDeployer.AGENT_BUNDLE_NAME; FileInputStream fileInputStream = new FileInputStream(agentBundlePath); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(bufferedInputStream); TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzInputStream); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (entry.getName().equals(AgentDeployer.AGENT_VERSION_FILENAME)) { final int BUFFER = 10; byte data[] = new byte[BUFFER]; tarInputStream.read(data, 0, BUFFER); String version = new String(data); agentBuildVersion = version.trim(); // Set the agent version in the AppStore with key as // agentVersion AppStore.setObject(AppStoreWrapper.KEY_AGENT_VERISON, agentBuildVersion); } } } catch (Exception e) { // log error message. log.error(e.getMessage(), e); } }