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:se.crisp.codekvast.agent.daemon.worker.impl.ZipFileDataExporterImpl.java

private void doExportDatabaseTable(ZipOutputStream zip, CSVWriter csvWriter, String table, String... columns)
        throws IOException {
    zip.putNextEntry(ExportFileEntry.fromString(table + ".csv").toZipEntry());
    csvWriter.writeNext(columns, false);

    String[] line = new String[columns.length];
    String sql = asList(columns).stream().collect(joining(", ", "SELECT ", " FROM " + table));
    jdbcTemplate.query(sql, rs -> {//from www  .java  2 s.  c  o m
        for (int i = 0; i < columns.length; i++) {
            line[i] = rs.getString(i + 1);
        }
        csvWriter.writeNext(line, false);
    });

    csvWriter.flush();
    zip.closeEntry();
}

From source file:com.googlecode.clearnlp.component.AbstractStatisticalComponent.java

/** Called by {@link AbstractStatisticalComponent#saveModels(ZipOutputStream)}}. */
protected void saveStatisticalModels(ZipOutputStream zout, String entryName) throws Exception {
    int i, size = s_models.length;
    PrintStream fout;//from   w w w . j av a 2s . co  m

    for (i = 0; i < size; i++) {
        zout.putNextEntry(new ZipEntry(entryName + i));
        fout = UTOutput.createPrintBufferedStream(zout);
        s_models[i].save(fout);
        fout.flush();
        zout.closeEntry();
    }
}

From source file:com.joliciel.talismane.machineLearning.linearsvm.LinearSVMOneVsRestModel.java

