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:FolderZiper.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {//from   ww w  . j av  a2s  .c  om
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

From source file:com.dv.util.DataViewerZipUtil.java

/**
 * ZipOutputStream/*from   w w  w . j  a va 2s. co  m*/
 * @param srcFile 
 * @param zipOut ZipOutputStream?
 * @param ns ZIP
 * @throws IOException
 */
private static void doZipFile(File srcFile, ZipOutputStream zipOut, String ns) throws IOException {
    if (srcFile.isFile()) {
        zipOut.putNextEntry(new ZipEntry(ns + srcFile.getName()));
        InputStream is = FileUtils.openInputStream(srcFile);
        try {
            IOUtils.copy(is, zipOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
        return;
    }
    for (File file : srcFile.listFiles()) {
        String entryName = ns + file.getName();

        if (file.isDirectory()) {
            entryName += File.separator;
            zipOut.putNextEntry(new ZipEntry(entryName));
        }
        doZipFile(file, zipOut, entryName);
    }
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

private static void zipDirectory(File directory, String name, ZipOutputStream zos) throws IOException {
    // *MUST* append the trailing slash for a ZipEntry to identify an
    // entry as a directory.
    name += "/";/*w  w w  . j a va 2s  . com*/

    zos.putNextEntry(new ZipEntry(name));
    zos.closeEntry();

    String[] entryList = directory.list();

    for (int i = 0; i < entryList.length; ++i) {
        File f = new File(directory, entryList[i]);

        if (f.isDirectory()) {
            zipDirectory(f, name + f.getName(), zos);
        } else {
            FileInputStream fis = new FileInputStream(f);
            ZipEntry entry = new ZipEntry(name + f.getName());
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesIn = 0;

            zos.putNextEntry(entry);

            while ((bytesIn = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesIn);
            }

            fis.close();
            zos.closeEntry();
        }
    }
}

From source file:es.caib.sgtsic.util.Zips.java

public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(baos);

    for (String key : documents.keySet()) {

        InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key)));
        ZipEntry zipEntry = new ZipEntry(key);
        zip.putNextEntry(zipEntry);
        IOUtils.copy(is, zip);/*w ww .  j av  a 2 s .  c o m*/
        zip.closeEntry();
        is.close();
    }

    zip.close();
    baos.close();

    return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip");

}

From source file:com.microsoft.azurebatch.jenkins.utils.ZipHelper.java

private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean isEmptyFolder)
        throws IOException {
    File folder = new File(srcFile);

    if (isEmptyFolder == true) {
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
    } else {/*  w  w w  . ja v  a 2s  . c  o  m*/
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            byte[] buf = new byte[1024];
            int len;
            try (FileInputStream in = new FileInputStream(srcFile)) {
                zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            }
        }
    }
}

From source file:Main.java

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();//w w  w.  j  a  va  2 s. c  o  m
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:com.youTransactor.uCube.LogManager.java

public static void getLogs(OutputStream out) throws IOException {
    File logDir = context.getDir(LOG_DIR, Context.MODE_PRIVATE);

    if (logDir.listFiles().length > 0) {
        ZipOutputStream zout = new ZipOutputStream(out);

        for (File file : logDir.listFiles()) {
            ZipEntry entry = new ZipEntry(file.getName());
            zout.putNextEntry(entry);
            IOUtils.copy(new FileInputStream(file), zout);
            zout.closeEntry();/* w  w w  .  j  av  a  2 s .  co m*/
        }

        zout.close();
    }
}

From source file:ZipHelper.java

private static void fileToZip(File file, ZipOutputStream zout, File baseDir) throws Exception {
    String entryName = file.getPath().substring(baseDir.getPath().length() + 1);
    if (File.separatorChar != '/')
        entryName = entryName.replace(File.separator, "/");
    if (file.isDirectory()) {
        zout.putNextEntry(new ZipEntry(entryName + "/"));
        zout.closeEntry();//  w ww  .  ja  v  a 2 s.c  o m
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++)
            fileToZip(files[i], zout, baseDir);
    } else {
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            zout.putNextEntry(new ZipEntry(entryName));
            streamCopy(is, zout);
        } finally {
            zout.closeEntry();
            if (is != null)
                is.close();
        }
    }
}

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

/**
 * @param workFlowId//from  w w w . ja  va2 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.ikanow.aleph2.analytics.storm.utils.TestStormControllerUtil_Cache.java

private static File createFakeZipFile(String file_name) throws IOException {
    File file;/*from www  . j  a va 2 s.co  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;
}