Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry.

Prototype

public TarArchiveEntry(byte[] headerBuf) 

Source Link

Document

Construct an entry from an archive's header bytes.

Usage

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

/**
 * Encrypts and encodes the string and writes it to the tar output stream with the specified file name
 *///from   w ww .j a v a 2 s .  co  m
protected void encryptAndWrite(TarArchiveOutputStream tarArchiveOutputStream, String contents, String fileName)
        throws InvalidCipherTextException, IOException {
    final byte[] contentBytes = contents.getBytes(CHARSET);

    //Encrypt contents
    final AsymmetricBlockCipher encryptCipher = this.getEncryptCipher();
    final byte[] encryptedContentBytes = encryptCipher.processBlock(contentBytes, 0, contentBytes.length);
    final byte[] encryptedContentBase64Bytes = Base64.encodeBase64(encryptedContentBytes);

    //Write encrypted contents to tar output stream
    final TarArchiveEntry contentEntry = new TarArchiveEntry(fileName);
    contentEntry.setSize(encryptedContentBase64Bytes.length);
    tarArchiveOutputStream.putArchiveEntry(contentEntry);
    tarArchiveOutputStream.write(encryptedContentBase64Bytes);
    tarArchiveOutputStream.closeArchiveEntry();
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void fillDataTar(Config config, ArFileOutputStream output) throws MojoExecutionException {
    TarArchiveOutputStream tar = null;/*from   w  w  w  . jav  a  2s.c  o m*/
    try {
        tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output)));
        tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        if (Boolean.TRUE.equals(javaServiceWrapper)) {
            byte[] daemonData = processTemplate(freemarkerConfig, config, "daemon.ftl");
            TarArchiveEntry initScript = new TarArchiveEntry("etc/init.d/" + project.getArtifactId());
            initScript.setSize(daemonData.length);
            initScript.setMode(040755);
            tar.putArchiveEntry(initScript);
            tar.write(daemonData);
            tar.closeArchiveEntry();
        }
        String packageBaseDir = "home/" + unixUserId + "/" + project.getArtifactId() + "/";
        if (fileSets != null && !fileSets.isEmpty()) {
            writeDirectory(tar, packageBaseDir);

            Collections.sort(fileSets, MappingPathComparator.INSTANCE);
            for (Fileset curPath : fileSets) {
                curPath.setTarget(packageBaseDir + curPath.getTarget());
                addRecursively(config, tar, curPath);
            }
        }

    } catch (Exception e) {
        throw new MojoExecutionException("unable to create data tar", e);
    } finally {
        IOUtils.closeQuietly(tar);
    }
}

From source file:freenet.client.async.ContainerInserter.java

