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:org.eclipse.vorto.maven.GeneratorMojo.java

private static void addToZip(ZipOutputStream zaos, Path path) {
    try {/*from   w  w w  . ja  v a2 s  .  co m*/
        ZipEntry zipEntry = new ZipEntry(path.toFile().getName());
        zaos.putNextEntry(zipEntry);
        zaos.write(IOUtils.toByteArray(new FileInputStream(path.toFile())));
        zaos.closeEntry();
    } catch (Exception ex) {
        // logging ?
    }
}

From source file:com.ikanow.aleph2.analytics.storm.utils.TestStormControllerUtil_Cache.java

private static File createFakeZipFile(String file_name) throws IOException {
    File file;//  w w  w .  j av a  2  s.  c  o m
    if (file_name == null)
        file = File.createTempFile("recent_date_test_", ".zip");
    else
        file = new File(file_name);
    Random r = new Random();
    ZipOutputStream outputZip = new ZipOutputStream(new FileOutputStream(file));
    ZipEntry e = new ZipEntry("some_file.tmp");
    outputZip.putNextEntry(e);
    outputZip.write(r.nextInt());
    outputZip.close();
    return file;
}

From source file:com.mcleodmoores.mvn.natives.UnpackDependenciesMojoTest.java

private static Artifact createArtifact(final File tmp, final String type, final String member)
        throws IOException {
    final File zipFile = new File(tmp, type + ".zip");
    try (final FileOutputStream out = new FileOutputStream(zipFile)) {
        final ZipOutputStream zipStream = new ZipOutputStream(out);
        final ZipEntry license = new ZipEntry("LICENSE");
        zipStream.putNextEntry(license);
        zipStream.write(26);
        zipStream.closeEntry();/*from w ww  .  j  a v  a 2s.  co m*/
        final ZipEntry payload = new ZipEntry(member);
        zipStream.putNextEntry(payload);
        zipStream.write(26);
        zipStream.closeEntry();
        zipStream.close();
    }
    final Artifact artifact = Mockito.mock(Artifact.class);
    Mockito.when(artifact.getType()).thenReturn(type);
    Mockito.when(artifact.getGroupId()).thenReturn("uk.co.beerdragon");
    Mockito.when(artifact.getArtifactId()).thenReturn("test-" + type);
    Mockito.when(artifact.getVersion()).thenReturn("SNAPSHOT");
    Mockito.when(artifact.getFile()).thenReturn(zipFile);
    return artifact;
}

From source file:com.baasbox.util.Util.java

public static void createZipFile(String path, File... files) {
    if (BaasBoxLogger.isDebugEnabled())
        BaasBoxLogger.debug("Zipping into:" + path);
    ZipOutputStream zip = null;
    FileOutputStream dest = null;
    try {//w  ww.  j a  v a2  s  . c o  m
        File f = new File(path);
        dest = new FileOutputStream(f);
        zip = new ZipOutputStream(new BufferedOutputStream(dest));
        for (File file : files) {
            zip.putNextEntry(new ZipEntry(file.getName()));
            zip.write(FileUtils.readFileToByteArray(file));
            zip.closeEntry();
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to create zip file");
    } finally {
        try {
            if (zip != null)
                zip.close();
            if (dest != null)
                dest.close();

        } catch (Exception ioe) {
            //Nothing to do
        }
    }
}

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

private static Object _serializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField,
        Session session) {/* w  ww . j  a  v  a  2s .co m*/
    if (targetField == null) {
        return value;
    }
    if (!targetField.isMany()) {
        Boolean targetZipped = targetField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        Boolean sourceZipped = sourceField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        if (sourceZipped == null && Boolean.TRUE.equals(targetZipped)) {
            try {
                ByteArrayOutputStream characters = new ByteArrayOutputStream();
                OutputStream bos = new Base64OutputStream(characters);
                ZipOutputStream zos = new ZipOutputStream(bos);
                ZipEntry zipEntry = new ZipEntry("content"); //$NON-NLS-1$
                zos.putNextEntry(zipEntry);
                zos.write(String.valueOf(value).getBytes("UTF-8")); //$NON-NLS-1$
                zos.closeEntry();
                zos.flush();
                zos.close();
                return new String(characters.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException("Unexpected compression exception", e); //$NON-NLS-1$
            }
        }
        String targetSQLType = targetField.getType().getData(TypeMapping.SQL_TYPE);
        if (targetSQLType != null && SQL_TYPE_CLOB.equals(targetSQLType)) {
            if (value != null) {
                return Hibernate.getLobCreator(session).createClob(String.valueOf(value));
            } else {
                return null;
            }
        }
    }
    return value;
}

From source file:com.algomedica.service.LicenseManagerServiceImpl.java

public static byte[] zipBytes(String filename, byte[] input) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(input.length);//from w  w w .  j  a  v a 2  s . c  o m
    zos.putNextEntry(entry);
    zos.write(input);
    zos.closeEntry();
    zos.close();
    return baos.toByteArray();
}

From source file:com.webautomation.ScreenCaptureHtmlUnitDriver.java

public static byte[] createZip(Map<String, byte[]> files) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipfile = new ZipOutputStream(bos);
    Iterator<String> i = files.keySet().iterator();
    String fileName = null;// w  w w  .ja v a2 s  . co  m
    ZipEntry zipentry = null;
    while (i.hasNext()) {
        fileName = i.next();
        zipentry = new ZipEntry(fileName);
        zipfile.putNextEntry(zipentry);
        zipfile.write(files.get(fileName));
    }
    zipfile.close();
    return bos.toByteArray();
}

