Example usage for java.util.zip ZipEntry ZipEntry

List of usage examples for java.util.zip ZipEntry ZipEntry

Introduction

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

Prototype

public ZipEntry(ZipEntry e) 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

From source file:com.openkm.misc.ZipTest.java

public void testJava() throws IOException {
    log.debug("testJava()");
    File zip = File.createTempFile("java_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zos.putNextEntry(new ZipEntry("coeta"));
    zos.closeEntry();//w  w  w .j  av  a2 s .  co m
    zos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    System.out.println(ze.getName());
    assertEquals(ze.getName(), "coeta");
    zis.close();
}

From source file:eu.scidipes.toolkits.pawebapp.util.zip.TestZipUtils.java

/**
 * Test method for//from w  ww  .  j  av a2s. c om
 * {@link eu.scidipes.toolkits.palibrary.utils.zip.ZipUtils#byteArrayZipEntriesToBase64(java.util.Set)}.
 * 
 * @throws IOException
 */
@Test
public final void testByteArrayZipEntriesToBase64() throws IOException {
    final String mainFileName = "SCIDIP-ES-Registry-Framework-Overview.pdf";
    final String metaFileName = "meta.xml";

    final InputStream mainFileStream = getClass().getResourceAsStream(mainFileName);
    final InputStream metaFileStream = getClass().getResourceAsStream(metaFileName);

    final byte[] mainFileBytes = IOUtils.toByteArray(mainFileStream);
    final byte[] metaFileBytes = IOUtils.toByteArray(metaFileStream);

    final ZipEntry zipMain = new ZipEntry(mainFileName);
    final ZipEntry zipMeta = new ZipEntry(metaFileName);

    final Set<ByteArrayZipEntry> entries = new HashSet<>();
    entries.add(new ByteArrayZipEntry(mainFileBytes, zipMain));
    entries.add(new ByteArrayZipEntry(metaFileBytes, zipMeta));

    final String zipResult = ZipUtils.byteArrayZipEntriesToBase64(entries);
    assertTrue(zipResult != null && zipResult.length() > 100);
}

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

/**
 * @param workFlowId//from w ww.  j a va 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();

}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

public String toBase64() {
    try {// w w  w .jav a2  s  .  com
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ZipOutputStream zip = new ZipOutputStream(bos);
            try {
                zip.setLevel(9);
                zip.putNextEntry(new ZipEntry("entry"));
                try {
                    ObjectOutput out = new ObjectOutputStream(zip);
                    try {
                        out.writeObject(this);
                    } finally {
                        out.flush();
                    }
                } finally {
                    zip.closeEntry();
                }
                zip.flush();
                return Base64.encodeBase64URLSafeString(bos.toByteArray());
            } finally {
                zip.close();
            }
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        log.log((Level.SEVERE), "IO Error while serializing member", e);
        return null;
    }
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

private static int putEntries(ZipOutputStream zos, String sDir, String sRelativeDir,
        final List<File> excludedFiles) throws Exception {
    Engine.logEngine.trace("==========================================================");
    Engine.logEngine.trace("sDir=" + sDir);
    Engine.logEngine.trace("sRelativeDir=" + sRelativeDir);
    Engine.logEngine.trace("excludedFiles=" + excludedFiles);

    File dir = new File(sDir);
    String[] files = dir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            File file = new File(dir, name);
            return (!excludedFiles.contains(file));
        }//from   w  w  w  .jav a2s .c  om
    });

    Engine.logEngine.trace("files=" + files);

    int nbe = 0;
    for (String file : files) {
        String sDirEntry = sDir + "/" + file;
        String sRelativeDirEntry = sRelativeDir != null ? (sRelativeDir + "/" + file) : file;

        File f = new File(sDirEntry);
        if (!f.isDirectory()) {
            Engine.logEngine.trace("+ " + sDirEntry);
            InputStream fi = new FileInputStream(f);

            try {
                ZipEntry entry = new ZipEntry(sRelativeDirEntry);
                entry.setTime(f.lastModified());
                zos.putNextEntry(entry);
                IOUtils.copy(fi, zos);
                nbe++;
            } finally {
                fi.close();
            }
        } else {
            nbe += putEntries(zos, sDirEntry, sRelativeDirEntry, excludedFiles);
        }
    }
    return nbe;
}

From source file:be.fedict.eid.applet.service.signer.asic.ASiCSignatureOutputStream.java

@Override
public void close() throws IOException {
    super.close();

    byte[] signatureData = toByteArray();

    /*//from  ww  w . j av  a2  s.c  om
     * Copy the original ZIP content.
     */
    ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile));
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (!zipEntry.getName().equals(ASiCUtil.SIGNATURE_FILE)) {
            ZipEntry newZipEntry = new ZipEntry(zipEntry.getName());
            zipOutputStream.putNextEntry(newZipEntry);
            LOG.debug("copying " + zipEntry.getName());
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }
    zipInputStream.close();

    /*
     * Add the XML signature file to the ASiC package.
     */
    zipEntry = new ZipEntry(ASiCUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

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);//ww w .  j av  a2s  .  co m
        zipStream.closeEntry();
        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:fll.web.GatherBugReport.java

