Example usage for java.util.zip ZipOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

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  v  a  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();
}

From source file:eu.delving.services.controller.DataSetController.java

private void writeSipZip(String dataSetSpec, OutputStream outputStream, String accessKey) throws IOException,
        MappingNotFoundException, AccessKeyException, XMLStreamException, MetadataException, MappingException {
    MetaRepo.DataSet dataSet = metaRepo.getDataSet(dataSetSpec);
    if (dataSet == null) {
        throw new IOException("Data Set not found"); // IOException?
    }/*from w ww. j a v  a  2  s . c  om*/
    ZipOutputStream zos = new ZipOutputStream(outputStream);
    zos.putNextEntry(new ZipEntry(FileStore.FACTS_FILE_NAME));
    Facts facts = Facts.fromBytes(dataSet.getDetails().getFacts());
    facts.setDownloadedSource(true);
    zos.write(Facts.toBytes(facts));
    zos.closeEntry();
    zos.putNextEntry(new ZipEntry(FileStore.SOURCE_FILE_NAME));
    String sourceHash = writeSourceStream(dataSet, zos, accessKey);
    zos.closeEntry();
    for (MetaRepo.Mapping mapping : dataSet.mappings().values()) {
        RecordMapping recordMapping = mapping.getRecordMapping();
        zos.putNextEntry(
                new ZipEntry(String.format(FileStore.MAPPING_FILE_PATTERN, recordMapping.getPrefix())));
        RecordMapping.write(recordMapping, zos);
        zos.closeEntry();
    }
    zos.finish();
    zos.close();
    dataSet.setSourceHash(sourceHash, true);
    dataSet.save();
}

From source file:be.neutrinet.ispng.vpn.api.VPNClientConfig.java

protected Representation finalizeZip(List<String> config, ZipOutputStream zip, ByteArrayOutputStream baos)
        throws Exception {
    String file = "";
    for (String s : config) {
        file += s + '\n';
    }//from  w w  w . j  a  v  a 2  s .co  m

    if (caCert == null) {
        caCert = IOUtils.toByteArray(new FileInputStream(VPN.cfg.getProperty("ca.crt")));
    }

    ZipEntry configFile = new ZipEntry("neutrinet.ovpn");
    configFile.setCreationTime(FileTime.from(Instant.now()));
    zip.putNextEntry(configFile);
    zip.write(file.getBytes());
    zip.putNextEntry(new ZipEntry("ca.crt"));
    zip.write(caCert);

    zip.close();

    ByteArrayRepresentation rep = new ByteArrayRepresentation(baos.toByteArray());
    rep.setMediaType(MediaType.APPLICATION_ZIP);
    rep.setSize(baos.size());
    rep.setCharacterSet(CharacterSet.UTF_8);
    rep.setDisposition(new Disposition(Disposition.TYPE_ATTACHMENT));
    return rep;
}

From source file:ZipUtilTest.java

public void testUnpackEntryFromFile() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {/*from  w  w w  .  j a  v a 2  s.  co m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file, name);
        assertNotNull(actual);
        assertEquals(new String(contents), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:ZipUtilTest.java

public void testUnpackEntryFromStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {//w  w w .j  a v  a  2  s .com
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        FileInputStream fis = new FileInputStream(file);
        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(fis, name);
        assertNotNull(actual);
        assertEquals(new String(contents), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:org.eclipse.vorto.repository.web.ModelRepositoryController.java

private void addModelToZip(ZipOutputStream zipOutputStream, ModelId modelId, ContentType contentType)
        throws Exception {
    byte[] modelContent = modelRepository.getModelContent(modelId, contentType);
    ModelResource modelResource = modelRepository.getById(modelId);

    try {//from  ww w.  j  a  v  a2s  .  c o m
        ZipEntry zipEntry = new ZipEntry(getFileName(modelResource, contentType));
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(modelContent);
        zipOutputStream.closeEntry();
    } catch (Exception ex) {
        // entry possible exists already, so skipping TODO: ugly hack!!
    }

    for (ModelId reference : modelResource.getReferences()) {
        addModelToZip(zipOutputStream, reference, contentType);
    }
}

From source file:com.ibm.liberty.starter.ProjectZipConstructor.java

public void createZipFromMap(ZipOutputStream zos) throws IOException {
    System.out.println("Entering method ProjectZipConstructor.createZipFromMap()");
    Enumeration<String> en = fileMap.keys();
    while (en.hasMoreElements()) {
        String path = en.nextElement();
        byte[] byteArray = fileMap.get(path);
        ZipEntry entry = new ZipEntry(path);
        entry.setSize(byteArray.length);
        entry.setCompressedSize(-1);/*from   ww w  .ja va2s  .c  o m*/
        try {
            zos.putNextEntry(entry);
            zos.write(byteArray);
        } catch (IOException e) {
            throw new IOException(e);
        }
    }
}

