Example usage for org.apache.commons.compress.changes ChangeSet ChangeSet

List of usage examples for org.apache.commons.compress.changes ChangeSet ChangeSet

Introduction

In this page you can find the example usage for org.apache.commons.compress.changes ChangeSet ChangeSet.

Prototype

ChangeSet

Source Link

Usage

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;/*from w w  w . j  av a 2  s. 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:org.obm.sync.ObmSyncArchiveUtils.java

private static File[] replaceServiceJar(File[] asFile) {
    return FluentIterable.from(Arrays.asList(asFile)).transform(new Function<File, File>() {

        @Override/*from  ww  w. ja  v  a2  s  .  c o  m*/
        public File apply(File input) {
            if (input.getName().contains("services-module")) {
                ZipFile servicesZip = null;
                try {
                    File outputFile = File.createTempFile("services-module", ".jar");
                    servicesZip = new ZipFile(input);

                    ChangeSet changeSet = new ChangeSet();
                    changeSet.add(new JarArchiveEntry("META-INF/MANIFEST.MF"),
                            ClassLoader.getSystemClassLoader().getResourceAsStream("MANIFEST.MF"));
                    ChangeSetPerformer changeSetPerformer = new ChangeSetPerformer(changeSet);
                    JarArchiveOutputStream jarArchiveOutputStream = new JarArchiveOutputStream(
                            new FileOutputStream(outputFile));
                    changeSetPerformer.perform(servicesZip, jarArchiveOutputStream);
                    return outputFile;
                } catch (IOException e) {
                    Throwables.propagate(e);
                } finally {
                    try {
                        if (servicesZip != null) {
                            servicesZip.close();
                        }
                    } catch (IOException e) {
                        Throwables.propagate(e);
                    }
                }
            }
            return input;
        }
    }).toArray(File.class);
}