Example usage for java.util.zip ZipOutputStream closeEntry

List of usage examples for java.util.zip ZipOutputStream closeEntry

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream closeEntry.

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createContentType(ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("[Content_Types].xml");
    zos.putNextEntry(ze);//from  w w w.  jav  a2s  .  c om

    InputStream is = getClass().getResourceAsStream("[Content_Types].xml");
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    is.close();
    zos.closeEntry();

}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

public static void editzip(InputStream stream, OutputStream outputStream, boolean close,
        EditArchiveCallback callback, String... matches) {
    ZipInputStream in = new ZipInputStream(stream);
    ZipOutputStream zout = new ZipOutputStream(outputStream);
    ZipEntry entry;/*  w ww . j a  v  a  2s  . c  o  m*/
    try {
        Map<String, File> skipped = new HashMap<String, File>();
        matches = (String[]) ArrayUtils.add(matches, ".*");
        while ((entry = in.getNextEntry()) != null) {
            String name = entry.getName();
            if (!entry.isDirectory()) {
                if (name.contains("/") && !File.separator.equals("/"))
                    name = name.replaceAll("/", File.separator + File.separator);
                if (name.contains("\\") && !File.separator.equals("\\"))
                    name = name.replaceAll("\\\\", File.separator);

                String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[0])) {
                    zout.putNextEntry(new ZipEntry(name));
                    callback.edit(dir, file, in, zout);
                    zout.closeEntry();
                } else {
                    File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16));
                    OutputStream out = FileUtils.openOutputStream(path);
                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(out);
                    skipped.put(name, path);
                }
            }
        }

        for (int i = 1; i < matches.length; i++) {
            for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator
                    .hasNext();) {
                Map.Entry<String, File> next = iterator.next();
                String name = next.getKey();
                File path = next.getValue();

                String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[i])) {
                    iterator.remove();
                    FileInputStream inputStream = FileUtils.openInputStream(path);
                    zout.putNextEntry(entry);
                    callback.edit(dir, file, inputStream, zout);
                    zout.closeEntry();
                    IOUtils.closeQuietly(inputStream);
                    FileUtils.forceDelete(path);
                }
            }
        }

        while (callback.add(zout)) {
            zout.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (close)
            IOUtils.closeQuietly(zout);
    }
}

From source file:brut.directory.ZipUtils.java

private static void processFolder(final File folder, final ZipOutputStream zipOutputStream,
        final int prefixLength) throws BrutException, IOException {
    for (final File file : folder.listFiles()) {
        if (file.isFile()) {
            final String cleanedPath = BrutIO.sanitizeUnknownFile(folder,
                    file.getPath().substring(prefixLength));
            final ZipEntry zipEntry = new ZipEntry(BrutIO.normalizePath(cleanedPath));

            // aapt binary by default takes in parameters via -0 arsc to list extensions that shouldn't be
            // compressed. We will replicate that behavior
            final String extension = FilenameUtils.getExtension(file.getAbsolutePath());
            if (mDoNotCompress != null
                    && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) {
                zipEntry.setMethod(ZipEntry.STORED);
                zipEntry.setSize(file.length());
                BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file));
                CRC32 crc = BrutIO.calculateCrc(unknownFile);
                zipEntry.setCrc(crc.getValue());
                unknownFile.close();//w  ww  .ja  va 2 s .  c  o  m
            } else {
                zipEntry.setMethod(ZipEntry.DEFLATED);
            }

            zipOutputStream.putNextEntry(zipEntry);
            try (FileInputStream inputStream = new FileInputStream(file)) {
                IOUtils.copy(inputStream, zipOutputStream);
            }
            zipOutputStream.closeEntry();
        } else if (file.isDirectory()) {
            processFolder(file, zipOutputStream, prefixLength);
        }
    }
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createRels(String id, ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("_rels/.rels");
    zos.putNextEntry(ze);//from   w w w. j  a  v  a2 s . co  m

    ByteArrayInputStream is = new ByteArrayInputStream(String.format(RELS_CONTENT, id).getBytes());
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    is.close();
    zos.closeEntry();
}

From source file:io.sledge.core.impl.installer.SledgePackageConfigurer.java

private void addToZipFile(String zipEntryName, InputStream inStream, ZipOutputStream zipStream)
        throws IOException {
    log.debug("Adding zip entry: " + zipEntryName);

    ZipEntry entry = new ZipEntry(zipEntryName);
    zipStream.putNextEntry(entry);/*from w ww  .  j a v  a  2  s. c om*/

    byte[] readBuffer = new byte[2048];
    int amountRead;
    int written = 0;

    while ((amountRead = inStream.read(readBuffer)) > 0) {
        zipStream.write(readBuffer, 0, amountRead);
        written += amountRead;
    }

    zipStream.closeEntry();
    log.debug("Written " + written + " bytes for: " + zipEntryName);
}

From source file:ee.ria.xroad.proxy.clientproxy.AsicContainerClientRequestProcessor.java

private void writeContainers(List<MessageRecord> requests, String queryId, AsicContainerNameGenerator nameGen,
        ZipOutputStream zos, String type) throws Exception {

    for (MessageRecord record : requests) {
        String filename = nameGen.getArchiveFilename(queryId, type);
        zos.putNextEntry(new ZipEntry(filename));
        zos.write(record.toAsicContainer().getBytes());
        zos.closeEntry();
    }//  w ww  .  j  av  a 2  s  .c o  m
}

