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(ZipArchiveEntry entry) throws ZipException 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

From source file:net.duckling.ddl.service.export.impl.ExportServiceImpl.java

private void writeTagIndex(String path, String tagname, List<Integer> rids, String tname, boolean isEpub,
        ArchiveOutputStream out, VWBContext context) {
    String html = getIndexHtml(tagname, rids, tname, isEpub, context);
    InputStream in = new ByteArrayInputStream(html.getBytes());
    try {/* w ww  .j a v  a2 s.  com*/
        out.putArchiveEntry(new ZipArchiveEntry(path + "/index.html"));
        IOUtils.copy(in, out);
        in.close();
        out.closeArchiveEntry();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:no.difi.sdp.client.asice.archive.CreateZip.java

public Archive zipIt(List<AsicEAttachable> files) {
    ByteArrayOutputStream archive = null;
    ZipArchiveOutputStream zipOutputStream = null;
    try {//  w  w  w  .ja  va  2  s  . com
        archive = new ByteArrayOutputStream();
        zipOutputStream = new ZipArchiveOutputStream(archive);
        zipOutputStream.setEncoding(Charsets.UTF_8.name());
        zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        for (AsicEAttachable file : files) {
            log.trace("Adding " + file.getFileName() + " to archive. Size in bytes before compression: "
                    + file.getBytes().length);
            ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getFileName());
            zipEntry.setSize(file.getBytes().length);

            zipOutputStream.putArchiveEntry(zipEntry);
            IOUtils.copy(new ByteArrayInputStream(file.getBytes()), zipOutputStream);
            zipOutputStream.closeArchiveEntry();
        }
        zipOutputStream.finish();
        zipOutputStream.close();

        return new Archive(archive.toByteArray());
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    } finally {
        IOUtils.closeQuietly(archive);
        IOUtils.closeQuietly(zipOutputStream);
    }
}

From source file:org.alfresco.repo.download.ZipDownloadExporter.java

@Override
public void startNode(NodeRef nodeRef) {
    this.currentName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
    path.push(new Pair<String, NodeRef>(currentName, nodeRef));
    if (ContentModel.TYPE_FOLDER.equals(nodeService.getType(nodeRef))) {
        String path = getPath() + PATH_SEPARATOR;
        ZipArchiveEntry archiveEntry = new ZipArchiveEntry(path);
        try {/*w  w  w  .j  a  v  a 2 s  .  co m*/
            zipStream.putArchiveEntry(archiveEntry);
            zipStream.closeArchiveEntry();
        } catch (IOException e) {
            throw new ExporterException("Unexpected IOException adding folder entry", e);
        }
    }
}

From source file:org.alfresco.repo.download.ZipDownloadExporter.java

@Override
public void contentImpl(NodeRef nodeRef, QName property, InputStream content, ContentData contentData,
        int index) {
    // if the content stream to output is empty, then just return content descriptor as is
    if (content == null) {
        return;/*  www  .j  a v  a2s. c o  m*/
    }

    try {
        // ALF-2016
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(getPath());
        zipStream.putArchiveEntry(zipEntry);

        // copy export stream to zip
        copyStream(zipStream, content);

        zipStream.closeArchiveEntry();
        filesAddedCount = filesAddedCount + 1;
    } catch (IOException e) {
        throw new ExporterException("Failed to zip export stream", e);
    }
}

From source file:org.alfresco.repo.exporter.ACPExportPackageHandler.java

public ContentData exportContent(InputStream content, ContentData contentData) {
    // if the content stream to output is empty, then just return content descriptor as is
    if (content == null) {
        return contentData;
    }/* w ww.j ava2s  .c om*/

    // create zip entry for stream to export
    String contentDirPath = contentDir.getPath();
    if (contentDirPath.charAt(contentDirPath.length() - 1) != '.' && contentDirPath.lastIndexOf('.') != -1) {
        contentDirPath = contentDirPath.substring(0, contentDirPath.lastIndexOf("."));
    }

    File file;
    if (exportAsFolders && nodeService != null && contentData instanceof NodeContentData) {
        NodeContentData nodeContentData = (NodeContentData) contentData;
        file = new File(contentDirPath + toDisplayPath(nodeService.getPath(nodeContentData.getNodeRef())));
    } else {
        String extension = "bin";
        if (mimetypeService != null) {
            String mimetype = contentData.getMimetype();
            if (mimetype != null && mimetype.length() > 0) {
                try {
                    extension = mimetypeService.getExtension(mimetype);
                } catch (AlfrescoRuntimeException e) {
                    // use default extension
                }
            }
        }
        file = new File(contentDirPath, "content" + iFileCnt++ + "." + extension);
    }

    try {
        // ALF-2016
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getPath());
        zipStream.putArchiveEntry(zipEntry);

        // copy export stream to zip
        copyStream(zipStream, content);
    } catch (IOException e) {
        throw new ExporterException("Failed to zip export stream", e);
    }

    return new ContentData(file.getPath(), contentData.getMimetype(), contentData.getSize(),
            contentData.getEncoding());
}

From source file:org.alfresco.repo.exporter.ACPExportPackageHandler.java

public void endExport() {
    // ensure data file has .xml extension
    String dataFilePath = dataFile.getPath();
    if (!dataFilePath.endsWith(".xml")) {
        dataFilePath += (dataFilePath.charAt(dataFilePath.length() - 1) == '.') ? "xml" : ".xml";
    }/*from www . jav  a  2 s.co m*/

    // add data file to zip stream
    // ALF-2016
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(dataFilePath);

    try {
        // close data file stream and place temp data file into zip output stream
        tempDataFileStream.close();
        // ALF-2016
        zipStream.putArchiveEntry(zipEntry);
        InputStream dataFileStream = new FileInputStream(tempDataFile);
        copyStream(zipStream, dataFileStream);
        // ALF-2016
        zipStream.closeArchiveEntry();
        dataFileStream.close();
    } catch (IOException e) {
        throw new ExporterException("Failed to zip data stream file", e);
    }

    try {
        // close zip stream
        zipStream.close();
    } catch (IOException e) {
        throw new ExporterException("Failed to close zip package stream", e);
    }
}

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

