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

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

Introduction

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

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Writes all necessary data for this entry.

Usage

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

/**
 * Recursively create ZIP archive from directory
 *///w ww .  j av a2 s .  co 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.openkm.util.ArchiveUtils.java

/**
 * Create ZIP archive from file//w  w w  .jav  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:fr.gael.dhus.datastore.processing.impl.ProcessProductPrepareDownload.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  w w . j av  a2 s.c o m*/
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, 65535);
            zOut.closeArchiveEntry();
        } finally {
            fInputStream.close();
        }
    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

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

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);/*from w  w  w.  ja v a  2s. c o m*/

    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:com.silverpeas.util.ZipManager.java

/**
 * Compress a file into a zip file.//from   w  w w  .  jav  a2 s.com
 *
 * @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.silverpeas.util.ZipManager.java

/**
 * Mthode compressant un dossier de faon rcursive au format zip.
 *
 * @param folderToZip - dossier  compresser
 * @param zipFile - fichier zip  creer/*ww  w .  j  a va  2  s . c o  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.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  w w w  . ja  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.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 {/*from w ww . ja v 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.citytechinc.cq.component.maven.util.ComponentMojoUtil.java

/**
 * Add files to the already constructed Archive file by creating a new
 * Archive file, appending the contents of the existing Archive file to it,
 * and then adding additional entries for the newly constructed artifacts.
 * //from  w  w  w  .  j  a  v a  2 s.c  o m
 * @param classList
 * @param classLoader
 * @param classPool
 * @param buildDirectory
 * @param componentPathBase
 * @param defaultComponentPathSuffix
 * @param defaultComponentGroup
 * @param existingArchiveFile
 * @param tempArchiveFile
 * @throws OutputFailureException
 * @throws IOException
 * @throws InvalidComponentClassException
 * @throws InvalidComponentFieldException
 * @throws ParserConfigurationException
 * @throws TransformerException
 * @throws ClassNotFoundException
 * @throws CannotCompileException
 * @throws NotFoundException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 */
public static void buildArchiveFileForProjectAndClassList(List<CtClass> classList,
        WidgetRegistry widgetRegistry, TouchUIWidgetRegistry touchUIWidgetRegistry, ClassLoader classLoader,
        ClassPool classPool, File buildDirectory, String componentPathBase, String defaultComponentPathSuffix,
        String defaultComponentGroup, File existingArchiveFile, File tempArchiveFile,
        ComponentNameTransformer transformer, boolean generateTouchUiDialogs) throws OutputFailureException,
        IOException, InvalidComponentClassException, InvalidComponentFieldException,
        ParserConfigurationException, TransformerException, ClassNotFoundException, CannotCompileException,
        NotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException,
        TouchUIDialogWriteException, TouchUIDialogGenerationException {

    if (!existingArchiveFile.exists()) {
        throw new OutputFailureException("Archive file does not exist");
    }

    if (tempArchiveFile.exists()) {
        tempArchiveFile.delete();
    }

    tempArchiveFile.createNewFile();

    deleteTemporaryComponentOutputDirectory(buildDirectory);

    /*
     * Create archive input stream
     */
    ZipArchiveInputStream existingInputStream = new ZipArchiveInputStream(
            new FileInputStream(existingArchiveFile));

    /*
     * Create a zip archive output stream for the temp file
     */
    ZipArchiveOutputStream tempOutputStream = new ZipArchiveOutputStream(tempArchiveFile);

    /*
     * Iterate through all existing entries adding them to the new archive
     */
    ZipArchiveEntry curArchiveEntry;

    Set<String> existingArchiveEntryNames = new HashSet<String>();

    while ((curArchiveEntry = existingInputStream.getNextZipEntry()) != null) {
        existingArchiveEntryNames.add(curArchiveEntry.getName().toLowerCase());
        getLog().debug("Current File Name: " + curArchiveEntry.getName());
        tempOutputStream.putArchiveEntry(curArchiveEntry);
        IOUtils.copy(existingInputStream, tempOutputStream);
        tempOutputStream.closeArchiveEntry();
    }

    /*
     * Create content.xml within temp archive
     */
    ContentUtil.buildContentFromClassList(classList, tempOutputStream, existingArchiveEntryNames,
            buildDirectory, componentPathBase, defaultComponentPathSuffix, defaultComponentGroup, transformer);

    /*
     * Create Dialogs within temp archive
     */
    DialogUtil.buildDialogsFromClassList(transformer, classList, tempOutputStream, existingArchiveEntryNames,
            widgetRegistry, classLoader, classPool, buildDirectory, componentPathBase,
            defaultComponentPathSuffix);

    if (generateTouchUiDialogs) {
        TouchUIDialogUtil.buildDialogsFromClassList(classList, classLoader, classPool, touchUIWidgetRegistry,
                transformer, buildDirectory, componentPathBase, defaultComponentPathSuffix, tempOutputStream,
                existingArchiveEntryNames);
    }

    /*
     * Create edit config within temp archive
     */
    EditConfigUtil.buildEditConfigFromClassList(classList, tempOutputStream, existingArchiveEntryNames,
            buildDirectory, componentPathBase, defaultComponentPathSuffix, transformer);

    /*
     * Copy temp archive to the original archive position
     */
    tempOutputStream.finish();
    existingInputStream.close();
    tempOutputStream.close();

    existingArchiveFile.delete();
    tempArchiveFile.renameTo(existingArchiveFile);

}

From source file:at.spardat.xma.xdelta.JarPatcher.java

/**
 * Close entry./*  w ww .j  a va 2s.c om*/
 *
 * @param output the output
 * @param outEntry the out entry
 * @param crc the crc
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void closeEntry(ZipArchiveOutputStream output, ZipArchiveEntry outEntry, long crc) throws IOException {
    output.flush();
    output.closeArchiveEntry();
    if (outEntry.getCrc() != crc)
        throw new IOException("CRC mismatch for " + outEntry.getName());
}