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

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

Introduction

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

Prototype

public void setUseLanguageEncodingFlag(boolean b) 

Source Link

Document

Whether to set the language encoding flag if the file name encoding is UTF-8.

Usage

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

/**
 * Recursively create ZIP archive from directory 
 *///  w  w  w .  j a  v a2s . 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

/**
 * Recursively create ZIP archive from directory
 *//*from   w w  w. j  a  v a  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.openkm.util.ArchiveUtils.java

/**
 * Create ZIP archive from file/*www . ja  v a2s . co  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.itldev.koya.alfservice.KoyaContentService.java

/**
 *
 * @param nodeRefs/*from  ww w .  j  a va  2 s .c  o  m*/
 * @return
 * @throws KoyaServiceException
 */
public File zip(List<String> nodeRefs) throws KoyaServiceException {
    File tmpZipFile = null;
    try {
        tmpZipFile = TempFileProvider.createTempFile("tmpDL", ".zip");
        FileOutputStream fos = new FileOutputStream(tmpZipFile);
        CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
        BufferedOutputStream buff = new BufferedOutputStream(checksum);
        ZipArchiveOutputStream zipStream = new ZipArchiveOutputStream(buff);
        // NOTE: This encoding allows us to workaround bug...
        // http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
        zipStream.setEncoding("UTF-8");

        zipStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        zipStream.setLevel(Deflater.BEST_COMPRESSION);

        zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
        zipStream.setUseLanguageEncodingFlag(true);
        zipStream.setFallbackToUTF8(true);

        try {
            for (String nodeRef : nodeRefs) {
                addToZip(koyaNodeService.getNodeRef(nodeRef), zipStream, "");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        } finally {
            zipStream.close();
            buff.close();
            checksum.close();
            fos.close();

        }
    } catch (IOException | WebScriptException e) {
        logger.error(e.getMessage(), e);
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    }

    return tmpZipFile;
}

From source file:com.naryx.tagfusion.expression.function.file.Zip.java

public cfData execute(cfSession session, cfArgStructData argStruct, List<cfZipItem> zipItems)
        throws cfmRunTimeException {

    String destStr = getNamedStringParam(argStruct, "zipfile", null);
    if (destStr.length() == 0 || destStr == null) {
        throwException(session, "Missing the ZIPFILE argument or is an empty string.");
    }//from w w w .  java  2s  .c o  m

    String sourceStr = getNamedStringParam(argStruct, "source", null);
    File src = null;
    if (sourceStr != null) {
        if (sourceStr.length() == 0) {
            throwException(session, "SOURCE specified is an empty string. It is not a valid path.");
        } else {
            src = new File(sourceStr);
            // Check if src is a valid file/directory
            checkZipfile(session, src);
        }
    }

    boolean recurse = getNamedBooleanParam(argStruct, "recurse", true);
    String prefixStr = getNamedStringParam(argStruct, "prefix", "");
    String prefix = prefixStr.replace('\\', '/');
    if (prefix.length() != 0 && !prefix.endsWith("/")) {
        prefix = prefix + "/";
    }

    String newpath = getNamedStringParam(argStruct, "newpath", null);
    if (newpath != null) {
        if (newpath.length() == 0) {
            throwException(session, "NEWPATH specified is an empty string. It is not a valid path.");
        }
    }
    boolean overwrite = getNamedBooleanParam(argStruct, "overwrite", false);

    int compressionLevel = getNamedIntParam(argStruct, "compressionlevel", ZipArchiveOutputStream.DEFLATED);

    if (compressionLevel < 0 || compressionLevel > 9) {
        throwException(session,
                "Invalid COMPRESSIONLEVEL specified. Please specify a number from 0-9 (inclusive).");
    }

    String filterStr = getNamedStringParam(argStruct, "filter", null);
    FilenameFilter filter = null;
    if (filterStr != null) {
        filter = new filePatternFilter(filterStr, true);
    }

    String charset = getNamedStringParam(argStruct, "charset", System.getProperty("file.encoding"));

    // Destination file
    File zipfile = new File(destStr);

    // OVERWRITE
    if (zipfile.exists() && overwrite) {
        zipfile.delete();
    } else if (zipfile.exists() && !overwrite) {
        throwException(session, "Cannot overwrite the existing file");
    }

    ZipArchiveOutputStream zipOut = null;
    FileOutputStream fout = null;

    File parent = zipfile.getParentFile();

    if (parent != null) { // create parent directories if required
        parent.mkdirs();
    }

    try {
        fout = new FileOutputStream(zipfile);

    } catch (IOException e) {
        throwException(session,
                "Failed to create zip file: [" + zipfile.getAbsolutePath() + "]. Reason: " + e.getMessage());
    }
    try {
        zipOut = new ZipArchiveOutputStream(fout);
        zipOut.setEncoding(charset);
        zipOut.setFallbackToUTF8(true);
        zipOut.setUseLanguageEncodingFlag(true);
        zipOut.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);

        // Code for Zipparams
        if (zipItems != null) {
            Iterator<cfZipItem> srcItems = zipItems.iterator();
            cfZipItem nextItem;
            while (srcItems.hasNext()) {
                nextItem = srcItems.next();
                zipOperation(session, zipOut, nextItem.getFile(), nextItem.getRecurse(), nextItem.getPrefix(),
                        compressionLevel, nextItem.getFilter(), nextItem.getNewPath());
            }
        }

        // Code for function without zippparams
        zipOperation(session, zipOut, src, recurse, prefix, compressionLevel, filter, newpath);

    } finally {
        StreamUtil.closeStream(zipOut);
        StreamUtil.closeStream(fout);
    }

    return cfBooleanData.TRUE;

}

