Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream putArchiveEntry

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Usage

From source file:com.openkm.util.ArchiveUtils.java

/**
 * Create ZIP archive from file/*from   w  w w  . ja  v a2s  . c o  m*/
 */
public static void createZip(File path, OutputStream os) throws IOException {
    log.debug("createZip({}, {})", new Object[] { path, os });

    if (path.exists() && path.canRead()) {
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
        zaos.setComment("Generated by OpenKM");
        zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        zaos.setUseLanguageEncodingFlag(true);
        zaos.setFallbackToUTF8(true);
        zaos.setEncoding("UTF-8");

        log.debug("FILE {}", path);
        ZipArchiveEntry zae = new ZipArchiveEntry(path.getName());
        zaos.putArchiveEntry(zae);
        FileInputStream fis = new FileInputStream(path);
        IOUtils.copy(fis, zaos);
        fis.close();
        zaos.closeArchiveEntry();

        zaos.flush();
        zaos.finish();
        zaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createZip: void");
}

From source file:com.github.braully.graph.DatabaseFacade.java

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 {/* w w  w.  j  av a2  s .  c o m*/
            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:com.ikon.util.ArchiveUtils.java

/**
 * Recursively create ZIP archive from directory 
 *///w  ww . ja va  2  s  . c om
public static void createZip(File path, String root, OutputStream os) throws IOException {
    log.debug("createZip({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
        zaos.setComment("Generated by openkm");
        zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        zaos.setUseLanguageEncodingFlag(true);
        zaos.setFallbackToUTF8(true);
        zaos.setEncoding("UTF-8");

        // Prevents java.util.zip.ZipException: ZIP file must have at least one entry
        ZipArchiveEntry zae = new ZipArchiveEntry(root + "/");
        zaos.putArchiveEntry(zae);
        zaos.closeArchiveEntry();

        createZipHelper(path, zaos, root);

        zaos.flush();
        zaos.finish();
        zaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createZip: void");
}

From source file:com.openkm.util.ArchiveUtils.java

/**
 * Recursively create ZIP archive from directory
 *//*from   w  ww  .j  a va  2 s  . c  o m*/
public static void createZip(File path, String root, OutputStream os) throws IOException {
    log.debug("createZip({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(os);
        zaos.setComment("Generated by OpenKM");
        zaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        zaos.setUseLanguageEncodingFlag(true);
        zaos.setFallbackToUTF8(true);
        zaos.setEncoding("UTF-8");

        // Prevents java.util.zip.ZipException: ZIP file must have at least one entry
        ZipArchiveEntry zae = new ZipArchiveEntry(root + "/");
        zaos.putArchiveEntry(zae);
        zaos.closeArchiveEntry();

        createZipHelper(path, zaos, root);

        zaos.flush();
        zaos.finish();
        zaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createZip: void");
}

From source file:com.silverpeas.util.ZipManager.java

/**
 * Compress a file into a zip file.//from w  w  w  .  j  ava  2  s . co  m
 *
 * @param filePath
 * @param zipFilePath
 * @return
 * @throws IOException
 */
public static long compressFile(String filePath, String zipFilePath) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
    InputStream in = new FileInputStream(filePath);
    try {
        // cration du flux zip
        zos = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath));
        zos.setFallbackToUTF8(true);
        zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE);
        zos.setEncoding(CharEncoding.UTF_8);
        String entryName = FilenameUtils.getName(filePath);
        entryName = entryName.replace(File.separatorChar, '/');
        zos.putArchiveEntry(new ZipArchiveEntry(entryName));
        IOUtils.copy(in, zos);
        zos.closeArchiveEntry();
        return new File(zipFilePath).length();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(zos);
    }
}

From source file:com.excelsiorjet.maven.plugin.JetMojo.java

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out)
        throws IOException {
    File[] files = new File(sourceDir).listFiles();
    assert files != null;
    for (File file : files) {
        if (file.isDirectory()) {
            compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out);
        } else {//from  www  .  j a v  a 2 s.c  o m
            ZipArchiveEntry entry = new ZipArchiveEntry(file.getAbsolutePath().substring(rootDir.length() + 1));
            if (Utils.isUnix() && file.canExecute()) {
                entry.setUnixMode(0100777);
            }
            out.putArchiveEntry(entry);
            InputStream in = new BufferedInputStream(
                    new FileInputStream(sourceDir + File.separator + file.getName()));
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            out.closeArchiveEntry();
        }
    }
}

