Example usage for java.util.zip ZipOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:org.cloudfoundry.client.lib.io.DynamicZipInputStreamTest.java

@Test
public void shouldCreateValidZipContent() throws Exception {

    byte[] f1 = newRandomBytes(10000);
    byte[] f2 = newRandomBytes(10000);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(bos);
    zipOutputStream.putNextEntry(new ZipEntry("a/b/c"));
    zipOutputStream.write(f1);//from   www.  j  a  va2  s. co  m
    zipOutputStream.closeEntry();
    zipOutputStream.putNextEntry(new ZipEntry("a/b/c/d/"));
    zipOutputStream.closeEntry();
    zipOutputStream.putNextEntry(new ZipEntry("d/e/f"));
    zipOutputStream.write(f2);
    zipOutputStream.closeEntry();
    zipOutputStream.flush();
    zipOutputStream.close();
    byte[] expected = bos.toByteArray();

    List<DynamicZipInputStream.Entry> entries = new ArrayList<DynamicZipInputStream.Entry>();
    entries.add(newEntry("a/b/c", f1));
    entries.add(newEntry("a/b/c/d/", null));
    entries.add(newEntry("d/e/f", f2));
    DynamicZipInputStream inputStream = new DynamicZipInputStream(entries);
    bos.reset();
    FileCopyUtils.copy(inputStream, bos);
    byte[] actual = bos.toByteArray();

    assertThat(actual, is(equalTo(expected)));
}

From source file:processing.app.tools.Archiver.java

public void run() {
    SketchController sketch = editor.getSketchController();

    // first save the sketch so that things don't archive strangely
    boolean success = false;
    try {/*  w  w  w .  j  a v a  2s  .co  m*/
        success = sketch.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!success) {
        Base.showWarning(tr("Couldn't archive sketch"),
                tr("Archiving the sketch has been canceled because\nthe sketch couldn't save properly."), null);
        return;
    }

    File location = sketch.getSketch().getFolder();
    String name = location.getName();
    File parent = new File(location.getParent());

    //System.out.println("loc " + location);
    //System.out.println("par " + parent);

    File newbie = null;
    String namely = null;
    int index = 0;
    do {
        // only use the date if the sketch name isn't the default name
        useDate = !name.startsWith("sketch_");

        if (useDate) {
            String purty = dateFormat.format(new Date());
            String stamp = purty + ((char) ('a' + index));
            namely = name + "-" + stamp;
            newbie = new File(parent, namely + ".zip");

        } else {
            String diggie = numberFormat.format(index + 1);
            namely = name + "-" + diggie;
            newbie = new File(parent, namely + ".zip");
        }
        index++;
    } while (newbie.exists());

    // open up a prompt for where to save this fella
    FileDialog fd = new FileDialog(editor, tr("Archive sketch as:"), FileDialog.SAVE);
    fd.setDirectory(parent.getAbsolutePath());
    fd.setFile(newbie.getName());
    fd.setVisible(true);

    String directory = fd.getDirectory();
    String filename = fd.getFile();

    // only write the file if not canceled
    if (filename != null) {
        newbie = new File(directory, filename);

        ZipOutputStream zos = null;
        try {
            //System.out.println(newbie);
            zos = new ZipOutputStream(new FileOutputStream(newbie));

            // recursively fill the zip file
            buildZip(location, name, zos);

            // close up the jar file
            zos.flush();
            editor.statusNotice("Created archive " + newbie.getName() + ".");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(zos);
        }
    } else {
        editor.statusNotice(tr("Archive sketch canceled."));
    }
}

From source file:org.talend.utils.io.FilesUtils.java

/**
 * /*from   w  w  w . j a va 2s  .  c om*/
 * DOC zshen Comment method "zip".
 * 
 * @param out
 * @param f
 * @param base
 * @param fileFilter optional
 * @throws IOException
 */
private static void zips(ZipOutputStream out, File f, String base, FileFilter fileFilter) throws IOException {
    if (f.isDirectory()) {
        base += f.getName() + '/';
        out.putNextEntry(new ZipEntry(base));
        for (File element : f.listFiles(fileFilter)) {
            zips(out, element, base, fileFilter);
        }
    } else {
        out.putNextEntry(new ZipEntry(base + f.getName()));
        InputStream in = new FileInputStream(f);

        byte[] b = new byte[BUFFER_SIZE];
        int readBytes = 0;
        while ((readBytes = in.read(b, 0, BUFFER_SIZE)) != -1) {
            out.write(b, 0, readBytes);
        }
        in.close();
        out.flush();
    }
}

From source file:org.geoserver.data.util.IOUtils.java

/**
 * See {@link #zipDirectory(File, ZipOutputStream, FilenameFilter)}, this version handles the prefix needed
 * to recursively zip data preserving the relative path of each
 *//* w ww  .ja  v a2s  . co  m*/