From source file:com.eviware.soapui.testondemand.TestOnDemandCaller.java

private static byte[] zipBytes(String filename, byte[] dataToBeZiped) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zipedOutputStream = new ZipOutputStream(outputStream);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(dataToBeZiped.length);
    try {//from   ww w  .  j  a  v a2s .  co  m
        zipedOutputStream.putNextEntry(entry);
        zipedOutputStream.write(dataToBeZiped);
    } finally {
        zipedOutputStream.closeEntry();
        zipedOutputStream.close();
    }
    return outputStream.toByteArray();
}

From source file:de.tor.tribes.util.AttackToTextWriter.java

private static boolean writeBlocksToZip(List<String> pBlocks, Tribe pTribe, File pPath) {
    int fileNo = 1;
    String baseFilename = pTribe.getName().replaceAll("\\W+", "");
    ZipOutputStream zout = null;
    try {/*from  w  w  w .j a  v  a2 s . c om*/
        zout = new ZipOutputStream(
                new FileOutputStream(FilenameUtils.concat(pPath.getPath(), baseFilename + ".zip")));
        for (String block : pBlocks) {
            String entryName = baseFilename + fileNo + ".txt";
            ZipEntry entry = new ZipEntry(entryName);
            try {
                zout.putNextEntry(entry);
                zout.write(block.getBytes());
                zout.closeEntry();
            } catch (IOException ioe) {
                logger.error("Failed to write attack to zipfile", ioe);
                return false;
            }
            fileNo++;
        }
    } catch (IOException ioe) {
        logger.error("Failed to write content to zip file", ioe);
        return false;
    } finally {
        if (zout != null) {
            try {
                zout.flush();
                zout.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}

From source file:de.decidr.model.workflowmodel.deployment.PackageBuilder.java

/**
 * @param workFlowId//from w ww. j av a  2 s .  co  m
 * @param bpel
 * @param wsdl
 * @param dd
 *            depoyment descriptor
 * @param serverLocation
 *            location for service endpoints
 * @return the zip'ed package as byte array
 * @throws JAXBException
 * @throws IOException
 */
public static byte[] getPackage(long workFlowId, TProcess bpel, TDefinitions wsdl, TDeployment dd,
        HashMap<String, List<ServerLoadView>> serverMap) throws JAXBException, IOException {

    ByteArrayOutputStream zipOut = new ByteArrayOutputStream();

    TransformationHelper th = new TransformationHelper();

    de.decidr.model.schema.bpel.executable.ObjectFactory of = new de.decidr.model.schema.bpel.executable.ObjectFactory();
    de.decidr.model.schema.wsdl.ObjectFactory ofwasl = new de.decidr.model.schema.wsdl.ObjectFactory();
    de.decidr.model.schema.bpel.dd.ObjectFactory ofdd = new de.decidr.model.schema.bpel.dd.ObjectFactory();

    JAXBElement<TDefinitions> def = ofwasl.createDefinitions(wsdl);
    JAXBElement<TDeployment> d = ofdd.createDeploy(dd);

    String bpelFilename = "BPELPROCESS_" + workFlowId + ".bpel";
    String wsdlFilename = TransformConstants.FILENAME_WSDL_PROCESS_NAME_TEMPLATE.replace("?", workFlowId + "");
    String ddFilename = "deploy.xml";

    ZipOutputStream zip_out_stream = new ZipOutputStream(zipOut);

    // Add BPEL to zip
    zip_out_stream.putNextEntry(new ZipEntry(bpelFilename));
    String fileBPEL = th.jaxbObjectToStringWithWorkflowNamespace(of.createProcess(bpel),
            bpel.getTargetNamespace(), TransformationHelper.BPEL_CLASSES);
    zip_out_stream.write(fileBPEL.getBytes());
    zip_out_stream.closeEntry();

    // Add WSDL to zip
    zip_out_stream.putNextEntry(new ZipEntry(wsdlFilename));
    String fileWSDL = th.jaxbObjectToStringWithWorkflowNamespace(def, bpel.getTargetNamespace(),
            TransformationHelper.WSDL_CLASSES);
    zip_out_stream.write(fileWSDL.getBytes());
    zip_out_stream.closeEntry();

    // Add Deployment Descriptor to zip
    zip_out_stream.putNextEntry(new ZipEntry(ddFilename));
    String fileDD = th.jaxbObjectToStringWithWorkflowNamespace(d, bpel.getTargetNamespace(),
            TransformationHelper.DD_CLASSES);
    zip_out_stream.write(fileDD.getBytes());
    zip_out_stream.closeEntry();

    // Add the EMail WSDL to package and modify the service endpoint
    zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_EMAIL));
    zip_out_stream.write(getEmailWsdl(
            URLGenerator.getEmailWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation())));
    zip_out_stream.closeEntry();

    // Add the HT WSDL to package and modify the service endpoint
    zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_HUMANTASK));
    zip_out_stream.write(getHTWsdl(
            URLGenerator.getHumanTaskWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation())));
    zip_out_stream.closeEntry();

    // Add the PS WSDL to package and modify the service endpoint
    zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_PROXY));
    zip_out_stream.write(getPSWsdl(URLGenerator
            .getProxyServiceWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation())));
    zip_out_stream.closeEntry();

    // Add DecidrTypes schema to package
    zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_XSD_DECIDR_TYPES));
    zip_out_stream.write(loadSchema(TransformConstants.FILENAME_XSD_DECIDR_TYPES));
    zip_out_stream.closeEntry();

    zip_out_stream.close();
    return zipOut.toByteArray();

}