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:net.firejack.platform.core.utils.ArchiveUtils.java

/**
  * @param basePath/*from www .j  a v  a 2s  . c  o m*/
  * @param tarPath
  * @param filePaths
  * @throws java.io.IOException
  */
public static void tar(String basePath, String tarPath, Map<String, String> filePaths) throws IOException {
    BufferedInputStream origin = null;
    TarArchiveOutputStream out = null;
    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(tarPath);
        out = new TarArchiveOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];
        for (Map.Entry<String, String> entryFile : filePaths.entrySet()) {
            String filename = entryFile.getKey();
            String filePath = entryFile.getValue();
            System.out.println("Adding: " + filename + " => " + filePath);
            FileInputStream fi = new FileInputStream(basePath + filePath);
            origin = new BufferedInputStream(fi, BUFFER);
            TarArchiveEntry entry = new TarArchiveEntry(filename);
            out.putArchiveEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (origin != null)
            origin.close();
        if (out != null)
            out.close();
        if (dest != null)
            dest.close();
    }
}

From source file:ezbake.deployer.publishers.EzSecurityRegistrationClient.java

public List<ArtifactDataEntry> get(EzSecurityToken token, String applicationId, String securityId)
        throws DeploymentException {
    List<ArtifactDataEntry> certs = new ArrayList<ArtifactDataEntry>();
    EzSecurityRegistration.Client client = null;
    try {/*from   ww  w . j  a v a2 s .c  o m*/
        client = pool.get().getClient(EzSecurityRegistrationConstants.SERVICE_NAME,
                EzSecurityRegistration.Client.class);

        AppCerts s = client.getAppCerts(token, securityId);
        for (AppCerts._Fields fields : AppCerts._Fields.values()) {
            Object o = s.getFieldValue(fields);
            if (o instanceof byte[]) {
                String fieldName = fields.getFieldName().replace("_", ".");
                TarArchiveEntry tae = new TarArchiveEntry(
                        Files.get(SSL_CONFIG_DIRECTORY, securityId, fieldName));
                certs.add(new ArtifactDataEntry(tae, (byte[]) o));
            }
        }

    } catch (RegistrationException e) {
        logger.error("Unable to download certificates from security service.", e);
        throw new DeploymentException(
                "Unable to download certificates from security service. " + e.getMessage());
    } catch (SecurityIDNotFoundException e) {
        logger.error("Unable to download certificates from security service.", e);
        throw new DeploymentException(
                "Unable to download certificates from security service. " + e.getMessage());
    } catch (TException e) {
        logger.error("Unable to download certificates from security service.", e);
        throw new DeploymentException(
                "Unable to download certificates from security service. " + e.getMessage());
    } finally {
        if (client != null) {
            pool.get().returnToPool(client);
        }
    }
    return certs;

}

From source file:ezbake.deployer.publishers.SecurityServiceClient.java

/**
 * The applicationId is part of the REST call to the Security Service.
 * Returns an empty list if no certificates were found or throws exception if
 * something went wrong with Security Service communication.
 */// w  w  w .jav a2s  .  c om
@Override
public List<ArtifactDataEntry> get(String applicationId, String securityId) throws DeploymentException {
    try {
        ArrayList<ArtifactDataEntry> certList = new ArrayList<ArtifactDataEntry>();
        String endpoint = config.getSecurityServiceBasePath() + "/registrations/" + securityId + "/download";
        URL url = new URL(endpoint);
        HttpsURLConnection urlConn = openUrlConnection(url);
        urlConn.connect();

        // Read in tar file of certificates into byte array
        BufferedInputStream in = new BufferedInputStream(urlConn.getInputStream());
        // Create CertDataEntry list from tarFile byte array
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
        TarArchiveEntry entry = tarIn.getNextTarEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(tarIn, bout);
                byte[] certData = bout.toByteArray();
                String certFileName = entry.getName().substring(entry.getName().lastIndexOf("/") + 1,
                        entry.getName().length());
                TarArchiveEntry tae = new TarArchiveEntry(
                        Files.get(SSL_CONFIG_DIRECTORY, securityId, certFileName));
                ArtifactDataEntry cde = new ArtifactDataEntry(tae, certData);
                certList.add(cde);
                bout.close();
            }

            entry = tarIn.getNextTarEntry();
        }
        tarIn.close();

        return certList;
    } catch (Exception e) {
        log.error("Unable to download certificates from security service.", e);
        throw new DeploymentException(
                "Unable to download certificates from security service. " + e.getMessage());
    }
}

From source file:com.moss.simpledeb.core.action.DigestAction.java

@Override
public void run(DebState state) throws Exception {

    final StringBuilder sb = new StringBuilder();
    final MessageDigest digest = java.security.MessageDigest.getInstance("MD5");

    for (ArchivePath path : state.contentPaths) {

        if (path.entry().isDirectory()) {
            continue;
        }/*from  w ww  .j  av  a 2 s.  com*/

        byte[] fileData;
        {
            InputStream in = path.read();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 10]; //10k buffer
            for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
                out.write(buffer, 0, numRead);
            }
            in.close();
            out.close();
            fileData = out.toByteArray();
        }

        digest.update(fileData);
        byte[] hash = digest.digest();
        digest.reset();

        sb.append(HexUtil.toHex(hash).toLowerCase());
        sb.append("  ");
        sb.append(path.entry().getName());
        sb.append("\n");
    }

    byte[] data = sb.toString().getBytes();

    TarArchiveEntry tarEntry = new TarArchiveEntry("md5sum");
    tarEntry.setGroupId(0);
    tarEntry.setGroupName("root");
    tarEntry.setIds(0, 0);
    tarEntry.setModTime(System.currentTimeMillis());
    tarEntry.setSize(data.length);
    tarEntry.setUserId(0);
    tarEntry.setUserName("root");

    state.addPath(DebComponent.CONTROL, new BytesArchivePath(tarEntry, data));
}

