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

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

Introduction

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

Prototype

public ArchiveEntry createArchiveEntry(File inputFile, String entryName) throws IOException 

Source Link

Document

Creates a new zip entry taking some information from the given file and using the provided name.

Usage

From source file:net.larry1123.elec.util.logger.LoggerDirectoryHandler.java

protected void packFile(File file, ZipArchiveOutputStream zipArchiveOutputStream) throws IOException {
    String relative = getDirectoryPath().relativize(file.toPath()).toString();
    // Make an Entry in the archive
    ArchiveEntry archiveEntry = zipArchiveOutputStream.createArchiveEntry(file, relative);
    FileInputStream fileInputStream = FileUtils.openInputStream(file);
    zipArchiveOutputStream.putArchiveEntry(archiveEntry);
    // Copy file content into archive
    IOUtils.copy(fileInputStream, zipArchiveOutputStream);
    zipArchiveOutputStream.closeArchiveEntry();
}

From source file:org.apache.james.mailbox.backup.Zipper.java

private void storeInArchive(Mailbox mailbox, ZipArchiveOutputStream archiveOutputStream) throws IOException {
    String name = mailbox.getName();
    ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new Directory(name),
            name);/* www.j  av  a  2 s .c o  m*/

    archiveEntry.addExtraField(new MailboxIdExtraField(mailbox.getMailboxId().serialize()));
    archiveEntry.addExtraField(new UidValidityExtraField(mailbox.getUidValidity()));

    archiveOutputStream.putArchiveEntry(archiveEntry);
    archiveOutputStream.closeArchiveEntry();
}

From source file:org.apache.james.mailbox.backup.Zipper.java

private void storeInArchive(MailboxMessage message, ZipArchiveOutputStream archiveOutputStream)
        throws IOException {
    String entryId = message.getMessageId().serialize();
    ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File(entryId),
            entryId);//from   w w  w .  j a va 2  s.c  om

    archiveEntry.addExtraField(new SizeExtraField(message.getFullContentOctets()));
    archiveEntry.addExtraField(new UidExtraField(message.getUid().asLong()));
    archiveEntry.addExtraField(new MessageIdExtraField(message.getMessageId().serialize()));
    archiveEntry.addExtraField(new MailboxIdExtraField(message.getMailboxId().serialize()));
    archiveEntry.addExtraField(new InternalDateExtraField(message.getInternalDate()));
    archiveEntry.addExtraField(new FlagsExtraField(message.createFlags()));

    archiveOutputStream.putArchiveEntry(archiveEntry);
    IOUtils.copy(message.getFullContent(), archiveOutputStream);
    archiveOutputStream.closeArchiveEntry();
}

From source file:org.orderofthebee.addons.support.tools.repo.LogFilesZIPPost.java

/**
 *
 * {@inheritDoc}//w  w w.jav  a  2 s  .  c o m
 */
@Override
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    final Map<String, Object> model = new HashMap<>();
    final Status status = new Status();
    final Cache cache = new Cache(this.getDescription().getRequiredCache());
    model.put("status", status);
    model.put("cache", cache);

    final Object parsedContent = req.parseContent();
    if (!(parsedContent instanceof FormData)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST,
                "No or invalid request data provided - only form data is supported");
    }

    final FormData rqData = (FormData) parsedContent;
    final List<String> filePaths = new ArrayList<>();
    final String[] paths = rqData.getParameters().get("paths");
    filePaths.addAll(Arrays.asList(paths));

    final List<File> files = this.validateFilePaths(filePaths);

    final File logFileZip = TempFileProvider.createTempFile("ootbee-support-tools-logFiles", "zip");
    try {
        try {
            final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip);
            try {
                for (final File logFile : files) {
                    final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName());
                    zipOS.putArchiveEntry(archiveEntry);

                    final FileInputStream fis = new FileInputStream(logFile);
                    try {
                        final byte[] buf = new byte[10240];
                        while (fis.read(buf) != -1) {
                            zipOS.write(buf);
                        }
                    } finally {
                        fis.close();
                    }

                    zipOS.closeArchiveEntry();
                }
            } finally {
                zipOS.close();
            }
        } catch (final IOException ioEx) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx);
        }

        this.delegate.streamContent(req, res, logFileZip, logFileZip.lastModified(), false, "log-files.zip",
                model);
    } finally {
        // eager cleanup
        if (!logFileZip.delete()) {
            logFileZip.deleteOnExit();
        }
    }
}