public Zip() {
    setFactory(new ZipStreamFactory() {
        public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException {
            ZipArchiveOutputStream o = (ZipArchiveOutputStream) super.getArchiveStream(stream, encoding);
            configure(o);/* w  ww . j a v  a2 s  . c  o m*/
            return o;
        }

        public ArchiveOutputStream getArchiveOutputStream(File f, String encoding) throws IOException {
            ZipArchiveOutputStream o = (ZipArchiveOutputStream) super.getArchiveOutputStream(f, encoding);
            configure(o);
            return o;
        }
    });
    setEntryBuilder(new ArchiveBase.EntryBuilder() {
        public ArchiveEntry buildEntry(ArchiveBase.ResourceWithFlags r) {
            boolean isDir = r.getResource().isDirectory();
            ZipArchiveEntry ent = new ZipArchiveEntry(r.getName());
            ent.setTime(round(r.getResource().getLastModified(), 2000));
            ent.setSize(isDir ? 0 : r.getResource().getSize());

            if (!isDir && r.getCollectionFlags().hasModeBeenSet()) {
                ent.setUnixMode(r.getCollectionFlags().getMode());
            } else if (isDir && r.getCollectionFlags().hasDirModeBeenSet()) {
                ent.setUnixMode(r.getCollectionFlags().getDirMode());
            } else if (r.getResourceFlags().hasModeBeenSet()) {
                ent.setUnixMode(r.getResourceFlags().getMode());
            } else {
                ent.setUnixMode(isDir ? ArchiveFileSet.DEFAULT_DIR_MODE : ArchiveFileSet.DEFAULT_FILE_MODE);
            }

            if (r.getResourceFlags().getZipExtraFields() != null) {
                ent.setExtraFields(r.getResourceFlags().getZipExtraFields());
            }

            if (keepCompression && r.getResourceFlags().hasCompressionMethod()) {
                ent.setMethod(r.getResourceFlags().getCompressionMethod());
            }

            return ent;
        }
    });
    setFileSetBuilder(new ArchiveBase.FileSetBuilder() {
        public ArchiveFileSet buildFileSet(Resource dest) {
            ArchiveFileSet afs = new ZipFileSet();
            afs.setSrcResource(dest);
            return afs;
        }
    });
}

From source file:org.apache.camel.processor.aggregate.ZipAggregationStrategy.java

@Override
public void onCompletion(Exchange exchange) {
    List<Exchange> list = exchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class);
    try {/*from   w  w w .  j av  a2 s . com*/
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipArchiveOutputStream zout = new ZipArchiveOutputStream(bout);
        for (Exchange item : list) {
            String name = item.getProperty(ZIP_ENTRY_NAME,
                    item.getProperty(Exchange.FILE_NAME, item.getExchangeId(), String.class), String.class);
            byte[] body = item.getIn().getBody(byte[].class);
            ZipArchiveEntry entry = new ZipArchiveEntry(name);
            entry.setSize(body.length);
            zout.putArchiveEntry(entry);
            zout.write(body);
            zout.closeArchiveEntry();
        }
        zout.close();
        exchange.getIn().setBody(bout.toByteArray());
        exchange.removeProperty(Exchange.GROUPED_EXCHANGE);
    } catch (Exception e) {
        throw new RuntimeException("Unable to zip exchanges!", e);
    }
}

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanJobHelper.java

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs")
private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException {
    try {//from ww w .  j  a v  a2  s . c  o m
        @Cleanup
        OutputStream archiveStream = new FileOutputStream(zipFile);
        @Cleanup
        ArchiveOutputStream archive = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);

        for (File fileToAdd : filesToAdd) {
            ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName());
            archive.putArchiveEntry(entry);

            @Cleanup
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd));
            IOUtils.copy(input, archive);
            archive.closeArchiveEntry();
        }

        archive.finish();
    } catch (ArchiveException e) {
        throw new IOException("Issue with creating archive", e);
    }
}

From source file:org.apache.karaf.tooling.ArchiveMojo.java

private void addFileToZip(ZipArchiveOutputStream tOut, Path f, String base) throws IOException {
    if (Files.isDirectory(f)) {
        String entryName = base + f.getFileName().toString() + "/";
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
        tOut.putArchiveEntry(zipEntry);// w ww  . j ava 2s  .c o m
        tOut.closeArchiveEntry();
        try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
            for (Path child : children) {
                addFileToZip(tOut, child, entryName);
            }
        }
    } else if (useSymLinks && Files.isSymbolicLink(f)) {
        String entryName = base + f.getFileName().toString();
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
        zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM);
        tOut.putArchiveEntry(zipEntry);
        tOut.write(Files.readSymbolicLink(f).toString().getBytes());
        tOut.closeArchiveEntry();
    } else {
        String entryName = base + f.getFileName().toString();
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
        zipEntry.setSize(Files.size(f));
        if (entryName.contains("/bin/") || (!usePathPrefix && entryName.startsWith("bin"))) {
            if (!entryName.endsWith(".bat")) {
                zipEntry.setUnixMode(0755);
            } else {
                zipEntry.setUnixMode(0644);
            }
        }
        tOut.putArchiveEntry(zipEntry);
        Files.copy(f, tOut);
        tOut.closeArchiveEntry();
    }
}