org.apache.tools.tar.TarEntry Java Examples

The following examples show how to use org.apache.tools.tar.TarEntry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplatesNotExecutedWhenNotFiltered() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello {{{deb.name}}}", false, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);

    final File dataDir = temporaryFolder.newFolder();
    ArchiveUtils.extractTarGzip(new File(packageDir, "data.tar.gz"), dataDir);

    final File testFile = new File(dataDir, "/tmp/test.txt");
    assertTrue(testFile.exists());
    assertEquals("hello {{{deb.name}}}", Files.asCharSource(testFile, StandardCharsets.UTF_8).read().trim());
}
 
Example #2
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplatesCorrectlyExecuted() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello {{{deb.name}}}", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);

    final File dataDir = temporaryFolder.newFolder();
    ArchiveUtils.extractTarGzip(new File(packageDir, "data.tar.gz"), dataDir);

    final File testFile = new File(dataDir, "/tmp/test.txt");
    assertTrue(testFile.exists());
    assertEquals("hello test", Files.asCharSource(testFile, StandardCharsets.UTF_8).read().trim());
}
 
Example #3
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpectedControlFiles() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello world", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);

    final File controlDir = temporaryFolder.newFolder();
    ArchiveUtils.extractTarGzip(new File(packageDir, "control.tar.gz"), controlDir);

    for (final String file : ImmutableSet.of(
        "control", "conffiles", "preinst", "postinst", "prerm", "postrm")) {
        assertTrue(new File(controlDir, file).exists());
    }
}
 
Example #4
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPackageSignature() throws IOException, PackagingException, PGPException, SignatureException, org.bouncycastle.openpgp.PGPException, NoSuchProviderException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello world", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);

    final File pgpSignatureFile = new File(packageDir, "_gpgorigin");
    assertTrue(pgpSignatureFile.exists());

    try (final InputStream keyringIn = PGPUtil.getDecoderStream(PackageBuilderTest.class.getResourceAsStream("public.asc"))) {
        try (final InputStream signatureIn = PGPUtil.getDecoderStream(new FileInputStream(pgpSignatureFile))) {
            final PGPPublicKey publicKey = ((PGPPublicKeyRing) new BcPGPPublicKeyRingCollection(keyringIn).getKeyRings().next()).getPublicKey();
            final PGPSignature signature = ((PGPSignatureList) new BcPGPObjectFactory(signatureIn).nextObject()).get(0);
            signature.init(new BcPGPContentVerifierBuilderProvider(), publicKey);

            signature.update(Files.asByteSource(new File(packageDir, "debian-binary")).read());
            signature.update(Files.asByteSource(new File(packageDir, "control.tar.gz")).read());
            signature.update(Files.asByteSource(new File(packageDir, "data.tar.gz")).read());

            assertTrue(signature.verify());
        }
    }
}
 
