List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void testCreateArchiveWithDetectedModes() throws Exception { String[] executablePaths = { "path/to/executable", "path/to/executable.bat" }; String[] confPaths = { "path/to/etc/file", "path/to/etc/file2" }; String[] logPaths = { "path/to/logs/log.txt" }; int exeMode = 0777; int confMode = 0600; int logMode = 0640; if (Os.isFamily(Os.FAMILY_WINDOWS)) { StackTraceElement e = new Throwable().getStackTrace()[0]; System.out/*w w w . j a va 2 s. co m*/ .println("Cannot execute test: " + e.getMethodName() + " on " + System.getProperty("os.name")); return; } File tmpDir = null; try { tmpDir = File.createTempFile("tbz2-with-chmod.", ".dir"); tmpDir.delete(); tmpDir.mkdirs(); for (String executablePath : executablePaths) { writeFile(tmpDir, executablePath, exeMode); } for (String confPath : confPaths) { writeFile(tmpDir, confPath, confMode); } for (String logPath : logPaths) { writeFile(tmpDir, logPath, logMode); } { Map attributesByPath = PlexusIoResourceAttributeUtils.getFileAttributesByPath(tmpDir); for (String path : executablePaths) { PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get(path); if (attrs == null) { attrs = (PlexusIoResourceAttributes) attributesByPath .get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, attrs.getOctalMode()); } for (String path : confPaths) { PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get(path); if (attrs == null) { attrs = (PlexusIoResourceAttributes) attributesByPath .get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, attrs.getOctalMode()); } for (String path : logPaths) { PlexusIoResourceAttributes attrs = (PlexusIoResourceAttributes) attributesByPath.get(path); if (attrs == null) { attrs = (PlexusIoResourceAttributes) attributesByPath .get(new File(tmpDir, path).getAbsolutePath()); } assertNotNull(attrs); assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, attrs.getOctalMode()); } } File tarFile = getTestFile("target/output/tar-with-modes.tar"); TarArchiver archiver = getPosixTarArchiver(); archiver.setDestFile(tarFile); archiver.addDirectory(tmpDir); archiver.createArchive(); assertTrue(tarFile.exists()); File tarFile2 = getTestFile("target/output/tar-with-modes-L2.tar"); archiver = getPosixTarArchiver(); archiver.setDestFile(tarFile2); archiver.addArchivedFileSet(tarFile); archiver.createArchive(); TarFile tf = new TarFile(tarFile2); Map<String, TarArchiveEntry> entriesByPath = new LinkedHashMap<String, TarArchiveEntry>(); for (Enumeration e = tf.getEntries(); e.hasMoreElements();) { TarArchiveEntry te = (TarArchiveEntry) e.nextElement(); entriesByPath.put(te.getName(), te); } for (String path : executablePaths) { TarArchiveEntry te = entriesByPath.get(path); int mode = te.getMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + exeMode, exeMode, mode); } for (String path : confPaths) { TarArchiveEntry te = entriesByPath.get(path); int mode = te.getMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + confMode, confMode, mode); } for (String path : logPaths) { TarArchiveEntry te = entriesByPath.get(path); int mode = te.getMode() & UnixStat.PERM_MASK; assertEquals("Wrong mode for: " + path + "; expected: " + logMode, logMode, mode); } } finally { if (tmpDir != null && tmpDir.exists()) { try { FileUtils.forceDelete(tmpDir); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void createArchive(final int directoryMode, final int fileModes[]) throws Exception { int defaultFileMode = fileModes[0]; int oneFileMode = fileModes[1]; int twoFileMode = fileModes[2]; TarArchiver archiver = getPosixTarArchiver(); archiver.setDirectoryMode(directoryMode); archiver.setFileMode(defaultFileMode); archiver.addDirectory(getTestFile("src/main")); archiver.setFileMode(oneFileMode);/*from www .j a va 2 s.com*/ archiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt"); archiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt", twoFileMode); archiver.setDestFile(getTestFile("target/output/archive.tar")); archiver.addSymlink("link_to_test_destinaton", "../test_destination/"); archiver.createArchive(); TarArchiveInputStream tis; tis = new TarArchiveInputStream(bufferedInputStream(new FileInputStream(archiver.getDestFile()))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { if (te.isDirectory()) { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", directoryMode, te.getMode() & UnixStat.PERM_MASK); } else if (te.isSymbolicLink()) { assertEquals("../test_destination/", te.getLinkName()); assertEquals("link_to_test_destinaton", te.getName()); assertEquals(0640, te.getMode() & UnixStat.PERM_MASK); } else { if (te.getName().equals("one.txt")) { assertEquals(oneFileMode, te.getMode() & UnixStat.PERM_MASK); } else if (te.getName().equals("two.txt")) { assertEquals(twoFileMode, te.getMode() & UnixStat.PERM_MASK); } else { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", defaultFileMode, te.getMode() & UnixStat.PERM_MASK); } } } IOUtil.close(tis); }
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void testCreateArchiveWithJiustASymlink() throws Exception { TarArchiver archiver = getPosixTarArchiver(); archiver.setDirectoryMode(0500);//from w w w. j a v a 2 s . c om archiver.setFileMode(0400); archiver.setFileMode(0640); archiver.setDestFile(getTestFile("target/output/symlinkarchive.tar")); archiver.addSymlink("link_to_test_destinaton", "../test_destination/"); archiver.createArchive(); TarArchiveInputStream tis; tis = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(archiver.getDestFile()))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { if (te.isDirectory()) { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0500, te.getMode() & UnixStat.PERM_MASK); } else if (te.isSymbolicLink()) { assertEquals("../test_destination/", te.getLinkName()); assertEquals("link_to_test_destinaton", te.getName()); assertEquals(0640, te.getMode() & UnixStat.PERM_MASK); } else { assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0400, te.getMode() & UnixStat.PERM_MASK); } } tis.close(); }
From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java
public void testGzipFIleHandleLeak() throws Exception { GZipTarHandler tarHandler = new GZipTarHandler(); final File tarFile = tarHandler.createTarFile(); final File tarFile2 = tarHandler.createTarfile2(tarFile); final TarFile cmp1 = tarHandler.newTarFile(tarFile); final TarFile cmp2 = new TarFile(tarFile2); ArchiveFileComparator.forEachTarArchiveEntry(cmp1, new ArchiveFileComparator.TarArchiveEntryConsumer() { public void accept(TarArchiveEntry ze1) throws IOException { final String name1 = ze1.getName(); Assert.assertNotNull(name1); }/*from ww w . j a v a 2s . com*/ }); cmp1.close(); cmp2.close(); }
From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java
/** * test round-tripping long (GNU) entries */// www. ja v a 2 s . c om public void testLongRoundTripping() throws IOException { TarArchiveEntry original = new TarArchiveEntry(LONG_NAME); assertEquals("over 100 chars", true, LONG_NAME.length() > 100); assertEquals("original name", LONG_NAME, original.getName()); ByteArrayOutputStream buff = new ByteArrayOutputStream(); TarArchiveOutputStream tos = new TarArchiveOutputStream(buff); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); tos.putArchiveEntry(original); tos.closeArchiveEntry(); tos.close(); TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray())); TarArchiveEntry tripped = tis.getNextTarEntry(); assertEquals("round-tripped name", LONG_NAME, tripped.getName()); assertNull("no more entries", tis.getNextEntry()); tis.close(); }
From source file:org.codehaus.plexus.archiver.tar.TarUnArchiver.java
protected void execute(File sourceFile, File destDirectory) throws ArchiverException { TarArchiveInputStream tis = null;// w w w. ja v a 2 s .com try { getLogger().info("Expanding: " + sourceFile + " into " + destDirectory); TarFile tarFile = new TarFile(sourceFile); tis = new TarArchiveInputStream( decompress(compression, sourceFile, new BufferedInputStream(new FileInputStream(sourceFile)))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { TarResource fileInfo = new TarResource(tarFile, te); if (isSelected(te.getName(), fileInfo)) { final String symlinkDestination = te.isSymbolicLink() ? te.getLinkName() : null; extractFile(sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(), te.getMode() != 0 ? te.getMode() : null, symlinkDestination); } } getLogger().debug("expand complete"); } catch (IOException ioe) { throw new ArchiverException("Error while expanding " + sourceFile.getAbsolutePath(), ioe); } finally { IOUtil.close(tis); } }
From source file:org.codelibs.fess.crawler.extractor.impl.TarExtractor.java
protected String getTextInternal(final InputStream in, final MimeTypeHelper mimeTypeHelper, final ExtractorFactory extractorFactory) { final UnsafeStringBuilder buf = new UnsafeStringBuilder(1000); ArchiveInputStream ais = null;// ww w . j a va 2 s . com try { ais = archiveStreamFactory.createArchiveInputStream("tar", in); TarArchiveEntry entry = null; long contentSize = 0; while ((entry = (TarArchiveEntry) ais.getNextEntry()) != null) { contentSize += entry.getSize(); if (maxContentSize != -1 && contentSize > maxContentSize) { throw new MaxLengthExceededException( "Extracted size is " + contentSize + " > " + maxContentSize); } final String filename = entry.getName(); final String mimeType = mimeTypeHelper.getContentType(null, filename); if (mimeType != null) { final Extractor extractor = extractorFactory.getExtractor(mimeType); if (extractor != null) { try { final Map<String, String> map = new HashMap<String, String>(); map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); buf.append(extractor.getText(new IgnoreCloseInputStream(ais), map).getContent()); buf.append('\n'); } catch (final Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception in an internal extractor.", e); } } } } } } catch (final MaxLengthExceededException e) { throw e; } catch (final Exception e) { if (buf.length() == 0) { throw new ExtractException("Could not extract a content.", e); } } finally { IOUtils.closeQuietly(ais); } return buf.toUnsafeString().trim(); }
From source file:org.codelibs.robot.extractor.impl.TarExtractor.java
protected String getTextInternal(final InputStream in, final MimeTypeHelper mimeTypeHelper, final ExtractorFactory extractorFactory) { final StringBuilder buf = new StringBuilder(1000); ArchiveInputStream ais = null;/* w w w.j ava 2s . c om*/ try { ais = archiveStreamFactory.createArchiveInputStream("tar", in); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ais.getNextEntry()) != null) { final String filename = entry.getName(); final String mimeType = mimeTypeHelper.getContentType(null, filename); if (mimeType != null) { final Extractor extractor = extractorFactory.getExtractor(mimeType); if (extractor != null) { try { final Map<String, String> map = new HashMap<String, String>(); map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename); buf.append(extractor.getText(new IgnoreCloseInputStream(ais), map).getContent()); buf.append('\n'); } catch (final Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception in an internal extractor.", e); } } } } } } catch (final Exception e) { if (buf.length() == 0) { throw new ExtractException("Could not extract a content.", e); } } finally { IOUtils.closeQuietly(ais); } return buf.toString(); }
From source file:org.dataconservancy.dcs.util.extraction.TarPackageExtractor.java
@Override public List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName) throws UnpackException { List<File> files = new ArrayList<>(); //use try-with-resources to make sure tar input stream is closed even in the event of an Exception try (TarArchiveInputStream tarInStream = TarArchiveInputStream.class .isAssignableFrom(packageInputStream.getClass()) ? (TarArchiveInputStream) packageInputStream : new TarArchiveInputStream(packageInputStream)) { TarArchiveEntry entry = tarInStream.getNextTarEntry(); //Get next tar entry returns null when there are no more entries while (entry != null) { //Directories are automatically handled by the base class so we can ignore them in this class. if (!entry.isDirectory()) { File entryFile = new File(packageDir, FilePathUtil.convertToPlatformSpecificSlash(entry.getName())); List<File> savedFiles = saveExtractedFile(entryFile, tarInStream); files.addAll(savedFiles); }//from ww w. jav a 2 s . co m entry = tarInStream.getNextTarEntry(); } tarInStream.close(); } catch (IOException e) { final String msg = "Error processing TarArchiveInputStream: " + e.getMessage(); log.error(msg, e); throw new UnpackException(msg, e); } return files; }
From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssemblerTest.java
/** * Test that the bag-info.txt file contains the parameter information passed in, including * parameters passed in after the initialization. * * Note that the package being generated in this case is empty, so the bag size and payload * oxum values will be 0.// w ww. j a v a2 s . com * @throws CompressorException * @throws ArchiveException * @throws IOException */ @Test public void testBagItInfoFile() throws CompressorException, ArchiveException, IOException { final String paramName = "TEST_PARAMETER"; final String paramValue = "test parameter"; underTest.addParameter(paramName, paramValue); Package pkg = underTest.assemblePackage(); CompressorInputStream cis = new CompressorStreamFactory() .createCompressorInputStream(CompressorStreamFactory.GZIP, pkg.serialize()); TarArchiveInputStream ais = (TarArchiveInputStream) (new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, cis)); String bagInfo = ""; TarArchiveEntry entry = ais.getNextTarEntry(); while (entry != null) { if (entry.getName().contains("bag-info.txt")) { byte[] content = new byte[(int) entry.getSize()]; ais.read(content, 0, (int) entry.getSize()); bagInfo = new String(content); break; } entry = ais.getNextTarEntry(); } // Test that expected initial parameters are present String expected = GeneralParameterNames.PACKAGE_NAME + ": " + packageName; assertTrue("Expected to find: " + expected, bagInfo.contains(expected)); // These two values should be 0 since there is nothing in the test package this time. expected = BagItParameterNames.BAG_SIZE + ": 0"; assertTrue("Expected to find: " + expected, bagInfo.contains(expected)); expected = BagItParameterNames.PAYLOAD_OXUM + ": 0"; assertTrue("Expected to find: " + expected, bagInfo.contains(expected)); // Test the post-init parameter expected = paramName + ": " + paramValue; assertTrue("Expected to find: " + expected, bagInfo.contains(expected)); }