Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveOutputStream

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveOutputStream

Introduction

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

Prototype

public ArchiveOutputStream createArchiveOutputStream(final String archiverName, final OutputStream out)
        throws ArchiveException 

Source Link

Document

Create an archive output stream from an archiver name and an input stream.

Usage

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

public static void appendFilesInTarArchive(OutputStream output, Iterable<ArtifactDataEntry> filesToAdd)
        throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();
    try (GZIPOutputStream gzs = new GZIPOutputStream(output)) {
        try (ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            for (ArtifactDataEntry entry : filesToAdd) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }/*from  ww  w .j  a  va2 s.  co m*/
        }
    } catch (ArchiveException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

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

public static void appendFilesInTarArchive(OutputStream output, byte[] currentArchive,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try (GZIPOutputStream gzs = new GZIPOutputStream(output)) {
        try (ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(currentArchive))) {
                try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzip)) {
                    TarArchiveEntry tarEntry = null;

                    while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
                        aos.putArchiveEntry(tarEntry);
                        IOUtils.copy(tarInputStream, aos);
                        aos.closeArchiveEntry();
                    }/*from   w  w w  .  j a  v a 2 s.  co  m*/
                }
            }

            for (ArtifactDataEntry entry : filesToAdd) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
        }
    } catch (ArchiveException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Lower level pack operation/*from w  w w . j a v  a2 s . c  o  m*/
 * 
 * @param source
 *            source file
 * @param destination
 *            destination file
 * @return the number of bytes processed
 * @throws ArchiveException
 * @throws IOException
 */
public static long pack(String source, File destination) throws ArchiveException, IOException {

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    OutputStream outStream = new FileOutputStream(destination);

    ArchiveOutputStream outputStream;
    try {
        outputStream = factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, outStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(outStream);

        throw e;
    }

    File startDirectory = new File(source);

    RecursiveArchiver archiver = new RecursiveArchiver(outputStream);

    try {
        archiver.archive(startDirectory);
        outputStream.flush();
        outputStream.close();

        outStream.flush();
        outStream.close();

    } catch (IOException e) {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(outputStream);

        throw e;
    }

    long ret = outputStream.getBytesWritten();

    if (logger.isDebugEnabled()) {
        logger.debug("Packed " + ret + " bytes");
    }

    return ret;
}

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

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *//*from  ww w.j  a v a 2  s.  c  o  m*/
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:com.geewhiz.pacify.utils.ArchiveUtils.java

public static void replaceFilesInArchive(File archive, String archiveType, Map<String, File> filesToReplace) {
    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    File manifest = null;/*ww w. j  a  v a 2s  .co  m*/
    InputStream archiveStream = null;
    ArchiveInputStream ais = null;
    ArchiveOutputStream aos = null;
    List<FileInputStream> streamsToClose = new ArrayList<FileInputStream>();

    File tmpArchive = FileUtils.createEmptyFileWithSamePermissions(archive);

    try {
        aos = factory.createArchiveOutputStream(archiveType, new FileOutputStream(tmpArchive));
        ChangeSet changes = new ChangeSet();

        if (ArchiveStreamFactory.JAR.equalsIgnoreCase(archiveType)) {
            manifest = manifestWorkaround(archive, archiveType, aos, changes, streamsToClose);
        }

        for (String filePath : filesToReplace.keySet()) {
            File replaceWithFile = filesToReplace.get(filePath);

            ArchiveEntry archiveEntry = aos.createArchiveEntry(replaceWithFile, filePath);
            FileInputStream fis = new FileInputStream(replaceWithFile);
            streamsToClose.add(fis);
            changes.add(archiveEntry, fis, true);
        }

        archiveStream = new FileInputStream(archive);
        ais = factory.createArchiveInputStream(archiveType, archiveStream);

        ChangeSetPerformer performer = new ChangeSetPerformer(changes);
        performer.perform(ais, aos);

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ArchiveException e) {
        throw new RuntimeException(e);
    } finally {
        for (FileInputStream fis : streamsToClose) {
            IOUtils.closeQuietly(fis);
        }
        IOUtils.closeQuietly(aos);
        IOUtils.closeQuietly(ais);
        IOUtils.closeQuietly(archiveStream);
    }

    if (manifest != null) {
        manifest.delete();
    }

    if (!archive.delete()) {
        throw new RuntimeException("Couldn't delete file [" + archive.getPath() + "]... Aborting!");
    }
    if (!tmpArchive.renameTo(archive)) {
        throw new RuntimeException("Couldn't rename filtered file from [" + tmpArchive.getPath() + "] to ["
                + archive.getPath() + "]... Aborting!");
    }
}

From source file:cpcc.vvrte.services.TarArchiveDemo.java

@Test
public void shouldWriteTarFile() throws IOException, ArchiveException {
    byte[] c1 = "content1\n".getBytes("UTF-8");
    byte[] c2 = "content2 text\n".getBytes("UTF-8");

    Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L));
    Date t2 = new Date(t1.getTime() - 30000);

    FileOutputStream fos = new FileOutputStream("bugger1.tar");

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos);

    TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1");
    archiveEntry1.setModTime(t1);/*from ww  w.  j  av a2  s  .com*/
    archiveEntry1.setSize(c1.length);
    archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE);
    archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE);

    outStream.putArchiveEntry(archiveEntry1);
    outStream.write(c1);
    outStream.closeArchiveEntry();

    TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2");
    archiveEntry2.setModTime(t2);
    archiveEntry2.setSize(c2.length);
    archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO);
    archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO);

    outStream.putArchiveEntry(archiveEntry2);
    outStream.write(c2);
    outStream.closeArchiveEntry();

    outStream.close();

    FileInputStream fis = new FileInputStream("bugger1.tar");
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis);

    TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry1.getModTime()).isEqualTo(t1);
    assertThat(entry1.getSize()).isEqualTo(c1.length);
    assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE);
    assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE);
    assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE);
    assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE);
    ByteArrayOutputStream b1 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b1);
    b1.close();
    assertThat(b1.toByteArray().length).isEqualTo(c1.length);
    assertThat(b1.toByteArray()).isEqualTo(c1);

    TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry2.getModTime()).isEqualTo(t2);
    assertThat(entry2.getSize()).isEqualTo(c2.length);
    assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO);
    assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO);
    assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO);
    assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b2);
    b2.close();
    assertThat(b2.toByteArray().length).isEqualTo(c2.length);
    assertThat(b2.toByteArray()).isEqualTo(c2);

    TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry3).isNull();

    inStream.close();
}