Example #5
Source File: TarCopyAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitFile(FileCopyDetails fileDetails) {
    try {
        TarEntry archiveEntry = new TarEntry(fileDetails.getRelativePath().getPathString());
        archiveEntry.setModTime(fileDetails.getLastModified());
        archiveEntry.setSize(fileDetails.getSize());
        archiveEntry.setMode(UnixStat.FILE_FLAG | fileDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        fileDetails.copyTo(tarOutStr);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", fileDetails, tarFile), e);
    }
}
 
Example #6
Source File: TarCopyAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDir(FileCopyDetails dirDetails) {
    try {
        // Trailing slash on name indicates entry is a directory
        TarEntry archiveEntry = new TarEntry(dirDetails.getRelativePath().getPathString() + '/');
        archiveEntry.setModTime(dirDetails.getLastModified());
        archiveEntry.setMode(UnixStat.DIR_FLAG | dirDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", dirDetails, tarFile), e);
    }
}
 
Example #7
Source File: DropwizardMojo.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("OctalInteger")
protected Collection<Resource> buildResourceList() {
    return ImmutableList.<Resource>builder()
            .add(new FileResource(configTemplate, true, path.getConfigFile(), unix.getUser(), unix.getUser(), UNIX_MODE_USER_ONLY))
            .add(new EmbeddedResource("/files/jvm.conf", true, path.getJvmConfigFile(), "root", "root", TarEntry.DEFAULT_FILE_MODE))
            .add(new EmbeddedResource("/files/upstart.conf", true, path.getUpstartFile(), "root", "root", TarEntry.DEFAULT_FILE_MODE))
            .add(new EmbeddedResource( "/files/systemv.sh", true, path.getSystemVFile(), "root", "root", TarEntry.DEFAULT_FILE_MODE | 0100111))
            .add(new EmbeddedResource( "/files/systemd.service", true, path.getSystemDFile(), "root", "root", TarEntry.DEFAULT_FILE_MODE))
            .add(new EmbeddedResource("/files/start.sh", true, path.getStartScript(), "root", "root", TarEntry.DEFAULT_FILE_MODE | 0100111))
            .add(new FileResource(artifactFile, false, path.getJarFile(), unix.getUser(), unix.getUser(), TarEntry.DEFAULT_FILE_MODE))
            .addAll(Collections2.transform(files, new ResourceProducer(unix.getUser())))
            .build();
}
 
Example #8
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #9
Source File: TarCopyAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDir(FileCopyDetails dirDetails) {
    try {
        // Trailing slash on name indicates entry is a directory
        TarEntry archiveEntry = new TarEntry(dirDetails.getRelativePath().getPathString() + '/');
        archiveEntry.setModTime(dirDetails.getLastModified());
        archiveEntry.setMode(UnixStat.DIR_FLAG | dirDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", dirDetails, tarFile), e);
    }
}
 
Example #10
Source File: TarCopyAction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitFile(FileCopyDetails fileDetails) {
    try {
        TarEntry archiveEntry = new TarEntry(fileDetails.getRelativePath().getPathString());
        archiveEntry.setModTime(fileDetails.getLastModified());
        archiveEntry.setSize(fileDetails.getSize());
        archiveEntry.setMode(UnixStat.FILE_FLAG | fileDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        fileDetails.copyTo(tarOutStr);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", fileDetails, tarFile), e);
    }
}
 
Example #11
Source File: ResourceProducerTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaults() {
    final Resource resource = producer.apply(new ResourceConfiguration().setSource(source).setTarget("/tmp/test"));
    assertNotNull(resource);
    assertEquals("tester", resource.getUser());
    assertEquals(TarEntry.DEFAULT_FILE_MODE, resource.getMode());
    assertTrue(resource.isFilter());
}
 
Example #12
Source File: TarFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #13
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #14
Source File: TarArchiveLocation.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Create an InputStream for a TarEntry.
 * <p>
 * The root TarArchiveLocation catalogs every TarEntry, so this method
 * provides (very nearly) random access to entries.
 */
@Override
public InputStream createInputStream(TarEntry entry) throws IOException {
	/*
	 * The slow way here would be to create a new TarInputStream and iterate
	 * over all TarEntries until we identified the correct entry and return
	 * that input.
	 * 
	 * ... but since we cataloged this up front, we can skip all that.
	 */
	EntryLocation loc = archiveEntryToLocation.get(entry.getName());
	if (loc == null)
		throw new IllegalArgumentException("unrecognized tar entry \""
				+ entry.getName() + "\"");

	InputStream in = null;
	try {
		in = root.createInputStream();
		MeasuredInputStream measuredIn = new MeasuredInputStream(in);
		IOUtils.skipFully(measuredIn, loc.startPtr);
		return new GuardedInputStream(measuredIn, loc.length, true);
	} catch (IOException e) {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		throw e;
	}
}
 
Example #15
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatesPackage() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello world", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));
    assertTrue(debFile.exists());
}
 
Example #16
Source File: TarArchiveLocation.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public String getArchivePath() {
	TarEntry entry = getArchiveEntry();
	if (entry == null)
		return null;

	return entry.getName();
}
 
Example #17
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidArchive() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello world", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);
}
 
Example #18
Source File: TestFileUtil.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test (timeout = 30000)
public void testUnTar() throws IOException {
  setupDirs();
  
  // make a simple tar:
  final File simpleTar = new File(del, FILE);
  OutputStream os = new FileOutputStream(simpleTar); 
  TarOutputStream tos = new TarOutputStream(os);
  try {
    TarEntry te = new TarEntry("/bar/foo");
    byte[] data = "some-content".getBytes("UTF-8");
    te.setSize(data.length);
    tos.putNextEntry(te);
    tos.write(data);
    tos.closeEntry();
    tos.flush();
    tos.finish();
  } finally {
    tos.close();
  }

  // successfully untar it into an existing dir:
  FileUtil.unTar(simpleTar, tmp);
  // check result:
  assertTrue(new File(tmp, "/bar/foo").exists());
  assertEquals(12, new File(tmp, "/bar/foo").length());
  
  final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
  regularFile.createNewFile();
  assertTrue(regularFile.exists());
  try {
    FileUtil.unTar(simpleTar, regularFile);
    assertTrue("An IOException expected.", false);
  } catch (IOException ioe) {
    // okay
  }
}
 
Example #19
Source File: TestFileUtil.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test (timeout = 30000)
public void testUnTar() throws IOException {
  setupDirs();
  
  // make a simple tar:
  final File simpleTar = new File(del, FILE);
  OutputStream os = new FileOutputStream(simpleTar); 
  TarOutputStream tos = new TarOutputStream(os);
  try {
    TarEntry te = new TarEntry("/bar/foo");
    byte[] data = "some-content".getBytes("UTF-8");
    te.setSize(data.length);
    tos.putNextEntry(te);
    tos.write(data);
    tos.closeEntry();
    tos.flush();
    tos.finish();
  } finally {
    tos.close();
  }

  // successfully untar it into an existing dir:
  FileUtil.unTar(simpleTar, tmp);
  // check result:
  assertTrue(new File(tmp, "/bar/foo").exists());
  assertEquals(12, new File(tmp, "/bar/foo").length());
  
  final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
  regularFile.createNewFile();
  assertTrue(regularFile.exists());
  try {
    FileUtil.unTar(simpleTar, regularFile);
    assertTrue("An IOException expected.", false);
  } catch (IOException ioe) {
    // okay
  }
}
 
Example #20
Source File: TarCopyAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDir(FileCopyDetails dirDetails) {
    try {
        // Trailing slash on name indicates entry is a directory
        TarEntry archiveEntry = new TarEntry(dirDetails.getRelativePath().getPathString() + '/');
        archiveEntry.setModTime(dirDetails.getLastModified());
        archiveEntry.setMode(UnixStat.DIR_FLAG | dirDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", dirDetails, tarFile), e);
    }
}
 
Example #21
Source File: TarCopyAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitFile(FileCopyDetails fileDetails) {
    try {
        TarEntry archiveEntry = new TarEntry(fileDetails.getRelativePath().getPathString());
        archiveEntry.setModTime(fileDetails.getLastModified());
        archiveEntry.setSize(fileDetails.getSize());
        archiveEntry.setMode(UnixStat.FILE_FLAG | fileDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        fileDetails.copyTo(tarOutStr);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", fileDetails, tarFile), e);
    }
}
 
Example #22
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpectedDataFiles() throws IOException, PackagingException {
    final File debFile = createPackage(ImmutableList.<Resource>of(
            new StringResource("hello world", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));

    final File packageDir = temporaryFolder.newFolder();
    ArchiveUtils.extractAr(debFile, packageDir);

    final File dataDir = temporaryFolder.newFolder();
    ArchiveUtils.extractTarGzip(new File(packageDir, "data.tar.gz"), dataDir);

    final File testFile = new File(dataDir, "/tmp/test.txt");
    assertTrue(testFile.exists());
}
 
Example #23
Source File: TarFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #24
Source File: TarCopyAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDir(FileCopyDetails dirDetails) {
    try {
        // Trailing slash on name indicates entry is a directory
        TarEntry archiveEntry = new TarEntry(dirDetails.getRelativePath().getPathString() + '/');
        archiveEntry.setModTime(dirDetails.getLastModified());
        archiveEntry.setMode(UnixStat.DIR_FLAG | dirDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", dirDetails, tarFile), e);
    }
}
 
Example #25
Source File: TarCopyAction.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitFile(FileCopyDetails fileDetails) {
    try {
        TarEntry archiveEntry = new TarEntry(fileDetails.getRelativePath().getPathString());
        archiveEntry.setModTime(fileDetails.getLastModified());
        archiveEntry.setSize(fileDetails.getSize());
        archiveEntry.setMode(UnixStat.FILE_FLAG | fileDetails.getMode());
        tarOutStr.putNextEntry(archiveEntry);
        fileDetails.copyTo(tarOutStr);
        tarOutStr.closeEntry();
    } catch (Exception e) {
        throw new GradleException(String.format("Could not add %s to TAR '%s'.", fileDetails, tarFile), e);
    }
}
 
Example #26
Source File: PackageBuilderTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test(expected = MissingParameterException.class)
public void testFailsOnMissingTemplateVariables() throws IOException, PackagingException {
    createPackage(ImmutableList.<Resource>of(
            new StringResource("hello {{{missing.variable}}}", true, "/tmp/test.txt", USER, USER, TarEntry.DEFAULT_FILE_MODE)
    ));
}
 
Example #27
Source File: ResourceTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testFileResource() throws IOException {
    final Resource resource = new FileResource(new File(Resource.class.getResource("file.txt").getFile()), true, "/tmp/test.txt", "dropwizard", "dropwizard", TarEntry.DEFAULT_FILE_MODE);
    final String result = new String(resource.getSource().read());
    assertEquals("file", result);
}
 
Example #28
Source File: ResourceTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmbeddedResource() throws IOException {
    final Resource resource = new EmbeddedResource("embedded.txt", true, "/tmp/test.txt", "dropwizard", "dropwizard", TarEntry.DEFAULT_FILE_MODE);
    final String result = new String(resource.getSource().read());
    assertEquals("embedded", result);
}
 
Example #29
Source File: ResourceTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testStringResource() throws IOException {
    final Resource resource = new StringResource("string", true, "/tmp/test.txt", "dropwizard", "dropwizard", TarEntry.DEFAULT_FILE_MODE);
    final String result = new String(resource.getSource().read());
    assertEquals("string", result);
}
 
Example #30
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public DetailsImpl(TarEntry entry, NoCloseTarInputStream tar, AtomicBoolean stopFlag, Chmod chmod) {
    super(chmod);
    this.entry = entry;
    this.tar = tar;
    this.stopFlag = stopFlag;
}