Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static void writeZipEntry(ZipOutputStream out, String name, byte[] data) throws IOException {
    final ZipEntry entry = new ZipEntry(StringUtils.removeStart(name, "/"));
    out.putNextEntry(entry);//from  ww  w . j a  v a2s  .  co  m
    out.write(data, 0, data.length);
    out.closeEntry();
}

From source file:it.geosolutions.mariss.wps.ppio.OutputResourcesPPIO.java

private static void addToZip(File f, ZipOutputStream zos) {

    FileInputStream in = null;//  w  ww  . j a v a 2  s  .co m
    try {
        in = new FileInputStream(f);
        zos.putNextEntry(new ZipEntry(f.getName()));
        IOUtils.copy(in, zos);
        zos.closeEntry();
    } catch (IOException e) {
        LOGGER.severe(e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
}

From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java

private static void addResourceFile(String name, String resource, ZipOutputStream out) throws IOException {
    ZipEntry zipEntry = new ZipEntry(name);
    out.putNextEntry(zipEntry);/*from w  ww  .jav  a2s . c  o  m*/
    OpenDocumentSpreadsheetCreator.addFromResource(resource, out);
    out.closeEntry();
}

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 += "/";//from   ww  w.j a  v a 2  s  .  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:com.wisemapping.util.ZipUtils.java

public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException {
    ZipOutputStream zip = null;
    final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    try {//from   w w  w  .  ja  va  2 s . c  o  m
        zip = new ZipOutputStream(byteArray);
        ZipEntry zEntry = new ZipEntry("content");
        zip.putNextEntry(zEntry);
        IOUtils.write(content, zip);
        zip.closeEntry();
    } finally {
        if (zip != null) {
            zip.flush();
            zip.close();
        }
    }

    return byteArray.toByteArray();
}

From source file:Main.java

public static void compressFile(File file, File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    FileInputStream fileInputStream = new FileInputStream(file);
    zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

    int size;//from w  ww.  j av a 2s.  com
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

    zipOutputStream.closeEntry();
    fileInputStream.close();
    zipOutputStream.close();
}

From source file:ZipHandler.java

/**
 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
 * @param zipURL//from  w w w  .  j  av a2  s.  c o m
 * @param xmlURL
 * @param xmlFileName
 */

public static void makeStructZip(String zipURL, String xmlURL, String xmlFileName) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(zipURL));
    ZipOutputStream zos = new ZipOutputStream(fos);
    //         zos.setLevel();
    FileInputStream fis = new FileInputStream(xmlURL);
    zos.putNextEntry(new ZipEntry(xmlFileName + ".xml"));
    writeInOutputStream(fis, zos);
    zos.closeEntry();
    fis.close();
    zos.close();
}

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);//  www  .  ja  v a2s  . c  o  m
        IOUtils.copy(is, zip);
        zip.closeEntry();
        is.close();
    }

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

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

}

From source file:Main.java

public static boolean compress(File file) {
    try {/*from   w  w w  .ja  v  a  2s  .c  om*/
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);
        out.closeEntry();

        in.close();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

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();
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++)
            fileToZip(files[i], zout, baseDir);
    } else {/*from   w  w w  .j  a va2 s  . co m*/
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            zout.putNextEntry(new ZipEntry(entryName));
            streamCopy(is, zout);
        } finally {
            zout.closeEntry();
            if (is != null)
                is.close();
        }
    }
}