From source file:com.moss.simpledeb.core.action.CopyAction.java

private void copyFile(File file, DebState state) throws Exception {

    String entryName = "./" + targetDir + "/" + file.getName();

    if (!file.isDirectory()) {

        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tarEntry.setGroupId(0);//w ww.  j a  v  a 2  s.  c o  m
        tarEntry.setGroupName("root");
        tarEntry.setIds(0, 0);
        tarEntry.setModTime(System.currentTimeMillis());
        tarEntry.setSize(file.length());
        tarEntry.setUserId(0);
        tarEntry.setUserName("root");
        tarEntry.setMode(Integer.parseInt(fileMode, 8));

        ArchivePath path = new FileArchivePath(tarEntry, file);
        state.addPath(component, path);

        if (appendToClasspath) {
            state.classpath.add("/" + targetDir + "/" + file.getName());
        }
    } else {
        entryName = entryName + "/";

        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tarEntry.setGroupId(0);
        tarEntry.setGroupName("root");
        tarEntry.setIds(0, 0);
        tarEntry.setModTime(System.currentTimeMillis());
        tarEntry.setSize(0);
        tarEntry.setUserId(0);
        tarEntry.setUserName("root");
        tarEntry.setMode(Integer.parseInt(dirMode, 8));

        ArchivePath path = new DirArchivePath(tarEntry);
        state.addPath(component, path);
    }
}

From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java

/**
 * This method creates an ArchiveEntry w/o the need of a File required by
 * ArchiveOutputStream.createArchiveEntry(File inputFile, String entryName).
 * //w ww  . j ava 2  s  . com
 * @param aos archive output stream.
 * @param name name of entry.
 * @param size size in bytes of the entry.
 * @return an archive entry that matches the archive stream.
 */
public ArchiveEntry createArchiveEntry(ArchiveOutputStream aos, String name, long size) {
    if (aos instanceof TarArchiveOutputStream) {
        final TarArchiveEntry te = new TarArchiveEntry(name);
        te.setSize(size);
        return te;
    } else if (aos instanceof ZipArchiveOutputStream) {
        final ZipArchiveEntry ze = new ZipArchiveEntry(name);
        ze.setSize(size);
        return ze;
    }
    throw new UnsupportedOperationException("unsupported archive " + aos.getClass());
}

From source file:ezbake.deployer.utilities.Utilities.java

public static ArtifactDataEntry createConfCertDataEntry(String fileName, byte[] data) {
    return new ArtifactDataEntry(new TarArchiveEntry(String.format("%s/%s", CONFIG_DIRECTORY, fileName)), data);
}

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

public static void fileEntryToDestination(File source, ArchiveOutputStream archive, boolean atRoot)
        throws IOException {

    TarArchiveEntry entry;//from  w w  w  .j a  va  2 s  .co m
    if (atRoot) {
        entry = new TarArchiveEntry(source.getName());
    } else {
        entry = new TarArchiveEntry(source.getPath().replace(BuildFile, ""));
    }
    entry.setSize(source.length());
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    archive.putArchiveEntry(entry);

    BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));
    IOUtils.copy(input, archive);

    input.close();
    archive.closeArchiveEntry();
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleAppTarBall(ArtifactType type) throws IOException, ArchiveException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
    CompressorOutputStream gzs = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(gzs);
    {//from  ww w.  j  a  v a2  s  .  c  o m
        TarArchiveEntry nextEntry = new TarArchiveEntry(CONFIG_DIRECTORY + "/app.conf");
        byte[] sampleConf = SAMPLE_CONF_DATA.getBytes();
        nextEntry.setSize(sampleConf.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(sampleConf, aos);
        aos.closeArchiveEntry();
    }
    if (type != ArtifactType.WebApp) {
        TarArchiveEntry nextEntry = new TarArchiveEntry("bin/myApplication.jar");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    } else {
        TarArchiveEntry nextEntry = new TarArchiveEntry("deployments/ROOT.war");
        byte[] jarData = SAMPLE_JAR_DATA.getBytes();
        nextEntry.setSize(jarData.length);
        aos.putArchiveEntry(nextEntry);
        IOUtils.write(jarData, aos);
        aos.closeArchiveEntry();
    }
    aos.finish();
    gzs.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static void buildTarData(URL dirURL, String folderNameFilter, OutputStream outputStream)
        throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
    final ZipFile jar = jarConnection.getJarFile();
    final Enumeration<? extends ZipEntry> entries = jar.entries();

    try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputStream)) {
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (!name.startsWith(folderNameFilter)) {
                // entry in wrong subdir -- don't copy
                continue;
            }//from  w w w .  j  av a 2 s. co m
            TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName().replaceAll(folderNameFilter, ""));
            try (InputStream is = jar.getInputStream(entry)) {
                putTarEntry(tarArchiveOutputStream, tarEntry, is, entry.getSize());
            }
        }

        tarArchiveOutputStream.flush();
        tarArchiveOutputStream.close();
    }
}