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

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

Introduction

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

Prototype

public void add(final ArchiveEntry pEntry, final InputStream pInput, final boolean replace) 

Source Link

Document

Adds a new archive entry to the archive.

Usage

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

private static File manifestWorkaround(File archive, String archiveType, ArchiveOutputStream aos,
        ChangeSet changes, List<FileInputStream> streamsToClose) throws IOException {
    String manifestPath = "META-INF/MANIFEST.MF";

    if (!archiveContainsFile(archive, archiveType, manifestPath)) {
        return null;
    }//from ww w  .  ja v  a 2 s .c  o  m

    File originalManifestFile = extractFile(archive, archiveType, manifestPath);

    ArchiveEntry archiveEntry = aos.createArchiveEntry(originalManifestFile, manifestPath);
    FileInputStream fis = new FileInputStream(originalManifestFile);
    streamsToClose.add(fis);
    changes.add(archiveEntry, fis, true);

    return originalManifestFile;
}

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.ja  v a2 s.c o 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!");
    }
}