private static void zipDirectory(File directory, String prefix, ZipOutputStream zipout,
        final FilenameFilter filter) throws IOException, FileNotFoundException {
    File[] files = directory.listFiles(filter);
    // copy file by reading 4k at a time (faster than buffered reading)
    byte[] buffer = new byte[4 * 1024];
    for (File file : files) {
        if (file.exists()) {
            if (file.isDirectory()) {
                // recurse and append
                zipDirectory(file, prefix + file.getName() + "/", zipout, filter);
            } else {
                ZipEntry entry = new ZipEntry(prefix + file.getName());
                zipout.putNextEntry(entry);

                InputStream in = new FileInputStream(file);
                int c;
                try {
                    while (-1 != (c = in.read(buffer))) {
                        zipout.write(buffer, 0, c);
                    }
                    zipout.closeEntry();
                } finally {
                    in.close();
                }
            }
        }
    }
    zipout.flush();
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl.ExportPhdIndividualProgramProcessesInHtml.java

static byte[] createZip(final PhdProgramPublicCandidacyHashCode hashCode) {

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = null;
    try {/*from   ww w  .  ja  v a 2 s.c  o  m*/
        zip = new ZipOutputStream(outputStream);

        int count = 1;
        for (final PhdProgramProcessDocument document : hashCode.getIndividualProgramProcess()
                .getCandidacyProcessDocuments()) {
            final ZipEntry zipEntry = new ZipEntry(count + "-" + document.getFilename());
            zip.putNextEntry(zipEntry);

            // TODO: use in local context copy(new ByteArrayInputStream(new
            // byte[20]), zip);
            copy(document.getStream(), zip);

            zip.closeEntry();
            count++;
        }
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (zip != null) {
            try {
                zip.flush();
                zip.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return outputStream.toByteArray();
}

From source file:org.bimserver.collada.OpenGLTransmissionFormatSerializer.java

private void zipTheDirectory(OutputStream outputStream, Path writeDirectory) throws IOException {
    // Create the archive.
    ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    // Copy the files into the ZIP file.
    for (Path f : PathUtils.list(writeDirectory)) {
        addToZipFile(f, zipOutputStream);
    }//from ww w.  j a  v  a 2  s . c o  m
    // Push the data into the parent stream (gets returned to the server).
    zipOutputStream.finish();
    zipOutputStream.flush();
}

From source file:org.wso2.carbon.pc.core.transfer.ProcessExport.java

/**
 * @param srcFolder//from   w  w w.  java 2 s .  c o  m
 * @param destZipFile
 * @throws Exception
 */
private void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
}

From source file:org.theospi.portfolio.presentation.export.PresentationExport.java

public void createZip(OutputStream out) throws IOException {
    File directory = new File(tempDirectory + webappName);

    CheckedOutputStream checksum = null;
    ZipOutputStream zos = null;
    try {//from   w ww .  j  av a2 s.c om
        checksum = new CheckedOutputStream(out, new Adler32());
        zos = new ZipOutputStream(new BufferedOutputStream(checksum));
        recurseDirectory("", directory, zos);

        zos.finish();
        zos.flush();
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
            }
        }
        if (checksum != null) {
            try {
                checksum.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:org.olat.core.util.openxml.OpenXMLDocumentTest.java

@Test
public void writeDoc() throws Exception {
    FileOutputStream fileOut = new FileOutputStream(new File("/HotCoffee/tmp/test_1_min.docx"));
    ZipOutputStream out = new ZipOutputStream(fileOut);

    OpenXMLDocument document = new OpenXMLDocument();
    String html = "<table style='height: 80px;' width='446'><tbody><tr><td>1-1</td><td colspan='2' rowspan='2'>1-21-32-32-2</td><td>1-4</td></tr><tr><td>2-1</td><td>2-4</td></tr><tr><td>3-1</td><td>3-2</td><td colspan='2'>3-33-4</td></tr></tbody></table>";
    document.appendHtmlText(html, false);

    OpenXMLDocumentWriter writer = new OpenXMLDocumentWriter();
    writer.createDocument(out, document);

    out.flush();
    fileOut.flush();//from www . j a  v  a2  s  .c  o  m
    IOUtils.closeQuietly(out);
    IOUtils.closeQuietly(fileOut);
}

From source file:com.sonicle.webtop.vfs.PublicService.java

private void writeStoreFile(HttpServletResponse response, int storeId, String filePath, String outFileName) {
    try {//www  . jav  a2s  .  c o m
        FileObject fo = null;
        try {
            fo = manager.getStoreFile(storeId, filePath);

            if (fo.isFile()) {
                String mediaType = ServletHelper.guessMediaType(fo.getName().getBaseName(), true);
                ServletUtils.setFileStreamHeaders(response, mediaType, DispositionType.ATTACHMENT, outFileName);
                ServletUtils.setContentLengthHeader(response, fo.getContent().getSize());
                IOUtils.copy(fo.getContent().getInputStream(), response.getOutputStream());

            } else if (fo.isFolder()) {
                ServletUtils.setFileStreamHeaders(response, "application/zip", DispositionType.ATTACHMENT,
                        outFileName);

                ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
                try {
                    VfsUtils.zipFileObject(fo, zos, true);
                    zos.flush();
                } finally {
                    IOUtils.closeQuietly(zos);
                }
            }

        } finally {
            IOUtils.closeQuietly(fo);
        }

    } catch (Exception ex) {
        logger.error("Error in DownloadFile", ex);
        ServletUtils.sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}