From source file:org.apache.ant.compress.taskdefs.Zip.java

private void configure(ZipArchiveOutputStream o) {
    o.setLevel(level);//w  w  w .j  a v a2 s . co  m
    o.setComment(comment);
    o.setFallbackToUTF8(fallBackToUTF8);
    o.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
    o.setCreateUnicodeExtraFields(createUnicodeExtraFields.getPolicy());
    o.setUseZip64(zip64Mode.getPolicy());
}

From source file:org.dataconservancy.packaging.tool.impl.ZipArchiveStreamFactory.java

public ZipArchiveOutputStream newArchiveOutputStream(OutputStream out) {
    ZipArchiveOutputStream zipOs = new ZipArchiveOutputStream(out);
    zipOs.setEncoding(encoding);/*ww  w  .ja  v a 2 s. com*/
    zipOs.setFallbackToUTF8(fallbackToUTF8);
    zipOs.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
    zipOs.setLevel(level);
    zipOs.setMethod(method);
    zipOs.setUseZip64(useZip64);

    return zipOs;
}

From source file:org.structr.web.function.CreateArchiveFunction.java

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {

    if (!(sources[1] instanceof File || sources[1] instanceof Folder || sources[1] instanceof Collection
            || sources.length < 2)) {

        logParameterError(caller, sources, ctx.isJavaScriptContext());

        return usage(ctx.isJavaScriptContext());
    }//from  ww  w.ja  va2  s .c o  m

    final ConfigurationProvider config = StructrApp.getConfiguration();

    try {

        java.io.File newArchive = java.io.File.createTempFile(sources[0].toString(), "zip");

        ZipArchiveOutputStream zaps = new ZipArchiveOutputStream(newArchive);
        zaps.setEncoding("UTF8");
        zaps.setUseLanguageEncodingFlag(true);
        zaps.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
        zaps.setFallbackToUTF8(true);

        if (sources[1] instanceof File) {

            File file = (File) sources[1];
            addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps);

        } else if (sources[1] instanceof Folder) {

            Folder folder = (Folder) sources[1];
            addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps);
            addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps);

        } else if (sources[1] instanceof Collection) {

            for (Object fileOrFolder : (Collection) sources[1]) {

                if (fileOrFolder instanceof File) {

                    File file = (File) fileOrFolder;
                    addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps);
                } else if (fileOrFolder instanceof Folder) {

                    Folder folder = (Folder) fileOrFolder;
                    addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps);
                    addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps);
                } else {

                    logParameterError(caller, sources, ctx.isJavaScriptContext());
                    return usage(ctx.isJavaScriptContext());
                }
            }
        } else {

            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }

        zaps.close();

        Class archiveClass = null;

        if (sources.length > 2) {

            archiveClass = config.getNodeEntityClass(sources[2].toString());

        }

        if (archiveClass == null) {

            archiveClass = org.structr.web.entity.File.class;
        }

        try (final FileInputStream fis = new FileInputStream(newArchive)) {
            return FileHelper.createFile(ctx.getSecurityContext(), fis, "application/zip", archiveClass,
                    sources[0].toString() + ".zip");
        }

    } catch (IOException e) {

        logException(caller, e, sources);
    }
    return null;
}