List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:org.openmrs.module.openconceptlab.client.OclClient.java
@SuppressWarnings("resource") public OclResponse ungzipAndUntarResponse(InputStream response, Date date) throws IOException { GZIPInputStream gzipIn = new GZIPInputStream(response); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn); boolean foundEntry = false; try {/*from w w w . j a v a 2 s. co m*/ TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().equals("export.json")) { foundEntry = true; return new OclResponse(tarIn, entry.getSize(), date); } entry = tarIn.getNextTarEntry(); } tarIn.close(); } finally { if (!foundEntry) { IOUtils.closeQuietly(tarIn); } } throw new IOException("Unsupported format of response. Expected tar.gz archive with export.json."); }
From source file:org.openo.nfvo.jujuvnfmadapter.common.UnCompressUtil.java
/** * tar.gz//from w w w . j a v a 2 s. c o m * <br/> * * @param zipfileName * @param outputDirectory * @param fileNames * @return * @since NFVO 0.5 */ public static boolean unCompressGzip(String zipfileName, String outputDirectory, List<String> fileNames) { FileInputStream fis = null; ArchiveInputStream in = null; BufferedInputStream bis = null; try { fis = new FileInputStream(zipfileName); GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(fis)); in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis); bis = new BufferedInputStream(in); TarArchiveEntry entry = (TarArchiveEntry) in.getNextEntry(); while (entry != null) { String name = entry.getName(); String[] names = name.split("/"); String fileName = outputDirectory; for (int i = 0; i < names.length; i++) { String str = names[i]; fileName = fileName + File.separator + str; } if (name.endsWith("/")) { FileUtils.mkDirs(fileName); } else { File file = getRealFileName(outputDirectory, name); if (null != fileNames) { fileNames.add(file.getName()); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } log.debug("ungzip to:" + file.getCanonicalPath()); bos.flush(); bos.close(); } entry = (TarArchiveEntry) in.getNextEntry(); } return true; } catch (Exception e) { log.error("UnCompressGZip faield:", e); return false; } finally { try { if (null != bis) { bis.close(); } } catch (IOException e) { log.error("UnCompressGZip faield:", e); } } }
From source file:org.openo.nfvo.jujuvnfmadapter.common.UnCompressUtil.java
/** * tar.xz//from w w w . j a v a 2 s . c o m * <br/> * * @param zipfileName * @param outputDirectory * @param fileNames * @return * @since NFVO 0.5 */ public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List<String> fileNames) { ArchiveInputStream in = null; BufferedInputStream bis = null; try { XZCompressorInputStream xzis = new XZCompressorInputStream( new BufferedInputStream(new FileInputStream(zipfileName))); in = new ArchiveStreamFactory().createArchiveInputStream("tar", xzis); bis = new BufferedInputStream(in); TarArchiveEntry entry = (TarArchiveEntry) in.getNextEntry(); while (entry != null) { String name = entry.getName(); String[] names = name.split("/"); String fileName = outputDirectory; for (int i = 0; i < names.length; i++) { String str = names[i]; fileName = fileName + File.separator + str; } if (name.endsWith("/")) { FileUtils.mkDirs(fileName); } else { File file = getRealFileName(outputDirectory, name); if (null != fileNames) { fileNames.add(file.getName()); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); int b = -1; while ((b = bis.read()) != -1) { bos.write(b); } log.debug("ungzip to:" + file.getCanonicalPath()); bos.flush(); bos.close(); } entry = (TarArchiveEntry) in.getNextEntry(); } return true; } catch (Exception e) { log.error("unCompressTarXZ faield:", e); } finally { try { if (null != bis) { bis.close(); } } catch (IOException e) { log.error("unCompressTarXZ faield:", e); } } return false; }
From source file:org.opensextant.xtext.collectors.ArchiveNavigator.java
public File untar(File tarFile) throws IOException, ConfigException { String _working = FilenameUtils.concat(getWorkingDir(), FilenameUtils.getBaseName(tarFile.getPath())); if (_working == null) { throw new IOException("Invalid archive path for " + tarFile.getPath()); }// w w w.ja v a 2 s.c om File workingDir = new File(_working); workingDir.mkdir(); InputStream input = new BufferedInputStream(new FileInputStream(tarFile)); TarArchiveInputStream in = null; try { in = (TarArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("tar", input)); TarArchiveEntry tarEntry; while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(tarEntry)) { continue; } try { File tmpFile = saveArchiveEntry(tarEntry, in, _working); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err); } } } catch (ArchiveException ae) { throw new IOException(ae); } finally { in.close(); } return workingDir; }
From source file:org.opentestsystem.delivery.testreg.transformer.TarUnbundler.java
public byte[][] unbundle(File bundledFile) throws IOException { TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new FileInputStream(bundledFile)); List<byte[]> byteArrays = new ArrayList<byte[]>(); ByteArrayOutputStream bytesOut = null; TarArchiveEntry entry = null; while ((entry = tarInputStream.getNextTarEntry()) != null) { byte[] buffer = new byte[4096]; int len = 0; bytesOut = new ByteArrayOutputStream(); while ((len = tarInputStream.read(buffer)) > 0) { bytesOut.write(buffer, 0, len); }//w ww .j av a 2 s . c o m byteArrays.add(entry.getName().getBytes()); byteArrays.add(bytesOut.toByteArray()); bytesOut.close(); } tarInputStream.close(); return byteArrays.toArray(new byte[0][]); }
From source file:org.pentaho.webpackage.deployer.archive.impl.UrlTransformer.java
boolean canHandleTarGzFile(File file) { TarArchiveInputStream tarInput = null; try {//from w w w . j a v a 2 s .c o m tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file))); TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); while (currentEntry != null) { if (currentEntry.getName().endsWith(WebPackageURLConnection.PACKAGE_JSON)) { return true; } currentEntry = tarInput.getNextTarEntry(); } } catch (IOException ignored) { // Ignore } finally { if (tarInput != null) { try { tarInput.close(); } catch (IOException ignored) { // Ignore } } } return false; }
From source file:org.pentaho.webpackage.deployer.UrlTransformer.java
@Override public boolean canHandle(File file) { if (!file.exists()) { return false; }// ww w. jav a2 s .c om if (file.getName().endsWith(".tgz")) { TarArchiveInputStream tarInput = null; try { tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file))); TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); while (currentEntry != null) { if (currentEntry.getName().endsWith("package.json")) { return true; } currentEntry = tarInput.getNextTarEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { if (tarInput != null) { try { tarInput.close(); } catch (IOException ignored) { // Ignore } } } return false; } else if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) { ZipFile zipFile = null; try { zipFile = new ZipFile(file); return zipFile.getEntry("package.json") != null && zipFile.getEntry("META-INF/MANIFEST.MF") == null; } catch (IOException e) { this.logger.error(e.getMessage(), e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ignored) { // Ignore } } } } return false; }
From source file:org.phoenicis.tools.archive.Tar.java
/** * Uncompress a tar/*from w ww . j av a2s . c o 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.queeg.hadoop.tar.TarExtractor.java
public void extract(ByteSource source) throws IOException { TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(source.openStream()); TarArchiveEntry entry; while ((entry = archiveInputStream.getNextTarEntry()) != null) { if (entry.isFile()) { BoundedInputStream entryInputStream = new BoundedInputStream(archiveInputStream, entry.getSize()); ByteSink sink = new PathByteSink(conf, new Path(destination, entry.getName())); sink.writeFrom(entryInputStream); } else if (entry.isDirectory()) { ByteStreams.skipFully(archiveInputStream, entry.getSize()); fs.mkdirs(new Path(destination, entry.getName())); }/* w ww.ja va2 s . c o m*/ } archiveInputStream.close(); }
From source file:org.renjin.cran.ProjectBuilder.java
private void unpackSources(File sourceArchive) throws IOException { FileInputStream in = new FileInputStream(sourceArchive); GZIPInputStream gzipIn = new GZIPInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { if (entry.getName().endsWith(".Rd")) { } else if (entry.getName().startsWith(pkg + "/src/") && entry.getSize() != 0) { } else if (entry.getName().startsWith(pkg + "/R/") && entry.getSize() != 0) { extractTo(entry, tarIn, rSourcesDir); } else if (entry.getName().equals(pkg + "/DESCRIPTION")) { extractTo(entry, tarIn, baseDir); } else if (entry.getName().equals(pkg + "/NAMESPACE")) { extractTo(entry, tarIn, baseDir); } else if (entry.getName().startsWith(pkg + "/tests/") && entry.getSize() != 0) { extractTo(entry, tarIn, rTestsDir); } else if (entry.getName().startsWith(pkg + "/data/") && entry.getSize() != 0) { extractTo(entry, tarIn, resourcesDir); addDataset(entry);/*from w ww . jav a2s.c o m*/ } } }