Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry

Introduction

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

Prototype

public ZipArchiveEntry(File inputFile, String entryName) 

Source Link

Document

Creates a new zip entry taking some information from the given file and using the provided name.

Usage

From source file:com.daphne.es.maintain.editor.web.controller.utils.CompressUtils.java

private static void addFilesToCompression(ZipArchiveOutputStream zaos, File file, String dir)
        throws IOException {

    ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, dir + file.getName());
    zaos.putArchiveEntry(zipArchiveEntry);

    if (file.isFile()) {
        BufferedInputStream bis = null;
        try {//from  www. j a v  a 2s  .  co m
            bis = new BufferedInputStream(new FileInputStream(file));
            IOUtils.copy(bis, zaos);
            zaos.closeArchiveEntry();
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(bis);
        }
    } else if (file.isDirectory()) {
        zaos.closeArchiveEntry();

        for (File childFile : file.listFiles()) {
            addFilesToCompression(zaos, childFile, dir + file.getName() + File.separator);
        }
    }
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.ArchiveEntryFactory.java

public ArchiveEntry create(final File file, final String fileName) {
    switch (compressionType) {
    case None://from   w w  w .ja va  2s  . c  o m
    case Zip:
        return new ZipArchiveEntry(file, fileName);
    case Tar:
    case TarGz:
        return new TarArchiveEntry(file, fileName);
    }

    return null;
}

From source file:com.sic.bb.jenkins.plugins.sicci_for_xcode.util.ExtendedFile.java

private static void zipDirectory(File file, String path, ZipArchiveOutputStream zipStream)
        throws IOException, InterruptedException {
    if (path == null)
        path = new String();
    else if (!path.isEmpty())
        path += File.separatorChar;

    ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, path + file.getName());
    zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode());

    /* TODO: archiving symlinks doesn't work atm
    zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode());
            // w  w  w  . ja v a 2s  .  c  o m
    if(Util.isSymlink(file)) {
       zipEntry = new ZipArchiveEntry(path + file.getName());
       zipEntry.setUnixMode(PosixAPI.get().stat(file.getAbsolutePath()).mode());
               
       AsiExtraField field = new AsiExtraField();
       field.setLinkedFile(path + file.getName());
               
       zipEntry.addExtraField(field);
        zipStream.putArchiveEntry(zipEntry);
      zipStream.closeArchiveEntry();
        return;
    }
    */

    zipStream.putArchiveEntry(zipEntry);

    if (!file.isDirectory()) {
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(file);
            Util.copyStream(fileInputStream, zipStream);
        } finally {
            IOUtils.closeQuietly(fileInputStream);
            zipStream.closeArchiveEntry();
        }
    } else {
        zipStream.closeArchiveEntry();

        String[] entries = file.list();

        if (entries != null)
            for (String entry : entries)
                zipDirectory(new File(file, entry), path + file.getName(), zipStream);
    }
}

From source file:au.org.ala.biocache.util.AlaFileUtils.java

/**
 * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory
 * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip.
 *
 * @param zOut The zip file's output stream
 * @param path The filesystem path of the file/directory being added
 * @param base The base prefix to for the name of the zip file entry
 *
 * @throws IOException If anything goes wrong
 *//*w  ww. j  a  v a2s  .  c om*/
private static void addFileToZip(ZipArchiveOutputStream zOut, String path, String base) throws IOException {
    File f = new File(path);
    String entryName = base + f.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

    zOut.putArchiveEntry(zipEntry);

    if (f.isFile()) {
        FileInputStream fInputStream = null;
        try {
            fInputStream = new FileInputStream(f);
            IOUtils.copy(fInputStream, zOut);
            zOut.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(fInputStream);
        }

    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                addFileToZip(zOut, child.getAbsolutePath(), entryName + "/");
            }
        }
    }
}

From source file:aiai.apps.commons.utils.ZipUtils.java

/**
 * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory
 * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip.
 *
 * @param zOut The zip file's output stream
 * @param f The filesystem path of the file/directory being added
 * @param base The base prefix to for the name of the zip file entry
 *
 * @throws IOException If anything goes wrong
 *//*from   w  ww.  j a  va 2s  .c om*/
private static void addFileToZip(ZipArchiveOutputStream zOut, File f, String base) throws IOException {
    String entryName = base + f.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

    zOut.putArchiveEntry(zipEntry);

    if (f.isFile()) {
        try (FileInputStream fInputStream = new FileInputStream(f)) {
            IOUtils.copy(fInputStream, zOut);
            zOut.closeArchiveEntry();
        }
    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                addFileToZip(zOut, child.getAbsoluteFile(), entryName + "/");
            }
        }
    }
}

From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidSourceTest.java

@Test
public void test() {

    ZipArchiveOutputStream zos = null;/*from  ww  w. jav  a2 s .  co m*/
    File archive;
    String filename = "";
    try {
        archive = File.createTempFile("test.", ".zip", null);
        archive.deleteOnExit();
        filename = archive.getAbsolutePath();
        zos = new ZipArchiveOutputStream(archive);

        for (int z = 0; z < 3; z++) {
            for (int x = 0; x < Math.pow(2, z); x++) {
                for (int y = 0; y < Math.pow(2, z); y++) {
                    ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(),
                            "test/tiles/" + z + "/" + x + "/" + y + ".dummy");
                    zos.putArchiveEntry(entry);
                }
            }
        }

        ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json");
        zos.putArchiveEntry(entry);

        zos.closeArchiveEntry();
        zos.close();
        zos = null;
    } catch (IOException e) {
        fail(e.getMessage());
    }

    try {
        ZipResourcePyramidSource src = new ZipResourcePyramidSource(filename, "dummy");

        TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1);
        InputStream is = src.getSourceTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        tileDef = new TileIndex(2, 3, 3, 1, 1);
        is = src.getSourceTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        is = src.getSourceMetaDataStream("test");
        Assert.assertTrue(is != null);

    } catch (IOException e) {
        fail(e.getMessage());
    }

}

