Example usage for java.util.zip ZipOutputStream putNextEntry

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

Introduction

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

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:brut.androlib.Androlib.java

private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException {
    // First, copy the contents from the existing outFile:
    Enumeration<? extends ZipEntry> entries = inputFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = new ZipEntry(entries.nextElement());

        // We can't reuse the compressed size because it depends on compression sizes.
        entry.setCompressedSize(-1);/*ww  w . j ava  2 s . co m*/
        outputFile.putNextEntry(entry);

        // No need to create directory entries in the final apk
        if (!entry.isDirectory()) {
            BrutIO.copy(inputFile, outputFile, entry);
        }

        outputFile.closeEntry();
    }
}

From source file:com.dbi.jmmerge.MapController.java

private void addDirectoryContents(String fullDirPath, File directory, ZipOutputStream zip) throws IOException {
    if (fullDirPath == null)
        fullDirPath = "";
    for (File file : directory.listFiles()) {
        if (file.isDirectory()) {
            if (fullDirPath.length() > 0) {
                addDirectoryContents(fullDirPath + "/" + file.getName(), file, zip);
            } else {
                addDirectoryContents(file.getName(), file, zip);
            }/*w  ww. java 2s . c  om*/
        } else {
            String entryName = "";
            if (fullDirPath.length() == 0) {
                entryName = file.getName();
            } else {
                entryName = fullDirPath + "/" + file.getName();
            }

            ZipEntry entry = new ZipEntry(entryName);
            zip.putNextEntry(entry);

            FileInputStream in = new FileInputStream(file);
            IOUtils.copy(in, zip);
            in.close();
            zip.closeEntry();
        }
    }
}

From source file:org.metaeffekt.dcc.controller.execution.RemoteExecutor.java

private void addDirectoryContentsToZipFile(Path rootDirectory, Path directory, ZipOutputStream zipFile,
        Set<String> includes) throws IOException {
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path path : directoryStream) {
            Path relativePath = rootDirectory.relativize(path);
            if (CollectionUtils.isEmpty(includes) || includes.contains(relativePath.toString())) {
                if (Files.isDirectory(path)) {
                    String name = relativePath.toString();
                    name = name.endsWith("/") ? name : name + "/";
                    zipFile.putNextEntry(new ZipEntry(replaceWindowsPathSeparator(name)));
                    addDirectoryContentsToZipFile(rootDirectory, path, zipFile, null);
                } else {
                    String name = relativePath.toString();
                    zipFile.putNextEntry(new ZipEntry(replaceWindowsPathSeparator(name)));
                    try (InputStream fileToBeZipped = new BufferedInputStream(Files.newInputStream(path))) {
                        IOUtils.copy(fileToBeZipped, zipFile);
                    }//w ww.j  a v  a 2 s  .c o  m
                }
            }
        }
    }
}

From source file:com.mweagle.tereus.commands.evaluation.common.LambdaUtils.java

