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

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

Introduction

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

Prototype

String ZIP

To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory ZIP.

Click Source Link

Document

Constant used to identify the ZIP archive format.

Usage

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();//from w  w w.  ja va 2  s. co  m
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

From source file:fr.duminy.jbackup.core.archive.zip.ZipArchiveInputStream.java

ZipArchiveInputStream(InputStream input) throws Exception {
    this.input = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, input);
}

From source file:fr.gael.dhus.util.UnZip.java

public static void unCompress(String zip_file, String output_folder)
        throws IOException, CompressorException, ArchiveException {
    ArchiveInputStream ais = null;/*from   ww  w  .ja va2s .c o m*/
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    FileInputStream fis = new FileInputStream(new File(zip_file));

    if (zip_file.toLowerCase().endsWith(".tar")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    } else if (zip_file.toLowerCase().endsWith(".zip")) {
        ais = asf.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis);
    } else if (zip_file.toLowerCase().endsWith(".tgz") || zip_file.toLowerCase().endsWith(".tar.gz")) {
        CompressorInputStream cis = new CompressorStreamFactory()
                .createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        ais = asf.createArchiveInputStream(new BufferedInputStream(cis));
    } else {
        try {
            fis.close();
        } catch (IOException e) {
            LOGGER.warn("Cannot close FileInputStream:", e);
        }
        throw new IllegalArgumentException("Format not supported: " + zip_file);
    }

    File output_file = new File(output_folder);
    if (!output_file.exists())
        output_file.mkdirs();

    // copy the existing entries
    ArchiveEntry nextEntry;
    while ((nextEntry = ais.getNextEntry()) != null) {
        File ftemp = new File(output_folder, nextEntry.getName());
        if (nextEntry.isDirectory()) {
            ftemp.mkdir();
        } else {
            FileOutputStream fos = FileUtils.openOutputStream(ftemp);
            IOUtils.copy(ais, fos);
            fos.close();
        }
    }
    ais.close();
    fis.close();
}

From source file:fr.duminy.jbackup.core.archive.zip.ZipArchiveOutputStream.java

ZipArchiveOutputStream(OutputStream output) throws ArchiveException {
    this.output = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, output);

}

From source file:com.geewhiz.pacify.model.PArchiveBase.java

public String getType() {
    String type = getInternalType();

    if ("jar".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }//from  ww  w.  j a  va  2  s  .co  m
    if ("war".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }
    if ("ear".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }
    if ("zip".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.ZIP;
    }
    if ("tar".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.TAR;
    }
    return type;
}

From source file:com.impetus.ankush.agent.utils.ZipFiles.java

/**
 * Zip file.// w  ww. java 2  s  . c  o  m
 *
 * @param filePath the file path
 * @return the string
 */
public String zipFile(String filePath) {
    try {
        /* Create Output Stream that will have final zip files */
        OutputStream zipOutput = new FileOutputStream(new File(filePath + ".zip"));
        /*
         * Create Archive Output Stream that attaches File Output Stream / and
         * specifies type of compression
         */
        ArchiveOutputStream logicalZip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipOutput);
        /* Create Archieve entry - write header information */
        logicalZip.putArchiveEntry(new ZipArchiveEntry(FilenameUtils.getName(filePath)));
        /* Copy input file */
        IOUtils.copy(new FileInputStream(new File(filePath)), logicalZip);
        /* Close Archieve entry, write trailer information */
        logicalZip.closeArchiveEntry();

        /* Finish addition of entries to the file */
        logicalZip.finish();
        /* Close output stream, our files are zipped */
        zipOutput.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }
    return filePath + ".zip";
}

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

@Override
public void create(FileModel model) throws IOException {
    try {// w  w w  .  j ava  2  s. co 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:com.geewhiz.pacify.utils.ArchiveUtils.java

public static String getArchiveType(String archiveName) {
    int idx = archiveName.lastIndexOf(".");
    String type = archiveName.substring(idx + 1);

    if ("jar".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }//from www.  ja v  a 2 s . c om
    if ("war".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }
    if ("ear".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.JAR;
    }
    if ("zip".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.ZIP;
    }
    if ("tar".equalsIgnoreCase(type)) {
        return ArchiveStreamFactory.TAR;
    }

    return type;
}

From source file:com.mirth.connect.util.ArchiveUtils.java

/**
 * Create an archive file from files/sub-folders in a given source folder.
 * //from  ww  w . j  a v a2 s  .c  o m
 * @param sourceFolder
 *            Sub-folders and files inside this folder will be copied into the archive
 * @param destinationFile
 *            The destination archive file
 * @param archiver
 *            The archiver format, see
 *            org.apache.commons.compress.archivers.ArchiveStreamFactory
 * @param compressor
 *            The compressor format, see
 *            org.apache.commons.compress.compressors.CompressorStreamFactory
 * @throws CompressException
 */
public static void createArchive(File sourceFolder, File destinationFile, String archiver, String compressor)
        throws CompressException {
    if (!sourceFolder.isDirectory() || !sourceFolder.exists()) {
        throw new CompressException("Invalid source folder: " + sourceFolder.getAbsolutePath());
    }

    logger.debug("Creating archive \"" + destinationFile.getAbsolutePath() + "\" from folder \""
            + sourceFolder.getAbsolutePath() + "\"");
    OutputStream outputStream = null;
    ArchiveOutputStream archiveOutputStream = null;

    try {
        /*
         * The commons-compress documentation recommends constructing a ZipArchiveOutputStream
         * with the archive file when using the ZIP archive format. See
         * http://commons.apache.org/proper/commons-compress/zip.html
         */
        if (archiver.equals(ArchiveStreamFactory.ZIP) && compressor == null) {
            archiveOutputStream = new ZipArchiveOutputStream(destinationFile);
        } else {
            // if not using ZIP format, use the archiver/compressor stream factories to initialize the archive output stream
            outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile));

            if (compressor != null) {
                outputStream = new CompressorStreamFactory().createCompressorOutputStream(compressor,
                        outputStream);
            }

            archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiver, outputStream);
        }

        createFolderArchive(sourceFolder, archiveOutputStream,
                sourceFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR);
    } catch (Exception e) {
        throw new CompressException(e);
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
        IOUtils.closeQuietly(outputStream);
        logger.debug("Finished creating archive \"" + destinationFile.getAbsolutePath() + "\"");
    }
}

From source file:algorithm.ZipPackaging.java

@Override
public File encapsulate(File carrier, List<File> payloadList) throws IOException {
    String outputName = getOutputFileName(carrier);
    int dotIndex = outputName.lastIndexOf('.');
    if (dotIndex > 0) {
        outputName = outputName.substring(0, dotIndex) + ".zip";
    }/*from  ww w.  j av a 2 s. c  o m*/
    File zipFile = new File(outputName);
    try {
        FileOutputStream outputStream = new FileOutputStream(zipFile);
        ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputStream);
        archiveFile(archiveOutputStream, carrier);
        for (File payload : payloadList) {
            archiveFile(archiveOutputStream, payload);
        }
        archiveOutputStream.close();
        outputStream.close();
    } catch (ArchiveException e) {
    }
    return zipFile;
}