From source file:ZipUtilTest.java

public void testUnpackEntryFromStreamToFile() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file = File.createTempFile("temp", null);
    try {/*w w w .j a  va 2  s  . c  o  m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        FileInputStream fis = new FileInputStream(file);

        File outputFile = File.createTempFile("temp-output", null);

        boolean result = ZipUtil.unpackEntry(fis, name, outputFile);
        assertTrue(result);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outputFile));
        byte[] actual = new byte[1024];
        int read = bis.read(actual);
        bis.close();

        assertEquals(new String(contents), new String(actual, 0, read));
    } finally {
        FileUtils.deleteQuietly(file);
    }
}

From source file:dk.itst.oiosaml.sp.configuration.ConfigurationHandler.java

protected File generateZipFile(final String contextPath, final String password, byte[] idpMetadata,
        byte[] keystore, EntityDescriptor descriptor) throws IOException {
    File zipFile = File.createTempFile("oiosaml-", ".zip");
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
    zos.putNextEntry(new ZipEntry("oiosaml-sp.properties"));
    zos.write(renderTemplate("defaultproperties.vm", new HashMap<String, Object>() {
        {/*from   w  ww . j  a v a 2  s .  co  m*/
            put("homename", Constants.PROP_HOME);

            put("servletPath", contextPath);
            put("password", password);
        }
    }, false).getBytes());
    zos.closeEntry();

    zos.putNextEntry(new ZipEntry("metadata/SP/SPMetadata.xml"));
    zos.write(SAMLUtil.getSAMLObjectAsPrettyPrintXML(descriptor).getBytes());
    zos.closeEntry();

    zos.putNextEntry(new ZipEntry("metadata/IdP/IdPMetadata.xml"));
    zos.write(idpMetadata);
    zos.closeEntry();

    zos.putNextEntry(new ZipEntry("certificate/keystore"));
    zos.write(keystore);
    zos.closeEntry();

    zos.putNextEntry(new ZipEntry("oiosaml-sp.log4j.xml"));
    IOUtils.copy(getClass().getResourceAsStream("oiosaml-sp.log4j.xml"), zos);
    zos.closeEntry();

    zos.close();
    return zipFile;
}

From source file:it.eng.spagobi.tools.scheduler.dispatcher.MailDocumentDispatchChannel.java

private MimeBodyPart zipAttachment(byte[] attach, String containedFileName, String zipFileName,
        String nameSuffix, String fileExtension) {
    MimeBodyPart messageBodyPart = null;
    try {//  w  w  w  .  j a v a 2  s  .c  o  m

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(bout);
        String entryName = containedFileName + nameSuffix + fileExtension;
        zipOut.putNextEntry(new ZipEntry(entryName));
        zipOut.write(attach);
        zipOut.closeEntry();

        zipOut.close();

        messageBodyPart = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(bout.toByteArray(), "application/zip");
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(zipFileName + nameSuffix + ".zip");

    } catch (Exception e) {
        // TODO: handle exception            
    }
    return messageBodyPart;
}