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:com.eviware.soapui.impl.wsdl.support.FileAttachment.java

public void setData(byte[] data) {
    try {//from   www  .j  av a2 s .c  om
        // write attachment-data to tempfile
        ByteArrayOutputStream tempData = new ByteArrayOutputStream();
        ZipOutputStream out = new ZipOutputStream(tempData);
        out.putNextEntry(new ZipEntry(config.getName()));
        config.setSize(data.length);
        out.write(data);
        out.closeEntry();
        out.finish();
        out.close();
        config.setData(tempData.toByteArray());
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:org.fenixedu.start.controller.StartController.java

private ResponseEntity<byte[]> build(Map<String, byte[]> project, ProjectRequest request) throws IOException {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(stream);

    for (Map.Entry<String, byte[]> mapEntry : project.entrySet()) {
        ZipEntry entry = new ZipEntry(request.getArtifactId() + "/" + mapEntry.getKey());
        zip.putNextEntry(entry);
        zip.write(mapEntry.getValue());//  w  w  w .  jav  a2s  .c  o m
        zip.closeEntry();
    }

    zip.close();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/zip");
    headers.add("Content-Disposition", "attachment; filename=\"" + request.getArtifactId() + ".zip\"");
    return new ResponseEntity<>(stream.toByteArray(), headers, HttpStatus.OK);
}

From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java

public Message toMessage(Object object, MessageProperties props) throws MessageConversionException {
    if (object instanceof BatchMessage) {
        BatchMessage batch = (BatchMessage) object;
        props.setCorrelationId(batch.getId().getBytes());
        props.setContentType("application/zip");

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream zout = new ZipOutputStream(bout);
        for (Map.Entry<String, String> msg : batch.getMessages().entrySet()) {
            ZipEntry zentry = new ZipEntry(msg.getKey());
            try {
                zout.putNextEntry(zentry);
                zout.write(msg.getValue().getBytes());
                zout.closeEntry();/*from www .java2s.c om*/
            } catch (IOException e) {
                throw new MessageConversionException(e.getMessage(), e);
            }
        }

        try {
            zout.flush();
            zout.close();
        } catch (IOException e) {
            throw new MessageConversionException(e.getMessage(), e);
        }

        return new Message(bout.toByteArray(), props);
    } else {
        throw new MessageConversionException(
                "Cannot convert object " + String.valueOf(object) + " using " + getClass().toString());
    }
}

From source file:Main.java

/**
 * zips a directory/*from  w w w  .  j  a  v a 2  s . co  m*/
 * @param dir2zip
 * @param zipOut
 * @param zipFileName
 * @throws IOException
 */
private static void zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName) throws IOException {
    File zipDir = new File(dir2zip);
    // get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            // if the File object is a directory, call this
            // function again to add its content recursively
            String filePath = f.getPath();
            zipDir(filePath, zipOut, zipFileName);
            // loop again
            continue;
        }

        if (f.getName().equals(zipFileName)) {
            continue;
        }
        // if we reached here, the File object f was not a directory
        // create a FileInputStream on top of f
        final InputStream fis = new BufferedInputStream(new FileInputStream(f));
        try {
            ZipEntry anEntry = new ZipEntry(f.getPath().substring(dir2zip.length() + 1));
            // place the zip entry in the ZipOutputStream object
            zipOut.putNextEntry(anEntry);
            // now write the content of the file to the ZipOutputStream
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zipOut.write(readBuffer, 0, bytesIn);
            }
        } finally {
            // close the Stream
            fis.close();
        }
    }
}

From source file:com.genericworkflownodes.knime.nodes.io.outputfile.OutputFileNodeModel.java

/**
 * {@inheritDoc}//from w ww  .  ja v a2 s .  c o  m
 */
@Override
protected void saveInternals(final File internDir, final ExecutionMonitor exec)
        throws IOException, CanceledExecutionException {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File(internDir, "loadeddata")));
    ZipEntry entry = new ZipEntry("rawdata.bin");
    out.putNextEntry(entry);
    out.write(data.getBytes());
    out.close();
}

From source file:de.uni_koeln.spinfo.maalr.services.editor.server.EditorServiceImpl.java