@Override
public void writeModelToStream(OutputStream outputStream) {
    try {/* www . j a  v a2 s  . c  om*/
        ZipOutputStream zos = new ZipOutputStream(outputStream);
        zos.setLevel(ZipOutputStream.STORED);
        int i = 0;
        for (Model model : models) {
            LOG.debug("Writing model " + i + " for outcome " + outcomes.get(i));
            ZipEntry zipEntry = new ZipEntry("model" + i);
            i++;
            zos.putNextEntry(zipEntry);
            Writer writer = new OutputStreamWriter(zos, "UTF-8");
            Writer unclosableWriter = new UnclosableWriter(writer);
            model.save(unclosableWriter);
            zos.closeEntry();
            zos.flush();
        }
    } catch (UnsupportedEncodingException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.googlecode.dex2jar.v3.DexExceptionHandlerImpl.java

public void dumpException(DexFileReader reader, File errorFile) throws IOException {
    for (Map.Entry<Method, Exception> e : exceptions.entrySet()) {
        System.err.println("Error:" + e.getKey().toString() + "->" + e.getValue().getMessage());
    }//w  w  w  .  j  a  v a  2 s  .c o m

    final ZipOutputStream errorZipOutputStream = new ZipOutputStream(FileUtils.openOutputStream(errorFile));
    errorZipOutputStream.putNextEntry(new ZipEntry("summary.txt"));
    final PrintWriter fw = new PrintWriter(new OutputStreamWriter(errorZipOutputStream, "UTF-8"));
    fw.println(getVersionString());
    fw.println("there are " + exceptions.size() + " error methods");
    fw.print("options: ");
    if ((readerConfig & DexFileReader.SKIP_DEBUG) == 0) {
        fw.print(" -d");
    }
    fw.println();
    fw.flush();
    errorZipOutputStream.closeEntry();
    final Out out = new Out() {

        @Override
        public void pop() {

        }

        @Override
        public void push() {

        }

        @Override
        public void s(String s) {
            fw.println(s);
        }

        @Override
        public void s(String format, Object... arg) {
            fw.println(String.format(format, arg));
        }
    };
    final int[] count = new int[] { 0 };
    reader.accept(new EmptyVisitor() {

        @Override
        public DexClassVisitor visit(int accessFlags, String className, String superClass,
                String[] interfaceNames) {
            return new EmptyVisitor() {

                @Override
                public DexMethodVisitor visitMethod(final int accessFlags, final Method method) {
                    if (exceptions.containsKey(method)) {
                        return new EmptyVisitor() {

                            @Override
                            public DexCodeVisitor visitCode() {
                                try {
                                    errorZipOutputStream.putNextEntry(new ZipEntry("t" + count[0]++ + ".txt"));
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                }
                                Exception exception = exceptions.get(method);
                                exception.printStackTrace(fw);
                                out.s("");
                                out.s("DexMethodVisitor mv=cv.visitMethod(%s, %s);",
                                        Escape.methodAcc(accessFlags), Escape.v(method));
                                out.s("DexCodeVisitor code = mv.visitCode();");
                                return new ASMifierCodeV(out);
                            }

                            @Override
                            public void visitEnd() {
                                out.s("mv.visitEnd();");
                                fw.flush();
                                try {
                                    errorZipOutputStream.closeEntry();
                                } catch (IOException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        };
                    }
                    return null;
                }

            };
        }

    }, readerConfig);
    errorZipOutputStream.close();

}

From source file:de.mpg.imeji.logic.export.format.ZIPExport.java

/**
 * This method exports all images of the current browse page as a zip file
 * //ww  w. j  av  a 2  s .c  om
 * @throws Exception
 * @throws URISyntaxException
 */
public void exportAllImages(SearchResult sr, OutputStream out) throws URISyntaxException, Exception {
    List<String> source = sr.getResults();
    ZipOutputStream zip = new ZipOutputStream(out);
    try {
        // Create the ZIP file
        for (int i = 0; i < source.size(); i++) {
            SessionBean session = (SessionBean) BeanHelper.getSessionBean(SessionBean.class);
            ItemController ic = new ItemController(session.getUser());
            Item item = ic.retrieve(new URI(source.get(i)));
            StorageController sc = new StorageController();
            try {
                zip.putNextEntry(new ZipEntry(item.getFilename()));
                sc.read(item.getFullImageUrl().toString(), zip, false);
                // Complete the entry
                zip.closeEntry();
            } catch (ZipException ze) {
                if (ze.getMessage().contains("duplicate entry")) {
                    String name = i + "_" + item.getFilename();
                    zip.putNextEntry(new ZipEntry(name));
                    sc.read(item.getFullImageUrl().toString(), zip, false);
                    // Complete the entry
                    zip.closeEntry();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // Complete the ZIP file
        zip.close();
    }
}

From source file:org.candlepin.sync.Exporter.java

private void addSignatureToArchive(ZipOutputStream out, byte[] signature)
        throws IOException, FileNotFoundException {

    log.debug("Adding signature to archive.");
    out.putNextEntry(new ZipEntry("signature"));
    out.write(signature, 0, signature.length);
    out.closeEntry();
}

From source file:nc.noumea.mairie.appock.services.impl.ExportExcelServiceImpl.java

private void addToZipFile(File file, String nomFichier, ZipOutputStream zos) throws IOException {
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(nomFichier);
    zos.putNextEntry(zipEntry);//  www .ja  v a 2  s  . c om

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

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

From source file:de.fu_berlin.inf.dpp.core.zip.FileZipper.java

private static void internalZipFiles(List<FileWrapper> files, File archive, boolean compress,
        boolean includeDirectories, long totalSize, ZipListener listener)
        throws IOException, OperationCanceledException {

    byte[] buffer = new byte[BUFFER_SIZE];

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(archive), BUFFER_SIZE);

    ZipOutputStream zipStream = new ZipOutputStream(outputStream);

    zipStream.setLevel(compress ? Deflater.DEFAULT_COMPRESSION : Deflater.NO_COMPRESSION);

    boolean cleanup = true;
    boolean isCanceled = false;

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from w  w  w.  ja  v a  2  s.  c  o  m*/

    long totalRead = 0L;

    try {
        for (FileWrapper file : files) {
            String entryName = includeDirectories ? file.getPath() : file.getName();

            if (listener != null) {
                isCanceled = listener.update(file.getPath());
            }

            log.trace("compressing file: " + entryName);

            zipStream.putNextEntry(new ZipEntry(entryName));

            InputStream in = null;

            try {
                int read = 0;
                in = file.getInputStream();
                while (-1 != (read = in.read(buffer))) {

                    if (isCanceled) {
                        throw new OperationCanceledException(
                                "compressing of file '" + entryName + "' was canceled");
                    }

                    zipStream.write(buffer, 0, read);

                    totalRead += read;

                    if (listener != null) {
                        listener.update(totalRead, totalSize);
                    }

                }
            } finally {
                IOUtils.closeQuietly(in);
            }
            zipStream.closeEntry();
        }
        cleanup = false;
    } finally {
        IOUtils.closeQuietly(zipStream);
        if (cleanup && archive != null && archive.exists() && !archive.delete()) {
            log.warn("could not delete archive file: " + archive);
        }
    }

    stopWatch.stop();

    log.debug(String.format("created archive %s I/O: [%s]", archive.getAbsolutePath(),
            CoreUtils.throughput(archive.length(), stopWatch.getTime())));

}

From source file:org.openremote.beehive.configuration.www.UsersAPI.java

private void writeZipEntry(ZipOutputStream zipOutput, File file, java.nio.file.Path basePath)
        throws IOException {
    ZipEntry entry = new ZipEntry(basePath.relativize(file.toPath()).toString());
    entry.setSize(file.length());//w ww . j a  v a  2 s.  co  m
    entry.setTime(file.lastModified());
    zipOutput.putNextEntry(entry);

    IOUtils.copy(new FileInputStream(file), zipOutput);

    zipOutput.flush();
    zipOutput.closeEntry();
}

From source file:com.googlecode.clearnlp.component.AbstractStatisticalComponent.java

protected void saveWeightVector(ZipOutputStream zout, String entryName) throws Exception {
    int i, size = s_models.length;
    ObjectOutputStream oout;//from   w ww  .  ja va 2 s  .  co  m

    for (i = 0; i < size; i++) {
        zout.putNextEntry(new ZipEntry(entryName + i));
        oout = new ObjectOutputStream(new BufferedOutputStream(zout));
        s_models[i].saveWeightVector(oout);
        oout.flush();
        zout.closeEntry();
    }
}