From source file:org.orderofthebee.addons.support.tools.share.LogFileHandler.java

protected void createZip(final List<File> files, final File logFileZip) {
    try {//www.  j a  va  2s  .  c o m
        final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip);
        try {
            for (final File logFile : files) {
                final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName());
                zipOS.putArchiveEntry(archiveEntry);

                final FileInputStream fis = new FileInputStream(logFile);
                try {
                    final byte[] buf = new byte[10240];
                    while (fis.read(buf) != -1) {
                        zipOS.write(buf);
                    }
                } finally {
                    fis.close();
                }

                zipOS.closeArchiveEntry();
            }
        } finally {
            zipOS.close();
        }
    } catch (final IOException ioEx) {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx);
    }
}

From source file:org.orderofthebee.addons.support.tools.share.LogFilesZIPPost.java

/**
 *
 * {@inheritDoc}//from  w w  w.  j  a v  a2s. co m
 */
@Override
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    final Map<String, Object> model = new HashMap<>();
    final Status status = new Status();
    final Cache cache = new Cache(this.getDescription().getRequiredCache());
    model.put("status", status);
    model.put("cache", cache);

    final Object parsedContent = req.parseContent();
    if (!(parsedContent instanceof FormData)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST,
                "No or invalid request data provided - only form data is supported");
    }

    final FormData rqData = (FormData) parsedContent;
    final List<String> filePaths = new ArrayList<>();
    final String[] paths = rqData.getParameters().get("paths");
    filePaths.addAll(Arrays.asList(paths));

    final List<File> files = this.validateFilePaths(filePaths);

    final File logFileZip = TempFileProvider.createTempFile("ootbee-support-tools-logFiles", "zip");
    try {
        try {
            final ZipArchiveOutputStream zipOS = new ZipArchiveOutputStream(logFileZip);
            try {
                for (final File logFile : files) {
                    final ArchiveEntry archiveEntry = zipOS.createArchiveEntry(logFile, logFile.getName());
                    zipOS.putArchiveEntry(archiveEntry);

                    final FileInputStream fis = new FileInputStream(logFile);
                    try {
                        final byte[] buf = new byte[10240];
                        while (fis.read(buf) != -1) {
                            zipOS.write(buf);
                        }
                    } finally {
                        fis.close();
                    }

                    zipOS.closeArchiveEntry();
                }
            } finally {
                zipOS.close();
            }
        } catch (final IOException ioEx) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Error creating ZIP file", ioEx);
        }

        this.streamContent(req, res, logFileZip, logFileZip.lastModified(), false, "log-files.zip", model,
                "application/zip");
    } finally {
        // eager cleanup
        if (!logFileZip.delete()) {
            logFileZip.deleteOnExit();
        }
    }
}

From source file:org.sourcepit.tools.shared.resources.internal.mojo.ZipUtils.java

private static void appendFileOrDirectory(final int pathOffset, final ZipArchiveOutputStream zipOut,
        File fileOrDirectory) throws IOException, FileNotFoundException {
    final String entryName = getZipEntryName(pathOffset, fileOrDirectory.getAbsolutePath());
    final ArchiveEntry zipEntry = zipOut.createArchiveEntry(fileOrDirectory, entryName);
    zipOut.putArchiveEntry(zipEntry);/*from w  w w. j  a  v  a 2s.  co  m*/

    final boolean isDirectory = fileOrDirectory.isDirectory();
    try {
        if (!isDirectory) {
            final InputStream fis = new FileInputStream(fileOrDirectory);
            try {
                IOUtils.copy(fis, zipOut);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }
    } finally {
        zipOut.closeArchiveEntry();
    }

    if (isDirectory) {
        for (File file : fileOrDirectory.listFiles()) {
            appendFileOrDirectory(pathOffset, zipOut, file);
        }
    }
}