From source file:com.xpn.xwiki.export.html.HtmlPackager.java

/**
 * Add a directory and all its sub-directories to the package.
 * /*from w w  w.j av a2  s.com*/
 * @param directory the directory to add.
 * @param out the ZIP output stream where to put the skin.
 * @param basePath the path where to put the directory in the package.
 * @throws IOException error when adding the directory to package.
 */
private static void addDirToZip(File directory, ZipOutputStream out, String basePath,
        Collection<String> exportedSkinFiles) throws IOException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Adding dir [" + directory.getPath() + "] to the Zip file being generated.");
    }

    if (!directory.isDirectory()) {
        return;
    }

    File[] files = directory.listFiles();

    if (files == null) {
        return;
    }

    for (int i = 0; i < files.length; ++i) {
        File file = files[i];
        if (file.isDirectory()) {
            addDirToZip(file, out, basePath + file.getName() + ZIPPATH_SEPARATOR, exportedSkinFiles);
        } else {
            String path = basePath + file.getName();

            if (exportedSkinFiles != null && exportedSkinFiles.contains(path)) {
                continue;
            }

            FileInputStream in = new FileInputStream(file);

            try {
                // Starts a new Zip entry. It automatically closes the previous entry if present.
                out.putNextEntry(new ZipEntry(path));

                try {
                    IOUtils.copy(in, out);
                } finally {
                    out.closeEntry();
                }
            } finally {
                in.close();
            }
        }
    }
}

From source file:de.uniwue.info6.database.jaxb.ScenarioExporter.java

/**
 *
 *
 * @param files/*from  w  w w.  ja  va 2  s . c o  m*/
 * @param zipfile
 * @return
 */
private File zip(List<File> files, File zipfile) {
    byte[] buf = new byte[1024];
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        for (int i = 0; i < files.size(); i++) {
            FileInputStream in = new FileInputStream(files.get(i));
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
        for (File file : files) {
            if (file.isFile() && file.exists() && file.canWrite()) {
                file.delete();
            }
        }
        return zipfile;
    } catch (Exception e) {
        LOGGER.error("FAILED TO ZIP FILES", e);
    }
    return null;
}

From source file:pl.betoncraft.betonquest.editor.model.PackageSet.java

/**
 * Saves the package to a .zip file./*from  ww  w  .  ja va 2s  .  co  m*/
 * 
 * @param zipFile
 */
public void saveToZip(File zip) {
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
        for (QuestPackage pack : packages) {
            String prefix = pack.getName().get().replace("-", File.separator) + File.separator;
            // save main.yml file
            ZipEntry main = new ZipEntry(prefix + "main.yml");
            out.putNextEntry(main);
            pack.printMainYAML(out);
            out.closeEntry();
            // save conversation files
            for (Conversation conv : pack.getConversations()) {
                ZipEntry conversation = new ZipEntry(
                        prefix + "conversations" + File.separator + conv.getId().get() + ".yml");
                out.putNextEntry(conversation);
                pack.printConversationYaml(out, conv);
                out.closeEntry();
            }
            // save events.yml file
            ZipEntry events = new ZipEntry(prefix + "events.yml");
            out.putNextEntry(events);
            pack.printEventsYaml(out);
            out.closeEntry();
            // save conditions.yml file
            ZipEntry conditions = new ZipEntry(prefix + "conditions.yml");
            out.putNextEntry(conditions);
            pack.printConditionsYaml(out);
            out.closeEntry();
            // save objectives.yml file
            ZipEntry objectives = new ZipEntry(prefix + "objectives.yml");
            out.putNextEntry(objectives);
            pack.printObjectivesYaml(out);
            out.closeEntry();
            // save items.yml file
            ZipEntry items = new ZipEntry(prefix + "items.yml");
            out.putNextEntry(items);
            pack.printItemsYaml(out);
            out.closeEntry();
            // save journal.yml file
            ZipEntry journal = new ZipEntry(prefix + "journal.yml");
            out.putNextEntry(journal);
            pack.printJournalYaml(out);
            out.closeEntry();
        }
        // done
        out.close();
    } catch (Exception e) {
        ExceptionController.display(e);
    }
}

From source file:com.music.service.PieceService.java

public void downloadPieces(OutputStream out, Collection<Piece> pieces) throws IOException {
    ZipOutputStream zip = new ZipOutputStream(out);
    for (Piece piece : pieces) {
        ZipEntry entry = new ZipEntry(piece.getTitle() + "-" + piece.getId() + ".mp3");
        zip.putNextEntry(entry);//from w  ww .  ja  va  2  s .c om
        zip.write(IOUtils.toByteArray(getPieceFile(piece.getId())));
        zip.closeEntry();

        entry = new ZipEntry(piece.getTitle() + "-" + piece.getId() + ".midi");
        zip.putNextEntry(entry);
        zip.write(IOUtils.toByteArray(getPieceMidiFile(piece.getId())));
        zip.closeEntry();
    }

    ZipEntry license = new ZipEntry("license.txt");
    zip.putNextEntry(license);
    zip.write(IOUtils.toByteArray(getClass().getResourceAsStream("/emailTemplates/license.txt")));
    zip.closeEntry();
    zip.finish();
}