public void export(Set<String> fields, MaalrQuery query, File dest) throws IOException, InvalidQueryException,
        NoIndexAvailableException, BrokenIndexException, InvalidTokenOffsetsException {
    query.setPageNr(0);// w  w w  . j a v  a 2s .  c  om
    query.setPageSize(50);
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(dest));
    zout.putNextEntry(new ZipEntry("exported.tsv"));
    OutputStream out = new BufferedOutputStream(zout);
    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    for (String field : fields) {
        writer.write(field);
        writer.write("\t");
    }
    writer.write("\n");
    while (true) {
        QueryResult result = index.query(query, false);
        if (result == null || result.getEntries() == null || result.getEntries().size() == 0)
            break;
        List<LemmaVersion> entries = result.getEntries();
        for (LemmaVersion version : entries) {
            write(writer, version, fields);
            writer.write("\n");
        }
        query.setPageNr(query.getPageNr() + 1);
    }
    writer.flush();
    zout.closeEntry();
    writer.close();

}

From source file:de.uni_koeln.spinfo.maalr.services.editor.server.EditorServiceImpl.java

public void export(Set<String> fields, EditorQuery query, File dest)
        throws NoDatabaseAvailableException, IOException {
    query.setCurrent(0);/*from   w w  w.ja  v a2s .  co m*/
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(dest));
    zout.putNextEntry(new ZipEntry("exported.tsv"));
    OutputStream out = new BufferedOutputStream(zout);
    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    for (String field : fields) {
        writer.write(field);
        writer.write("\t");
    }
    writer.write("\n");
    while (true) {
        LexEntryList result = Database.getInstance().queryForLexEntries(query.getUserOrIp(), query.getRole(),
                query.getVerification(), query.getVerifier(), query.getStartTime(), query.getEndTime(),
                query.getState(), query.getPageSize(), query.getCurrent(), query.getSortColumn(),
                query.isSortAscending());
        if (result == null || result.getEntries() == null || result.getEntries().size() == 0)
            break;
        for (LexEntry lexEntry : result.entries()) {
            addUserInfos(lexEntry);
            LemmaVersion version = lexEntry.getCurrent();
            write(writer, version, fields);
            writer.write("\n");
        }
        query.setCurrent(query.getCurrent() + query.getPageSize());
    }
    writer.flush();
    zout.closeEntry();
    writer.close();
}

From source file:com.taobao.android.utils.ZipUtils.java

/**
 * zip/*from w  ww  .jav  a  2  s  . c  o  m*/
 *
 * @param zipFile
 * @param file
 * @param destPath  ?
 * @param overwrite ?
 * @throws IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (ZipEntry.STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}

From source file:io.fabric8.support.impl.SupportServiceImpl.java

private void collectFromResource(ZipOutputStream zip, Resource resource) {
    ZipEntry entry = null;//from  w w w  . j a  v a2  s .c  o m
    try {
        entry = new ZipEntry(resource.getName());
        zip.putNextEntry(entry);
        resource.write(zip);
    } catch (Exception e) {
        LOGGER.warn("Unable to add support resource " + resource, e);
    } finally {
        safeClose(zip);
    }
}

From source file:com.wabacus.WabacusFacade.java

private static void tarFileToZip(String originFilePath, String zipFilePath) {
    if (Tools.isEmpty(originFilePath) || Tools.isEmpty(zipFilePath))
        return;/* ww  w.ja va2 s  .  c om*/
    int idx = originFilePath.lastIndexOf(File.separator);
    String fileName = idx > 0 ? originFilePath.substring(idx + File.separator.length()) : originFilePath;//???
    idx = fileName.lastIndexOf("_");
    if (idx > 0)
        fileName = fileName.substring(idx + 1).trim();
    try {
        FileOutputStream fout = new FileOutputStream(zipFilePath);
        ZipOutputStream zipout = new ZipOutputStream(fout);
        FileInputStream fis = new FileInputStream(originFilePath);
        zipout.putNextEntry(new ZipEntry(fileName));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            zipout.write(buffer, 0, len);
        }
        zipout.closeEntry();
        fis.close();
        zipout.close();
        fout.close();
    } catch (Exception e) {
        throw new WabacusRuntimeException("" + originFilePath + "zip", e);
    }
}