stream zip file code example

Example: stream zip file

try (final OutputStream bufferedOut = new BufferedOutputStream(rawOut);
                            final ZipOutputStream out = new ZipOutputStream(bufferedOut)) {
                            out.setLevel(compressionLevel);
                            for (final FlowFile flowFile : contents) {
                                final String path = keepPath ? getPath(flowFile) : "";
                                final String entryName = path + flowFile.getAttribute(CoreAttributes.FILENAME.key());
                                final ZipEntry zipEntry = new ZipEntry(entryName);
                                zipEntry.setSize(flowFile.getSize());
                                try {
                                    out.putNextEntry(zipEntry);

                                    bin.getSession().exportTo(flowFile, out);
                                    out.closeEntry();
                                    unmerged.remove(flowFile);
                                } catch (ZipException e) {
                                    getLogger().error("Encountered exception merging {}", new Object[] {flowFile}, e);
                                }
                            }

                            out.finish();
                            out.flush();
                        }

Tags:

Misc Example