protected void processRequest(final HttpServletRequest request, final HttpServletResponse response,
        final ServletContext application, final HttpSession session) throws IOException, ServletException {
    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    ZipOutputStream zipOut = null;
    final StringBuilder message = new StringBuilder();
    try {//  w  w w . j  av  a  2s.  c  o m
        if (null != SessionAttributes.getMessage(session)) {
            message.append(SessionAttributes.getMessage(session));
        }

        connection = datasource.getConnection();
        final Document challengeDocument = ApplicationAttributes.getChallengeDocument(application);

        final File fllWebInfDir = new File(application.getRealPath("/WEB-INF"));
        final String nowStr = new SimpleDateFormat("yyyy-MM-dd_HH-mm").format(new Date());
        final File bugReportFile = File.createTempFile(nowStr, ".zip", fllWebInfDir);

        zipOut = new ZipOutputStream(new FileOutputStream(bugReportFile));

        final String description = request.getParameter("bug_description");
        if (null != description) {
            zipOut.putNextEntry(new ZipEntry("bug_description.txt"));
            zipOut.write(description.getBytes(Utilities.DEFAULT_CHARSET));
        }

        zipOut.putNextEntry(new ZipEntry("server_info.txt"));
        zipOut.write(String.format(
                "Java version: %s%nJava vendor: %s%nOS Name: %s%nOS Arch: %s%nOS Version: %s%nServlet API: %d.%d%nServlet container: %s%n",
                System.getProperty("java.vendor"), //
                System.getProperty("java.version"), //
                System.getProperty("os.name"), //
                System.getProperty("os.arch"), //
                System.getProperty("os.version"), //
                application.getMajorVersion(), application.getMinorVersion(), //
                application.getServerInfo()).getBytes(Utilities.DEFAULT_CHARSET));

        addDatabase(zipOut, connection, challengeDocument);
        addLogFiles(zipOut, application);

        message.append(String.format(
                "<i>Bug report saved to '%s', please notify the computer person in charge to look for bug report files.</i>",
                bugReportFile.getAbsolutePath()));
        session.setAttribute(SessionAttributes.MESSAGE, message);

        response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/"));

    } catch (final SQLException sqle) {
        throw new RuntimeException(sqle);
    } finally {
        SQLFunctions.close(connection);
        IOUtils.closeQuietly(zipOut);
    }

}

From source file:com.healthcit.analytics.servlet.DataExportServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String token = request.getParameter("token");
    TOKENS_MAP.put(token, DownloadStatus.STARTED);
    String keys = request.getParameter("keys");
    if (keys != null) {
        //JSONArray ownerIdsJSON = (JSONArray)JSONSerializer.toJSON(ownerIds ) ;
        String[] ownerIdsArray = StringUtils.split(keys, ",");

        response.setContentType(Constants.JSON_CONTENT_TYPE);
        response.addHeader(Constants.CONTENT_DISPOSITION, "attachment; filename=dataExport.zip");
        response.addHeader(Constants.PRAGMA_HEADER, Constants.NO_CACHE);

        response.setHeader(Constants.CACHE_CONTROL_HEADER, Constants.NO_CACHE);
        ServletOutputStream os = response.getOutputStream();
        ZipOutputStream zos = new ZipOutputStream(os);
        //         ZipEntry entry = new ZipEntry("dataExport."+OutputFormat.JSON.toString().toLowerCase());
        ZipEntry entry = new ZipEntry("dataExport." + OutputFormat.XML.toString().toLowerCase());
        zos.putNextEntry(entry);/* ww  w  .j  av  a 2 s  . co m*/
        //         OwnerDataManager ownerManager = new OwnerDataManager(OutputFormat.JSON);
        OwnerDataManager ownerManager = new OwnerDataManager(OutputFormat.XML);
        try {
            ownerManager.getEntitiesData(ownerIdsArray, zos);
            TOKENS_MAP.put(token, DownloadStatus.FINISHED);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            TOKENS_MAP.put(token, DownloadStatus.ERROR);
        } finally {
            zos.close();
            //            os.flush();

            //            os.close();
        }
    } else {
        TOKENS_MAP.put(token, DownloadStatus.ERROR);
    }
}