protected void createStableZip(ZipOutputStream zipOS, Path parentDirectory, Path archiveRoot, MessageDigest md)
        throws IOException {
    // Sort & zip files
    final List<Path> childDirectories = new ArrayList<>();
    final List<Path> childFiles = new ArrayList<>();
    DirectoryStream<Path> dirStream = Files.newDirectoryStream(parentDirectory);
    for (Path eachChild : dirStream) {
        if (Files.isDirectory(eachChild)) {
            childDirectories.add(eachChild);
        } else {/*w  w  w . j  a v  a  2  s. com*/
            childFiles.add(eachChild);
        }
    }
    final int archiveRootLength = archiveRoot.toAbsolutePath().toString().length() + 1;
    childFiles.stream().sorted().forEach(eachPath -> {
        final String zeName = eachPath.toAbsolutePath().toString().substring(archiveRootLength);
        try {
            final ZipEntry ze = new ZipEntry(zeName);
            zipOS.putNextEntry(ze);
            Files.copy(eachPath, zipOS);
            md.update(Files.readAllBytes(eachPath));
            zipOS.closeEntry();
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    });

    childDirectories.stream().sorted().forEach(eachPath -> {
        try {
            createStableZip(zipOS, eachPath, archiveRoot, md);
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    });
}

From source file:com.webpagebytes.cms.controllers.FlatStorageImporterExporter.java

protected void exportFiles(ZipOutputStream zos, String path) throws WPBIOException {
    try {//www  .jav a  2  s  . c  om
        List<WPBFile> files = dataStorage.getAllRecords(WPBFile.class);
        for (WPBFile file : files) {
            String fileXmlPath = path + file.getExternalKey() + "/" + "metadata.xml";
            Map<String, Object> map = new HashMap<String, Object>();
            exporter.export(file, map);
            ZipEntry metadataZe = new ZipEntry(fileXmlPath);
            zos.putNextEntry(metadataZe);
            exportToXMLFormat(map, zos);
            zos.closeEntry();
            String contentPath = String.format(PATH_FILE_CONTENT, file.getExternalKey());
            ZipEntry contentZe = new ZipEntry(contentPath);
            zos.putNextEntry(contentZe);
            zos.closeEntry();

            if (file.getDirectoryFlag() == null || file.getDirectoryFlag() != 1) {
                try {
                    String filePath = contentPath + file.getFileName();
                    WPBFilePath cloudFile = new WPBFilePath(PUBLIC_BUCKET, file.getBlobKey());
                    InputStream is = cloudFileStorage.getFileContent(cloudFile);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    IOUtils.copy(is, bos);
                    bos.flush();
                    byte[] content = bos.toByteArray();
                    ZipEntry fileZe = new ZipEntry(filePath);
                    zos.putNextEntry(fileZe);
                    zos.write(content);
                    zos.closeEntry();
                } catch (Exception e) {
                    log.log(Level.SEVERE, " Exporting file :" + file.getExternalKey(), e);
                    // do nothing, we do not abort the export because of a failure, but we need to log this as warning
                }
            }
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        throw new WPBIOException("Cannot export files to Zip", e);
    }
}

From source file:it.eng.spagobi.tools.downloadFiles.service.DownloadZipAction.java

public void createZipFromFiles(Vector<File> files, String outputFileName, String folderName)
        throws IOException {
    logger.debug("IN");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
    // Compress the files 
    for (Iterator iterator = files.iterator(); iterator.hasNext();) {
        File file = (File) iterator.next();

        FileInputStream in = new FileInputStream(file);

        String fileName = file.getName();
        // The name to Inssert: remove the parameter folder
        //         int lastIndex=folderName.length();
        //         String fileToInsert=fileName.substring(lastIndex+1);

        logger.debug("Adding to zip entry " + fileName);
        ZipEntry zipEntry = new ZipEntry(fileName);

        // Add ZIP entry to output stream. 
        out.putNextEntry(zipEntry);

        // Transfer bytes from the file to the ZIP file 
        int len;/*from  w  ww .j a v a  2s .co  m*/
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry 
        out.closeEntry();
        in.close();
    }
    // Complete the ZIP file 
    out.close();
    logger.debug("OUT");
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinFileSystemServiceImpl.java

protected void writeSkinZipEntries(String prefix, File dir, ZipOutputStream out) throws IOException {
    if (dir.isDirectory()) {
        if (prefix.length() > 0) {
            prefix = prefix + "/";
        }//from w ww  .  j a  va2s  .c  om
        File[] myChildren = dir.listFiles();
        for (File myFile : myChildren) {
            if (myFile.isFile()) {
                writeZipEntry(prefix, out, myFile);
            } else {
                ZipEntry mySkinFile = new ZipEntry(prefix + myFile.getName() + "/");
                mySkinFile.setTime(myFile.lastModified());
                out.putNextEntry(mySkinFile);
                out.write(new byte[0]);
                out.closeEntry();
                writeSkinZipEntries(prefix + myFile.getName(), myFile, out);
            }
        }
    }
}

From source file:com.joliciel.csvLearner.CSVLearner.java

private void doCommandTrain() throws IOException {
    if (resultFilePath == null)
        throw new RuntimeException("Missing argument: resultFile");
    if (featureDir == null)
        throw new RuntimeException("Missing argument: featureDir");
    if (maxentModelFilePath == null)
        throw new RuntimeException("Missing argument: maxentModel");

    CSVEventListReader reader = this.getReader(TrainingSetType.ALL_TRAINING, false);
    GenericEvents events = reader.getEvents();

    if (generateEventFile) {
        File eventFile = new File(maxentModelFilePath + ".events.txt");
        this.generateEventFile(eventFile, events);
    }/*w  w w .  j  a  v  a2s .c  om*/

    File modelFile = new File(maxentModelFilePath);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(modelFile, false));
    zos.putNextEntry(new ZipEntry(maxentModelBaseName + ".bin"));
    this.train(events, zos);
    zos.flush();

    Writer writer = new BufferedWriter(new OutputStreamWriter(zos));
    zos.putNextEntry(new ZipEntry(maxentModelBaseName + ".nrm_limits.csv"));
    this.writeNormalisationLimits(writer);
    zos.flush();
    zos.close();

    LOG.info("#### Complete ####");
}