List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:com.amaze.filemanager.filesystem.compressed.extractcontents.helpers.TarExtractor.java
private void extractEntry(@NonNull final Context context, TarArchiveInputStream inputStream, TarArchiveEntry entry, String outputDir) throws IOException { File outputFile = new File(outputDir, fixEntryName(entry.getName())); if (!outputFile.getCanonicalPath().startsWith(outputDir)) { throw new IOException("Incorrect TarArchiveEntry path!"); }// w w w . j a va 2s. c om if (entry.isDirectory()) { FileUtil.mkdir(outputFile, context); return; } if (!outputFile.getParentFile().exists()) { FileUtil.mkdir(outputFile.getParentFile(), context); } BufferedOutputStream outputStream = new BufferedOutputStream(FileUtil.getOutputStream(outputFile, context)); try { int len; byte buf[] = new byte[GenericCopyUtil.DEFAULT_BUFFER_SIZE]; while ((len = inputStream.read(buf)) != -1) { if (!listener.isCancelled()) { outputStream.write(buf, 0, len); ServiceWatcherUtil.position += len; } else break; } } finally { outputStream.close(); } }
From source file:com.amaze.filemanager.filesystem.compressed.extractcontents.helpers.GzipExtractor.java
private void extractEntry(@NonNull final Context context, TarArchiveInputStream inputStream, TarArchiveEntry entry, String outputDir) throws IOException { File outputFile = new File(outputDir, fixEntryName(entry.getName())); if (!outputFile.getCanonicalPath().startsWith(outputDir)) { throw new IOException("Incorrect ZipEntry path!"); }//from w w w . java2 s . c o m if (entry.isDirectory()) { FileUtil.mkdir(outputFile, context); return; } if (!outputFile.getParentFile().exists()) { FileUtil.mkdir(outputFile.getParentFile(), context); } BufferedOutputStream outputStream = new BufferedOutputStream(FileUtil.getOutputStream(outputFile, context)); try { int len; byte buf[] = new byte[GenericCopyUtil.DEFAULT_BUFFER_SIZE]; while ((len = inputStream.read(buf)) != -1) { if (!listener.isCancelled()) { outputStream.write(buf, 0, len); ServiceWatcherUtil.position += len; } else break; } } finally { outputStream.close(); } }
From source file:com.google.cloud.tools.managedcloudsdk.install.TarGzExtractorProvider.java
@Override public void extract(Path archive, Path destination, ProgressListener progressListener) throws IOException { progressListener.start("Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN); String canonicalDestination = destination.toFile().getCanonicalPath(); GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive)); try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { Path entryTarget = destination.resolve(entry.getName()); String canonicalTarget = entryTarget.toFile().getCanonicalPath(); if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) { throw new IOException("Blocked unzipping files outside destination: " + entry.getName()); }/*from w w w .j av a 2 s .c o m*/ progressListener.update(1); logger.fine(entryTarget.toString()); if (entry.isDirectory()) { if (!Files.exists(entryTarget)) { Files.createDirectories(entryTarget); } } else if (entry.isFile()) { if (!Files.exists(entryTarget.getParent())) { Files.createDirectories(entryTarget.getParent()); } try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) { IOUtils.copy(in, out); PosixFileAttributeView attributeView = Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class); if (attributeView != null) { attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode())); } } } else { // we don't know what kind of entry this is (we only process directories and files). logger.warning("Skipping entry (unknown type): " + entry.getName()); } } progressListener.done(); } }
From source file:com.renard.ocr.help.OCRLanguageInstallService.java
@Override protected void onHandleIntent(Intent intent) { if (!intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) { return;//from w w w. j a va2 s.c o m } final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); if (downloadId != 0) { DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); ParcelFileDescriptor file; try { file = dm.openDownloadedFile(downloadId); FileInputStream fin = new FileInputStream(file.getFileDescriptor()); BufferedInputStream in = new BufferedInputStream(fin); FileOutputStream out = openFileOutput("tess-lang.tmp", Context.MODE_PRIVATE); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); final byte[] buffer = new byte[2048 * 2]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); gzIn.close(); FileInputStream fileIn = openFileInput("tess-lang.tmp"); TarArchiveInputStream tarIn = new TarArchiveInputStream(fileIn); TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null && !(entry.getName().endsWith(".traineddata") && !entry.getName().endsWith("_old.traineddata"))) { entry = tarIn.getNextTarEntry(); } if (entry != null) { File tessDir = Util.getTrainingDataDir(this); final String langName = entry.getName().substring("tesseract-ocr/tessdata/".length()); File trainedData = new File(tessDir, langName); FileOutputStream fout = new FileOutputStream(trainedData); int len; while ((len = tarIn.read(buffer)) != -1) { fout.write(buffer, 0, len); } fout.close(); String lang = langName.substring(0, langName.length() - ".traineddata".length()); notifyReceivers(lang); } tarIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { String tessDir = Util.getTessDir(this); File targetFile = new File(tessDir, OCRLanguageActivity.DOWNLOADED_TRAINING_DATA); if (targetFile.exists()) { targetFile.delete(); } } } }
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 . ja v a 2s. c om*/ 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:com.wenzani.maven.mongodb.TarUtils.java
private void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); File subDir = new File(outputDir, entry.getName()); for (TarArchiveEntry e : entry.getDirectoryEntries()) { unpackEntries(tis, e, subDir); }//from www . j a v a 2 s . co m return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { byte[] content = new byte[(int) entry.getSize()]; tis.read(content); if (content.length > 0) { IOUtils.copy(new ByteArrayInputStream(content), outputStream); } } finally { IOUtils.closeQuietly(outputStream); } }
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;// www . j ava 2 s . c o m 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:ezbake.deployer.publishers.local.BaseLocalPublisher.java
protected String getConfigPath(DeploymentArtifact artifact) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact()))); File tmpDir = new File("user-conf", ArtifactHelpers.getServiceId(artifact)); tmpDir.mkdirs();// w w w. j ava 2s .c o m String configPath = tmpDir.getAbsolutePath(); try { TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().startsWith("config/")) { File tmpFile = new File(configPath, Files.getNameWithoutExtension(entry.getName()) + ".properties"); FileOutputStream fos = new FileOutputStream(tmpFile); try { IOUtils.copy(tarIn, fos); } finally { IOUtils.closeQuietly(fos); } } entry = tarIn.getNextTarEntry(); } } finally { IOUtils.closeQuietly(tarIn); } return configPath; }
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/*from ww w . j av a 2 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:ezbake.deployer.publishers.ezcentos.EzCentosThriftRunnerPublisher.java
/** * Copies files from the artifact .tar file * that end in the suffix to the specified destination. * * @param artifact/*from www . j a va 2 s . c o m*/ * @param destination * @param suffix * @return Paths that the files were copied to * @throws IOException */ private List<String> copyFiles(DeploymentArtifact artifact, File destination, String suffix) throws IOException { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact()))); List<String> filePaths = new ArrayList<>(); try { TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().endsWith(suffix)) { String newFilePath = destination + File.separator + entry.getName(); FileOutputStream fos = new FileOutputStream(newFilePath); try { IOUtils.copy(tarIn, fos); } finally { IOUtils.closeQuietly(fos); } filePaths.add(newFilePath); } entry = tarIn.getNextTarEntry(); } } finally { IOUtils.closeQuietly(tarIn); } return filePaths; }