From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidStreamSourceTest.java

@Test
public void test() {

    ZipArchiveOutputStream zos = null;//www .  j a  v a  2  s.  c o  m
    File archive;
    String filename = "";
    try {
        archive = File.createTempFile("test.", ".zip", null);
        archive.deleteOnExit();
        filename = archive.getAbsolutePath();
        zos = new ZipArchiveOutputStream(archive);

        for (int z = 0; z < 3; z++) {
            for (int x = 0; x < Math.pow(2, z); x++) {
                for (int y = 0; y < Math.pow(2, z); y++) {
                    ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(),
                            "test/tiles/" + z + "/" + x + "/" + y + ".dummy");
                    zos.putArchiveEntry(entry);
                }
            }
        }

        ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json");
        zos.putArchiveEntry(entry);

        zos.closeArchiveEntry();
        zos.close();
        zos = null;
    } catch (IOException e) {
        fail(e.getMessage());
    }

    try {
        ZipResourcePyramidStreamSource src = new ZipResourcePyramidStreamSource(filename, "dummy");

        TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1);
        InputStream is = src.getTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        tileDef = new TileIndex(2, 3, 3, 1, 1);
        is = src.getTileStream("test", tileDef);
        Assert.assertTrue(is != null);

        is = src.getMetaDataStream("test");
        Assert.assertTrue(is != null);

    } catch (IOException e) {
        fail(e.getMessage());
    }

}

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

/**
 * Produces Zip compressed archive./*from  w  w  w  .  j a v  a  2 s.  c  o m*/
 */
@Override
public File copy(final File src, final File zip_file, final String dst) throws Exception {
    if (zip_file.exists()) {
        final FileInputStream fis = new FileInputStream(zip_file);
        final ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);

        final File tempFile = File.createTempFile("updateZip", "zip");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        // copy the existing entries
        ZipArchiveEntry nextEntry;
        while ((nextEntry = zis.getNextZipEntry()) != null) {
            zos.putArchiveEntry(nextEntry);
            IOUtils.copy(zis, zos);
            zos.closeArchiveEntry();
        }

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

        zos.finish();
        zis.close();
        fis.close();
        zos.close();

        // Rename the new file over the old
        boolean status = zip_file.delete();
        File saved_tempFile = tempFile;
        status = tempFile.renameTo(zip_file);

        // Copy the new file over the old if the renaming failed
        if (!status) {
            final FileInputStream tfis = new FileInputStream(saved_tempFile);
            final FileOutputStream tfos = new FileOutputStream(zip_file);

            final byte[] buf = new byte[1024];
            int i = 0;

            while ((i = tfis.read(buf)) != -1) {
                tfos.write(buf, 0, i);
            }

            tfis.close();
            tfos.close();

            saved_tempFile.delete();
        }

        return zip_file;

    } else {
        final FileOutputStream fos = new FileOutputStream(zip_file);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst);
        entry.setSize(src.length());
        zos.putArchiveEntry(entry);

        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, zos);
        sfis.close();

        zos.closeArchiveEntry();

        zos.finish();
        zos.close();
        fos.close();
    }
    return zip_file;
}

From source file:hudson.util.io.ZipArchiver.java

public void visit(final File f, final String relativePath) throws IOException {
    int mode = IOUtils.mode(f);

    // ZipArchiveEntry already covers all the specialities we used to handle here:
    // - Converts backslashes to slashes
    // - Handles trailing slash of directories
    // - Sets entry's time from file
    // - Sets bitmask from setUnixMode() argument.

    ZipArchiveEntry zae = new ZipArchiveEntry(f, relativePath);
    if (mode != -1) {
        zae.setUnixMode(mode);//from   w  w w.ja  v a  2 s  .  co m
    }
    zip.putArchiveEntry(zae);
    if (!zae.isDirectory()) {
        FileInputStream in = new FileInputStream(f);
        try {
            int len;
            while ((len = in.read(buf)) >= 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
    zip.closeArchiveEntry();
    entriesWritten++;
}

From source file:com.streamsets.datacollector.restapi.ZipEdgeArchiveBuilder.java

protected void addArchiveEntry(ArchiveOutputStream archiveOutput, Object fileContent, String pipelineId,
        String fileName) throws IOException {
    File pipelineFile = File.createTempFile(pipelineId, fileName);
    FileOutputStream pipelineOutputStream = new FileOutputStream(pipelineFile);
    ObjectMapperFactory.get().writeValue(pipelineOutputStream, fileContent);
    pipelineOutputStream.flush();// w w  w . j a  v  a  2s. c om
    pipelineOutputStream.close();
    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(pipelineFile,
            DATA_PIPELINES_FOLDER + pipelineId + "/" + fileName);
    archiveEntry.setSize(pipelineFile.length());
    archiveOutput.putArchiveEntry(archiveEntry);
    IOUtils.copy(new FileInputStream(pipelineFile), archiveOutput);
    archiveOutput.closeArchiveEntry();
}