List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isFile
public boolean isFile()
From source file:gobblin.data.management.copy.converter.UnGzipConverterTest.java
private static String readGzipStreamAsString(InputStream is) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream(is); try {/*from w ww .j a va 2 s . c om*/ TarArchiveEntry tarEntry; while ((tarEntry = tarIn.getNextTarEntry()) != null) { if (tarEntry.isFile() && tarEntry.getName().endsWith(".txt")) { return IOUtils.toString(tarIn, "UTF-8"); } } } finally { tarIn.close(); } return Strings.EMPTY; }
From source file:cgs_lda_multicore.DataModel.DataPreparation.java
public static LDADataset readDatasetCORE(String fileName) throws Exception { try {//from w ww . jav a 2 s .c om // Read document file. BufferedReader reader = null; if (fileName.endsWith(".tar.gz")) { // This case read from .tar.gz file. TarArchiveInputStream tAIS = new TarArchiveInputStream( new GZIPInputStream(new FileInputStream(fileName))); TarArchiveEntry tarArchiveEntry; while ((tarArchiveEntry = tAIS.getNextTarEntry()) != null) { if (tarArchiveEntry.isFile()) { reader = new BufferedReader( new InputStreamReader(new FileInputStream(tarArchiveEntry.getFile()), "UTF-8")); String line; while ((line = reader.readLine()) != null) { // Process line, each line is a json of a document. } reader.close(); } } tAIS.close(); } return null; } catch (Exception e) { System.out.println("Read Dataset Error: " + e.getMessage()); e.printStackTrace(); return null; } }
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 ww w .j ava 2s. c om*/ * 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:data.TarExtractorTest.java
@Test public void itExtractsTarFile() throws Exception { TarArchiveInputStream tarArchiveInputStream = mock(TarArchiveInputStream.class); whenNew(TarArchiveInputStream.class).withArguments(any(InputStream.class)) .thenReturn(tarArchiveInputStream); when(tarArchiveInputStream.getNextTarEntry()).thenAnswer(new Answer() { private int count = 0; public Object answer(InvocationOnMock invocationOnMock) { count++;// ww w. j a v a 2s . c o m if (count == 1) { TarArchiveEntry tarArchiveEntry = mock(TarArchiveEntry.class); when(tarArchiveEntry.getName()).thenReturn("data.gpdb"); when(tarArchiveEntry.isFile()).thenReturn(true); return tarArchiveEntry; } if (count == 2) { TarArchiveEntry tarArchiveEntry = mock(TarArchiveEntry.class); when(tarArchiveEntry.getName()).thenReturn("IpV6Data"); when(tarArchiveEntry.isDirectory()).thenReturn(true); return tarArchiveEntry; } return null; } }); File directory = mock(File.class); File fileInTar = spy(mock(File.class)); when(fileInTar.createNewFile()).thenReturn(true); whenNew(File.class).withArguments(directory, "data.gpdb").thenReturn(fileInTar); File directoryInTar = spy(mock(File.class)); when(directoryInTar.createNewFile()).thenReturn(true); whenNew(File.class).withArguments(directory, "IpV6Data").thenReturn(directoryInTar); FileOutputStream fileOutputStream = mock(FileOutputStream.class); whenNew(FileOutputStream.class).withArguments(fileInTar).thenReturn(fileOutputStream); when(tarArchiveInputStream.read(any(byte[].class))).thenAnswer(new Answer() { private int count = 0; public Object answer(InvocationOnMock invocationOnMock) { count++; return (count == 1) ? new Integer(654321) : new Integer(-1); } }); InputStream inputStream1 = mock(InputStream.class); TarExtractor tarExtractor = new TarExtractor(); assertThat(tarExtractor.extractTo(directory, inputStream1), equalTo(true)); verify(fileInTar).createNewFile(); verify(fileOutputStream).write(any(byte[].class), eq(0), eq(654321)); verify(fileOutputStream).close(); verifyNoMoreInteractions(fileOutputStream); verifyZeroInteractions(directoryInTar); }
From source file:io.takari.maven.testing.executor.junit.MavenVersionResolver.java
private void unarchive(File archive, File directory) throws IOException { try (TarArchiveInputStream ais = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(archive)))) { TarArchiveEntry entry; while ((entry = ais.getNextTarEntry()) != null) { if (entry.isFile()) { String name = entry.getName(); File file = new File(directory, name); file.getParentFile().mkdirs(); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { copy(ais, os);/*from w ww .ja v a2s. c om*/ } int mode = entry.getMode(); if (mode != -1 && (mode & 0100) != 0) { try { Path path = file.toPath(); Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(path); permissions.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(path, permissions); } catch (UnsupportedOperationException e) { // must be windows, ignore } } } } } }
From source file:eu.openanalytics.rsb.component.AdminResource.java
private void extractCatalogFiles(final File packageSourceFile) throws IOException { final File tempDirectory = packageSourceFile.getParentFile(); // 1) extract TAR final File packageTarFile = File.createTempFile("rsb-install.", ".tar", tempDirectory); final GzipCompressorInputStream gzIn = new GzipCompressorInputStream( new FileInputStream(packageSourceFile)); FileOutputStream output = new FileOutputStream(packageTarFile); IOUtils.copyLarge(gzIn, output);//from w ww . ja v a2 s . c om IOUtils.closeQuietly(output); IOUtils.closeQuietly(gzIn); // 2) parse TAR and drop files in catalog final TarArchiveInputStream tarIn = new TarArchiveInputStream(new FileInputStream(packageTarFile)); TarArchiveEntry tarEntry = null; while ((tarEntry = tarIn.getNextTarEntry()) != null) { if (!tarEntry.isFile()) { continue; } final Matcher matcher = TAR_CATALOG_FILE_PATTERN.matcher(tarEntry.getName()); if (matcher.matches()) { final byte[] data = IOUtils.toByteArray(tarIn, tarEntry.getSize()); final String catalogFile = matcher.group(1); final File targetCatalogFile = new File(getConfiguration().getCatalogRootDirectory(), catalogFile); output = new FileOutputStream(targetCatalogFile); IOUtils.write(data, output); IOUtils.closeQuietly(output); getLogger().info("Wrote " + data.length + " bytes in catalog file: " + targetCatalogFile); } } IOUtils.closeQuietly(tarIn); }
From source file:com.arturmkrtchyan.kafka.util.TarUnpacker.java
/** * Unpack data from the stream to specified directory. * * @param in stream with tar data * @param outputPath destination path// w w w .j a v a 2s . com * @return true in case of success, otherwise - false */ protected boolean unpack(final TarArchiveInputStream in, final Path outputPath) throws IOException { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { final Path entryPath = outputPath.resolve(entry.getName()); if (entry.isDirectory()) { if (!Files.exists(entryPath)) { Files.createDirectories(entryPath); } } else if (entry.isFile()) { try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryPath))) { IOUtils.copy(in, out); setPermissions(entry.getMode(), entryPath); } } } return true; }
From source file:com.blackducksoftware.integration.hub.docker.tar.DockerTarParser.java
public List<File> extractLayerTars(final File dockerTar) throws IOException { final File tarExtractionDirectory = getTarExtractionDirectory(); final List<File> untaredFiles = new ArrayList<>(); final File outputDir = new File(tarExtractionDirectory, dockerTar.getName()); final TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream( new FileInputStream(dockerTar)); try {/*from ww w . j a va2 s . c o m*/ TarArchiveEntry tarArchiveEntry = null; while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) { final File outputFile = new File(outputDir, tarArchiveEntry.getName()); if (tarArchiveEntry.isFile()) { if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } final OutputStream outputFileStream = new FileOutputStream(outputFile); try { IOUtils.copy(tarArchiveInputStream, outputFileStream); if (tarArchiveEntry.getName().contains(DOCKER_LAYER_TAR_FILENAME)) { untaredFiles.add(outputFile); } } finally { outputFileStream.close(); } } } } finally { IOUtils.closeQuietly(tarArchiveInputStream); } return untaredFiles; }
From source file:com.surevine.gateway.scm.IncomingProcessorImpl.java
public Collection<Path> extractTarGz(final Path archivePath) throws IOException { TarArchiveInputStream archive = openTarGz(archivePath); if (!tarGzHasExpectedContents(archive)) { LOGGER.debug(archivePath.toString() + " does not have the correct contents - exactly one .bundle and exactly one .metadata.json"); return null; }//from ww w . j av a 2 s . com // We need to re-open the archive as the Iterator implementation // has #reset as a no-op archive.close(); archive = openTarGz(archivePath); final Path tmpDir = getTmpExtractionPath(archivePath); Files.createDirectories(tmpDir); final Collection<Path> extractedFiles = new ArrayList<Path>(); LOGGER.debug("Extracting " + archivePath.toString() + " to " + tmpDir); final File tmp = tmpDir.toFile(); TarArchiveEntry entry = archive.getNextTarEntry(); while (entry != null) { final File outputFile = new File(tmp, entry.getName()); if (!entry.isFile()) { continue; } LOGGER.debug("Creating output file " + outputFile.getAbsolutePath()); registerCreatedFile(outputFile); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(archive, outputFileStream); outputFileStream.close(); extractedFiles.add(outputFile.toPath()); entry = archive.getNextTarEntry(); } archive.close(); return extractedFiles; }
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 w w . j av a2s . c o 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; }