From source file:com.silverpeas.util.ZipManager.java

/**
 * Mthode compressant un dossier de faon rcursive au format zip.
 *
 * @param folderToZip - dossier  compresser
 * @param zipFile - fichier zip  creer//from   www .  j a  v  a2s .  co  m
 * @return la taille du fichier zip gnr en octets
 * @throws FileNotFoundException
 * @throws IOException
 */
public static long compressPathToZip(File folderToZip, File zipFile) throws IOException {
    ZipArchiveOutputStream zos = null;
    try {
        // cration du flux zip
        zos = new ZipArchiveOutputStream(new FileOutputStream(zipFile));
        zos.setFallbackToUTF8(true);
        zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE);
        zos.setEncoding(CharEncoding.UTF_8);
        Collection<File> folderContent = FileUtils.listFiles(folderToZip, null, true);
        for (File file : folderContent) {
            String entryName = file.getPath().substring(folderToZip.getParent().length() + 1);
            entryName = FilenameUtils.separatorsToUnix(entryName);
            zos.putArchiveEntry(new ZipArchiveEntry(entryName));
            InputStream in = new FileInputStream(file);
            IOUtils.copy(in, zos);
            zos.closeArchiveEntry();
            IOUtils.closeQuietly(in);
        }
    } finally {
        if (zos != null) {
            IOUtils.closeQuietly(zos);
        }
    }
    return zipFile.length();
}

From source file:com.excelsiorjet.api.util.Utils.java

private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipArchiveOutputStream out)
        throws IOException {
    File[] files = new File(sourceDir).listFiles();
    assert files != null;
    for (File file : files) {
        if (file.isDirectory()) {
            compressDirectoryToZipfile(rootDir, sourceDir + File.separator + file.getName(), out);
        } else {//www  . j av  a2  s  .c  o m
            ZipArchiveEntry entry = new ZipArchiveEntry(file.getAbsolutePath().substring(rootDir.length() + 1));
            if (Host.isUnix()) {
                if (file.canExecute()) {
                    //set -rwxr-xr-x
                    entry.setUnixMode(0100755);
                } else {
                    //set -rw-r--r--
                    entry.setUnixMode(0100644);
                }
            }
            out.putArchiveEntry(entry);
            try (InputStream in = new BufferedInputStream(
                    new FileInputStream(sourceDir + File.separator + file.getName()))) {
                copy(in, out);
            }
            out.closeArchiveEntry();
        }
    }
}

From source file:com.glaf.core.util.ZipUtils.java

/**
 * /*w  w  w .j ava  2 s. c  o  m*/
 * ?zip?
 * 
 * @param files
 *            ?
 * 
 * @param zipFilePath
 *            ?zip ,"/var/data/aa.zip";
 */
public static void compressFile(File[] files, String zipFilePath) {
    if (files != null && files.length > 0) {
        if (isEndsWithZip(zipFilePath)) {
            ZipArchiveOutputStream zaos = null;
            try {
                File zipFile = new File(zipFilePath);
                zaos = new ZipArchiveOutputStream(zipFile);

                // Use Zip64 extensions for all entries where they are
                // required
                zaos.setUseZip64(Zip64Mode.AsNeeded);

                for (File file : files) {
                    if (file != null) {
                        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
                        zaos.putArchiveEntry(zipArchiveEntry);
                        InputStream is = null;
                        try {
                            is = new BufferedInputStream(new FileInputStream(file));
                            byte[] buffer = new byte[BUFFER];
                            int len = -1;
                            while ((len = is.read(buffer)) != -1) {
                                zaos.write(buffer, 0, len);
                            }

                            // Writes all necessary data for this entry.
                            zaos.closeArchiveEntry();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        } finally {
                            IOUtils.closeStream(is);
                        }
                    }
                }
                zaos.finish();
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeStream(zaos);
            }
        }
    }
}

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.  j a v a2s  . com
    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);
    }
}