From source file:at.beris.virtualfile.provider.LocalArchivedFileOperationProvider.java

@Override
public void create(FileModel model) throws IOException {
    try {//  ww  w. java2s.c o m
        // if not exists create UrlArchive
        // insert or update ArchiveEntry
        FileOutputStream fileOutputStream = new FileOutputStream(new java.io.File(model.getUrl().toURI()));
        ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
        ArchiveOutputStream archiveOutputStream = archiveStreamFactory
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fileOutputStream);
        archiveOutputStream.close();

    } catch (ArchiveException | URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:ezbake.deployer.cli.commands.SSLCertsCommand.java

@Override
public void call() throws IOException, TException {
    String[] args = globalParameters.unparsedArgs;
    minExpectedArgs(2, args, this);
    String securityId = args[0];/*w ww .j a v a2  s.c  o  m*/
    String filePath = args[1];

    List<ArtifactDataEntry> certs = new ArrayList<>();
    EzSecurityRegistration.Client client = null;
    ThriftClientPool pool = poolSupplier.get();
    try {
        client = pool.getClient(EzSecurityRegistrationConstants.SERVICE_NAME,
                EzSecurityRegistration.Client.class);

        AppCerts s = client.getAppCerts(
                getSecurityToken(pool.getSecurityId(EzSecurityRegistrationConstants.SERVICE_NAME)), 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(
                        new File(new File(SSL_CONFIG_DIRECTORY, securityId), fieldName));
                certs.add(new ArtifactDataEntry(tae, (byte[]) o));
            }
        }

        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        FileOutputStream fos = new FileOutputStream(filePath);
        GZIPOutputStream gzs = new GZIPOutputStream(fos);
        try (TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            for (ArtifactDataEntry entry : certs) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
            aos.finish();
            gzs.finish();
        } catch (ArchiveException ex) {
            throw new DeploymentException(ex.getMessage());
        } finally {
            IOUtils.closeQuietly(fos);
        }
    } finally {
        pool.returnToPool(client);
    }
}

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

public void buildDistributionInArchive(String distZip, @Nullable String jdkArchivePath, String path,
        String archiveOutType) throws Exception {
    myListener.info("Build: " + path);

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    final File fileZip = new File(myDistPath, distZip);

    final List<String> executables = Arrays.asList(ourExecutable);

    try (OutputStream pathStream = createOutputStream(archiveOutType, path)) {
        ArchiveOutputStream archiveOutputStream = factory.createArchiveOutputStream(archiveOutType, pathStream);
        if (archiveOutputStream instanceof TarArchiveOutputStream) {
            ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        }//from  ww w.  j a  v  a2 s. co m

        // move Consulo to archive, and change permissions
        try (InputStream is = new FileInputStream(fileZip)) {
            try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
                ArchiveEntry tempEntry = ais.getNextEntry();
                while (tempEntry != null) {
                    final ArchiveEntryWrapper newEntry = createEntry(archiveOutType, tempEntry.getName(),
                            tempEntry);

                    newEntry.setMode(extractMode(tempEntry));
                    newEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                    if (executables.contains(tempEntry.getName())) {
                        newEntry.setMode(0b111_101_101);
                    }

                    copyEntry(archiveOutputStream, ais, tempEntry, newEntry);

                    tempEntry = ais.getNextEntry();
                }
            }
        }

        boolean mac = distZip.contains("mac");

        // jdk check
        if (jdkArchivePath != null) {
            try (InputStream is = new FileInputStream(jdkArchivePath)) {
                try (GzipCompressorInputStream gz = new GzipCompressorInputStream(is)) {
                    try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.TAR,
                            gz)) {
                        ArchiveEntry tempEntry = ais.getNextEntry();
                        while (tempEntry != null) {
                            final String name = tempEntry.getName();

                            // is our path
                            if (!mac && name.startsWith("jre/")) {
                                final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                        "Consulo/platform/buildSNAPSHOT/" + name, tempEntry);
                                jdkEntry.setMode(extractMode(tempEntry));
                                jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                            } else if (mac && name.startsWith("jdk")) {
                                boolean needAddToArchive = true;
                                for (String prefix : ourMacSkipJdkList) {
                                    if (name.startsWith(prefix)) {
                                        needAddToArchive = false;
                                    }
                                }

                                if (needAddToArchive) {
                                    final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                            "Consulo.app/Contents/platform/buildSNAPSHOT/jre/" + name,
                                            tempEntry);
                                    jdkEntry.setMode(extractMode(tempEntry));
                                    jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                    copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                                }
                            }

                            tempEntry = ais.getNextEntry();
                        }
                    }
                }
            }
        }

        archiveOutputStream.finish();
    }
}

From source file:fr.gael.ccsds.sip.archive.TarArchiveManager.java

/**
 * Produces TAR archive/* w  ww  . j a  va  2  s.co  m*/
 */
@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}