List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java
protected String getArtifactPath(DeploymentArtifact artifact) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact()))); String jarPath = null;//from ww w.j a v a 2s . c om try { TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().startsWith("bin/") || entry.getName().endsWith(".war")) { boolean isWar = entry.getName().endsWith(".war"); File tmpFile = File.createTempFile(Files.getNameWithoutExtension(entry.getName()), isWar ? ".war" : ".jar"); FileOutputStream fos = new FileOutputStream(tmpFile); try { IOUtils.copy(tarIn, fos); } finally { IOUtils.closeQuietly(fos); } jarPath = tmpFile.getAbsolutePath(); break; } entry = tarIn.getNextTarEntry(); } } finally { IOUtils.closeQuietly(tarIn); } return jarPath; }
From source file:gzipper.algorithms.Gzip.java
@Override protected void extract(String path, String name) throws IOException { try (TarArchiveInputStream tis = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(path + name))))) { ArchiveEntry entry = tis.getNextEntry(); /*create main folder of gzip archive*/ File folder = new File(Settings._outputPath + name.substring(0, 7)); if (!folder.exists()) { folder.mkdir();/* ww w . j a v a2 s .c o m*/ } while (entry != null & _runFlag) { String entryName = entry.getName(); /*check if entry contains a directory*/ if (entryName.contains("/")) { File newFile; if (Settings._isUnix) { //check OS for correct file path newFile = new File(folder.getAbsolutePath() + "/" + entryName); } else { newFile = new File(folder.getAbsolutePath() + "\\" + entryName); } /*mkdirs also creates parent directories*/ if (!newFile.getParentFile().exists()) { newFile.getParentFile().mkdirs(); } } String newFilePath; if (Settings._isUnix) { //check OS for correct file path newFilePath = folder.getAbsolutePath() + "/" + entryName; } else { newFilePath = folder.getAbsolutePath() + "\\" + entryName; } /*create new OutputStream and write bytes to file*/ try (BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(newFilePath))) { byte[] buffer = new byte[4096]; int readBytes; while ((readBytes = tis.read(buffer)) != -1) { buf.write(buffer, 0, readBytes); } } entry = tis.getNextEntry(); } } }
From source file:com.ibm.util.merge.CompareArchives.java
/** * @param archive//from ww w . j a v a2s . co m * @return * @throws IOException */ private static final HashMap<String, TarArchiveEntry> getMembers(String archive) throws IOException { TarArchiveInputStream input = new TarArchiveInputStream( new BufferedInputStream(new FileInputStream(archive))); TarArchiveEntry entry; HashMap<String, TarArchiveEntry> map = new HashMap<>(); while ((entry = input.getNextTarEntry()) != null) { map.put(entry.getName(), entry); } input.close(); return map; }
From source file:jetbrains.exodus.util.CompressBackupUtilTest.java
@Test public void testFolderArchived() throws Exception { File src = new File(randName); src.mkdir();//w ww .j a v a 2 s . c o m FileWriter fw = new FileWriter(new File(src, "1.txt")); fw.write("12345"); fw.close(); fw = new FileWriter(new File(src, "2.txt")); fw.write("12"); fw.close(); CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry1 = tai.getNextEntry(); ArchiveEntry entry2 = tai.getNextEntry(); if (entry1.getName().compareTo(entry2.getName()) > 0) { // kinda sort them lol ArchiveEntry tmp = entry1; entry1 = entry2; entry2 = tmp; } Assert.assertNotNull("No entry found in destination archive", entry1); Assert.assertEquals("Entry has wrong size", 5, entry1.getSize()); System.out.println(entry1.getName()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName()); System.out.println(entry2.getName()); Assert.assertEquals("Entry has wrong size", 2, entry2.getSize()); Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName()); }
From source file:ezbake.deployer.publishers.SecurityServiceClient.java
/** * The applicationId is part of the REST call to the Security Service. * Returns an empty list if no certificates were found or throws exception if * something went wrong with Security Service communication. *///from w w w . ja va 2s . co m @Override public List<ArtifactDataEntry> get(String applicationId, String securityId) throws DeploymentException { try { ArrayList<ArtifactDataEntry> certList = new ArrayList<ArtifactDataEntry>(); String endpoint = config.getSecurityServiceBasePath() + "/registrations/" + securityId + "/download"; URL url = new URL(endpoint); HttpsURLConnection urlConn = openUrlConnection(url); urlConn.connect(); // Read in tar file of certificates into byte array BufferedInputStream in = new BufferedInputStream(urlConn.getInputStream()); // Create CertDataEntry list from tarFile byte array TarArchiveInputStream tarIn = new TarArchiveInputStream(in); TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (!entry.isDirectory()) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); IOUtils.copy(tarIn, bout); byte[] certData = bout.toByteArray(); String certFileName = entry.getName().substring(entry.getName().lastIndexOf("/") + 1, entry.getName().length()); TarArchiveEntry tae = new TarArchiveEntry( Files.get(SSL_CONFIG_DIRECTORY, securityId, certFileName)); ArtifactDataEntry cde = new ArtifactDataEntry(tae, certData); certList.add(cde); bout.close(); } entry = tarIn.getNextTarEntry(); } tarIn.close(); return certList; } catch (Exception e) { log.error("Unable to download certificates from security service.", e); throw new DeploymentException( "Unable to download certificates from security service. " + e.getMessage()); } }
From source file:io.crate.testing.Utils.java
static void uncompressTarGZ(File tarFile, File dest) throws IOException { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { Path entryPath = Paths.get(tarEntry.getName()); if (entryPath.getNameCount() == 1) { tarEntry = tarIn.getNextTarEntry(); continue; }// ww w. ja v a2 s. c o m Path strippedPath = entryPath.subpath(1, entryPath.getNameCount()); File destPath = new File(dest, strippedPath.toString()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); if (destPath.getParent().equals(dest.getPath() + "/bin")) { destPath.setExecutable(true); } } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }
From source file:com.fizzed.stork.deploy.Archive.java
static private ArchiveInputStream newArchiveInputStream(Path file) throws IOException { String name = file.getFileName().toString(); if (name.endsWith(".tar.gz")) { return new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(file), true)); } else if (name.endsWith(".zip")) { return new ZipArchiveInputStream(Files.newInputStream(file)); } else {//from w w w . j a v a2 s. c o m throw new IOException("Unsupported archive file type (we support .tar.gz and .zip)"); } }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry;//from w w w. j a va2 s . c om int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; } Path outFile = outDir.resolve(entryNameWithoutLeadingDir); if (entry.isDirectory()) { outFile.toFile().mkdirs(); continue; } int count; byte data[] = new byte[bufferSize]; BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()), bufferSize); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { fios.write(data, 0, count); } fios.close(); } tarInput.close(); gzipStream.close(); }
From source file:cc.arduino.utils.ArchiveExtractor.java
public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite) throws IOException, InterruptedException { // Folders timestamps must be set at the end of archive extraction // (because creating a file in a folder alters the folder's timestamp) Map<File, Long> foldersTimestamps = new HashMap<>(); ArchiveInputStream in = null;// ww w .ja va2s.c o m try { // Create an ArchiveInputStream with the correct archiving algorithm if (archiveFile.getName().endsWith("tar.bz2")) { in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile))); } else if (archiveFile.getName().endsWith("zip")) { in = new ZipArchiveInputStream(new FileInputStream(archiveFile)); } else if (archiveFile.getName().endsWith("tar.gz")) { in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile))); } else if (archiveFile.getName().endsWith("tar")) { in = new TarArchiveInputStream(new FileInputStream(archiveFile)); } else { throw new IOException("Archive format not supported."); } String pathPrefix = ""; Map<File, File> hardLinks = new HashMap<>(); Map<File, Integer> hardLinksMode = new HashMap<>(); Map<File, String> symLinks = new HashMap<>(); Map<File, Long> symLinksModifiedTimes = new HashMap<>(); // Cycle through all the archive entries while (true) { ArchiveEntry entry = in.getNextEntry(); if (entry == null) { break; } // Extract entry info long size = entry.getSize(); String name = entry.getName(); boolean isDirectory = entry.isDirectory(); boolean isLink = false; boolean isSymLink = false; String linkName = null; Integer mode = null; long modifiedTime = entry.getLastModifiedDate().getTime(); { // Skip MacOSX metadata // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x int slash = name.lastIndexOf('/'); if (slash == -1) { if (name.startsWith("._")) { continue; } } else { if (name.substring(slash + 1).startsWith("._")) { continue; } } } // Skip git metadata // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html if (name.contains("pax_global_header")) { continue; } if (entry instanceof TarArchiveEntry) { TarArchiveEntry tarEntry = (TarArchiveEntry) entry; mode = tarEntry.getMode(); isLink = tarEntry.isLink(); isSymLink = tarEntry.isSymbolicLink(); linkName = tarEntry.getLinkName(); } // On the first archive entry, if requested, detect the common path // prefix to be stripped from filenames if (stripPath > 0 && pathPrefix.isEmpty()) { int slash = 0; while (stripPath > 0) { slash = name.indexOf("/", slash); if (slash == -1) { throw new IOException("Invalid archive: it must contain a single root folder"); } slash++; stripPath--; } pathPrefix = name.substring(0, slash); } // Strip the common path prefix when requested if (!name.startsWith(pathPrefix)) { throw new IOException("Invalid archive: it must contain a single root folder while file " + name + " is outside " + pathPrefix); } name = name.substring(pathPrefix.length()); if (name.isEmpty()) { continue; } File outputFile = new File(destFolder, name); File outputLinkedFile = null; if (isLink) { if (!linkName.startsWith(pathPrefix)) { throw new IOException("Invalid archive: it must contain a single root folder while file " + linkName + " is outside " + pathPrefix); } linkName = linkName.substring(pathPrefix.length()); outputLinkedFile = new File(destFolder, linkName); } if (isSymLink) { // Symbolic links are referenced with relative paths outputLinkedFile = new File(linkName); if (outputLinkedFile.isAbsolute()) { System.err.println(I18n.format(tr("Warning: file {0} links to an absolute path {1}"), outputFile, outputLinkedFile)); System.err.println(); } } // Safety check if (isDirectory) { if (outputFile.isFile() && !overwrite) { throw new IOException( "Can't create folder " + outputFile + ", a file with the same name exists!"); } } else { // - isLink // - isSymLink // - anything else if (outputFile.exists() && !overwrite) { throw new IOException("Can't extract file " + outputFile + ", file already exists!"); } } // Extract the entry if (isDirectory) { if (!outputFile.exists() && !outputFile.mkdirs()) { throw new IOException("Could not create folder: " + outputFile); } foldersTimestamps.put(outputFile, modifiedTime); } else if (isLink) { hardLinks.put(outputFile, outputLinkedFile); hardLinksMode.put(outputFile, mode); } else if (isSymLink) { symLinks.put(outputFile, linkName); symLinksModifiedTimes.put(outputFile, modifiedTime); } else { // Create the containing folder if not exists if (!outputFile.getParentFile().isDirectory()) { outputFile.getParentFile().mkdirs(); } copyStreamToFile(in, size, outputFile); outputFile.setLastModified(modifiedTime); } // Set file/folder permission if (mode != null && !isSymLink && outputFile.exists()) { platform.chmod(outputFile, mode); } } for (Map.Entry<File, File> entry : hardLinks.entrySet()) { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } platform.link(entry.getValue(), entry.getKey()); Integer mode = hardLinksMode.get(entry.getKey()); if (mode != null) { platform.chmod(entry.getKey(), mode); } } for (Map.Entry<File, String> entry : symLinks.entrySet()) { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } platform.symlink(entry.getValue(), entry.getKey()); entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey())); } } finally { IOUtils.closeQuietly(in); } // Set folders timestamps for (File folder : foldersTimestamps.keySet()) { folder.setLastModified(foldersTimestamps.get(folder)); } }
From source file:io.fabric8.spi.process.AbstractProcessHandler.java
@Override public final ManagedProcess create(AgentRegistration agentReg, ProcessOptions options, ProcessIdentity identity) {/*from w w w . ja va2s . c o m*/ File targetDir = options.getTargetPath().toAbsolutePath().toFile(); IllegalStateAssertion.assertTrue(targetDir.isDirectory() || targetDir.mkdirs(), "Cannot create target dir: " + targetDir); File homeDir = null; for (MavenCoordinates artefact : options.getMavenCoordinates()) { Resource resource = mavenRepository.findMavenResource(artefact); IllegalStateAssertion.assertNotNull(resource, "Cannot find maven resource: " + artefact); ResourceContent content = resource.adapt(ResourceContent.class); IllegalStateAssertion.assertNotNull(content, "Cannot obtain resource content for: " + artefact); try { ArchiveInputStream ais; if ("tar.gz".equals(artefact.getType())) { InputStream inputStream = content.getContent(); ais = new TarArchiveInputStream(new GZIPInputStream(inputStream)); } else { InputStream inputStream = content.getContent(); ais = new ArchiveStreamFactory().createArchiveInputStream(artefact.getType(), inputStream); } ArchiveEntry entry = null; boolean needContainerHome = homeDir == null; while ((entry = ais.getNextEntry()) != null) { File targetFile; if (needContainerHome) { targetFile = new File(targetDir, entry.getName()); } else { targetFile = new File(homeDir, entry.getName()); } if (!entry.isDirectory()) { File parentDir = targetFile.getParentFile(); IllegalStateAssertion.assertTrue(parentDir.exists() || parentDir.mkdirs(), "Cannot create target directory: " + parentDir); FileOutputStream fos = new FileOutputStream(targetFile); copyStream(ais, fos); fos.close(); if (needContainerHome && homeDir == null) { File currentDir = parentDir; while (!currentDir.getParentFile().equals(targetDir)) { currentDir = currentDir.getParentFile(); } homeDir = currentDir; } } } ais.close(); } catch (RuntimeException rte) { throw rte; } catch (Exception ex) { throw new IllegalStateException("Cannot extract artefact: " + artefact, ex); } } managedProcess = new DefaultManagedProcess(identity, options, homeDir.toPath(), State.CREATED); managedProcess.addAttribute(ManagedProcess.ATTRIBUTE_KEY_AGENT_REGISTRATION, agentReg); managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_SERVER_URL, agentReg.getJmxServerUrl()); managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_USERNAME, agentReg.getJmxUsername()); managedProcess.addAttribute(ContainerAttributes.ATTRIBUTE_KEY_AGENT_JMX_PASSWORD, agentReg.getJmxPassword()); try { doConfigure(managedProcess); } catch (Exception ex) { throw new LifecycleException("Cannot configure container", ex); } return new ImmutableManagedProcess(managedProcess); }