/**
** OutputStream os will be close()d if this method returns successfully.
*/// www .  j  a va2 s .  co  m
private String createTarBucket(OutputStream os) throws IOException {
    if (logMINOR)
        Logger.minor(this, "Create a TAR Bucket");

    TarArchiveOutputStream tarOS = new TarArchiveOutputStream(os);
    try {
        tarOS.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        TarArchiveEntry ze;

        for (ContainerElement ph : containerItems) {
            if (logMINOR)
                Logger.minor(this, "Putting into tar: " + ph + " data length " + ph.data.size() + " name "
                        + ph.targetInArchive);
            ze = new TarArchiveEntry(ph.targetInArchive);
            ze.setModTime(0);
            long size = ph.data.size();
            ze.setSize(size);
            tarOS.putArchiveEntry(ze);
            BucketTools.copyTo(ph.data, tarOS, size);
            tarOS.closeArchiveEntry();
        }
    } finally {
        tarOS.close();
    }

    return ARCHIVE_TYPE.TAR.mimeTypes[0];
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtilsTest.java

private void addFilesToTar(TarArchiveOutputStream tos, File... filesToAdd) throws IOException {
    for (File file : filesToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(file);
        tos.putArchiveEntry(tarEntry);/*from  w w  w . j  av a 2  s. co  m*/
        if (file.isFile()) {
            com.google.common.io.Files.copy(file, tos);
        }
        tos.closeArchiveEntry();
    }
}

From source file:deployer.TestUtils.java

public static List<ArtifactDataEntry> sampleOpenShiftConfiguration() {
    return Lists.newArrayList(
            new ArtifactDataEntry(new TarArchiveEntry(Paths.get(CONFIG_DIRECTORY, "standalone.xml").toString()),
                    SAMPLE_STANDALONE_DATA.getBytes()));
}

From source file:deployer.TestUtils.java

public static List<ArtifactDataEntry> sampleSSL() {
    byte[] sampleCert = SAMPLE_SSL_DATA.getBytes();
    return Lists.newArrayList(new ArtifactDataEntry(
            new TarArchiveEntry(CONFIG_DIRECTORY + "/ssl/" + SERVICE_NAME + ".jks"), sampleCert));
}

From source file:com.st.maven.debian.DebianPackageMojo.java

private void addRecursively(Config config, TarArchiveOutputStream tar, Fileset fileset)
        throws MojoExecutionException {
    File sourceFile = new File(fileset.getSource());
    String targetFilename = fileset.getTarget();
    // skip well-known ignore directories
    if (ignore.contains(sourceFile.getName()) || sourceFile.getName().endsWith(".rrd")
            || sourceFile.getName().endsWith(".log")) {
        return;//from   w  w  w.  j av a  2 s.c  om
    }
    FileInputStream fis = null;
    try {
        if (!sourceFile.isDirectory()) {
            TarArchiveEntry curEntry = new TarArchiveEntry(targetFilename);
            if (fileset.isFilter()) {
                byte[] bytes = processTemplate(freemarkerConfig, config, fileset.getSource());
                curEntry.setSize(bytes.length);
                tar.putArchiveEntry(curEntry);
                tar.write(bytes);
            } else {
                curEntry.setSize(sourceFile.length());
                tar.putArchiveEntry(curEntry);
                fis = new FileInputStream(sourceFile);
                IOUtils.copy(fis, tar);
            }
            tar.closeArchiveEntry();
        } else if (sourceFile.isDirectory()) {
            targetFilename += "/";
            if (!dirsAdded.contains(targetFilename)) {
                dirsAdded.add(targetFilename);
                writeDirectory(tar, targetFilename);
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("unable to write", e);
    } finally {
        IOUtils.closeQuietly(fis);
    }

    if (sourceFile.isDirectory()) {
        File[] subFiles = sourceFile.listFiles();
        for (File curSubFile : subFiles) {
            Fileset curSubFileset = new Fileset(fileset.getSource() + "/" + curSubFile.getName(),
                    fileset.getTarget() + "/" + curSubFile.getName(), fileset.isFilter());
            addRecursively(config, tar, curSubFileset);
        }
    }
}

From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.MavenDeployer.java

private File populateTgzArchive(File archive, List<FileRef> fileset) throws IOException {
    archive.getParentFile().mkdirs();/*from w  w  w  .j  a  v a 2  s.  co m*/
    CompressorOutputStream zip = new GzipCompressorOutputStream(new FileOutputStream(archive));
    TarArchiveOutputStream tar = new TarArchiveOutputStream(zip);
    for (FileRef fileRef : fileset) {
        TarArchiveEntry entry = new TarArchiveEntry(new File(fileRef.getRelativeLocation()));
        byte[] bytes = fileRef.getContent().getBytes();
        entry.setSize(bytes.length);
        tar.putArchiveEntry(entry);
        tar.write(bytes);
        tar.closeArchiveEntry();
    }
    tar.close();
    return archive;
}

From source file:io.fabric8.docker.client.impl.BuildImage.java

@Override
public OutputHandle fromFolder(String path) {
    try {/*from   www .j av a2s .  c  o m*/
        final Path root = Paths.get(path);
        final Path dockerIgnore = root.resolve(DOCKER_IGNORE);
        final List<String> ignorePatterns = new ArrayList<>();
        if (dockerIgnore.toFile().exists()) {
            for (String p : Files.readAllLines(dockerIgnore, UTF_8)) {
                ignorePatterns.add(path.endsWith(File.separator) ? path + p : path + File.separator + p);
            }
        }

        final DockerIgnorePathMatcher dockerIgnorePathMatcher = new DockerIgnorePathMatcher(ignorePatterns);

        File tempFile = Files.createTempFile(Paths.get(DEFAULT_TEMP_DIR), DOCKER_PREFIX, BZIP2_SUFFIX).toFile();

        try (FileOutputStream fout = new FileOutputStream(tempFile);
                BufferedOutputStream bout = new BufferedOutputStream(fout);
                BZip2CompressorOutputStream bzout = new BZip2CompressorOutputStream(bout);
                final TarArchiveOutputStream tout = new TarArchiveOutputStream(bzout)) {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    if (dockerIgnorePathMatcher.matches(dir)) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (dockerIgnorePathMatcher.matches(file)) {
                        return FileVisitResult.SKIP_SUBTREE;
                    }

                    final Path relativePath = root.relativize(file);
                    final TarArchiveEntry entry = new TarArchiveEntry(file.toFile());
                    entry.setName(relativePath.toString());
                    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);
                    entry.setSize(attrs.size());
                    tout.putArchiveEntry(entry);
                    Files.copy(file, tout);
                    tout.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }
            });
            fout.flush();
        }
        return fromTar(tempFile.getAbsolutePath());

    } catch (IOException e) {
        throw DockerClientException.launderThrowable(e);
    }
}

From source file:com.facebook.buck.jvm.java.DefaultJavaLibraryIntegrationTest.java

/**
 * writeTarZst writes a .tar.zst file to 'file'.
 *
 * <p>For each key:value in archiveContents, a file named 'key' with contents 'value' will be
 * created in the archive. File names ending with "/" are considered directories.
 */// w  ww. jav  a  2  s. c o m
private void writeTarZst(Path file, Map<String, byte[]> archiveContents) throws IOException {
    try (OutputStream o = new BufferedOutputStream(Files.newOutputStream(file));
            OutputStream z = new ZstdCompressorOutputStream(o);
            TarArchiveOutputStream archive = new TarArchiveOutputStream(z)) {
        archive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
        for (Entry<String, byte[]> mapEntry : archiveContents.entrySet()) {
            String fileName = mapEntry.getKey();
            byte[] fileContents = mapEntry.getValue();
            boolean isRegularFile = !fileName.endsWith("/");

            TarArchiveEntry e = new TarArchiveEntry(fileName);
            if (isRegularFile) {
                e.setSize(fileContents.length);
                archive.putArchiveEntry(e);
                archive.write(fileContents);
            } else {
                archive.putArchiveEntry(e);
            }
            archive.closeArchiveEntry();
        }
        archive.finish();
    }
}