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:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws Exception {

    for (File file : folder.listFiles()) {

        if (file.isDirectory()) {
            zos.putNextEntry(new ZipEntry(parentFolder + file.getName() + "/"));
            zos.closeEntry();
            addFolderToZip(file, parentFolder + file.getName() + "/", zos);
            continue;
        } else {/*from   w  w  w .  j  av  a2  s  .  c om*/
            zos.putNextEntry(new ZipEntry(parentFolder + file.getName()));
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

            long bytesRead = 0;
            byte[] bytesIn = new byte[1024];
            int read = 0;

            while ((read = bis.read(bytesIn)) != -1) {
                zos.write(bytesIn, 0, read);
                bytesRead += read;
            }

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

From source file:lucee.commons.io.compress.CompressUtil.java

private static void compressZip(String parent, Resource source, ZipOutputStream zos, ResourceFilter filter)
        throws IOException {
    if (source.isFile()) {
        //if(filter.accept(source)) {
        ZipEntry ze = new ZipEntry(parent);
        ze.setTime(source.lastModified());
        zos.putNextEntry(ze);/* w  ww .j  a  va  2 s. c  om*/
        try {
            IOUtil.copy(source, zos, false);
        } finally {
            zos.closeEntry();
        }
        //}
    } else if (source.isDirectory()) {
        if (!StringUtil.isEmpty(parent)) {
            ZipEntry ze = new ZipEntry(parent + "/");
            ze.setTime(source.lastModified());
            try {
                zos.putNextEntry(ze);
            } catch (IOException ioe) {
                if (Caster.toString(ioe.getMessage()).indexOf("duplicate") == -1)
                    throw ioe;
            }
            zos.closeEntry();
        }
        compressZip(parent, filter == null ? source.listResources() : source.listResources(filter), zos,
                filter);
    }
}

From source file:Main.java

private static boolean addEntryInputStream(ZipOutputStream zos, String entryName, InputStream inputStream) {
    final ZipEntry zipEntry = new ZipEntry(entryName);
    try {//from   w w w. ja v  a 2  s .  c  o  m
        zos.putNextEntry(zipEntry);
    } catch (final ZipException e) {

        // Ignore duplicate entry - no overwriting
        return false;
    } catch (final IOException e) {
        throw new RuntimeException("Error while adding zip entry " + zipEntry, e);
    }
    final int buffer = 2048;
    final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, buffer);
    int count;
    try {
        final byte data[] = new byte[buffer];
        while ((count = bufferedInputStream.read(data, 0, buffer)) != -1) {
            zos.write(data, 0, count);
        }
        bufferedInputStream.close();
        inputStream.close();
        zos.closeEntry();
        return true;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.coinsweb.sdk.FileManager.java

/**
 * Zips all the content of the [TEMP_ZIP_PATH] / [internalRef] to the specified target .ccr-file.
 *
 * @param target  the .ccr-file containing all the content from this container
 *//* w w  w  .j  a  v a  2s  .co m*/
public static void zip(String internalRef, File target) {

    try {

        final Path homePath = getTempZipPath().resolve(internalRef);

        FileOutputStream fos = new FileOutputStream(target);
        final ZipOutputStream zos = new ZipOutputStream(fos);

        ZipEntry ze;
        File[] files;

        // Add bim
        ze = new ZipEntry(RDF_PATH + "/");
        zos.putNextEntry(ze);
        zos.closeEntry();
        files = homePath.resolve(RDF_PATH).toFile().listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                addFileToZip(files[i].toString(), Paths.get(RDF_PATH).resolve(files[i].getName()), zos);
            }
        }

        // Add bim/repository
        ze = new ZipEntry(ONTOLOGIES_PATH + "/");
        zos.putNextEntry(ze);
        zos.closeEntry();
        files = homePath.resolve(ONTOLOGIES_PATH).toFile().listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                addFileToZip(files[i].toString(), Paths.get(ONTOLOGIES_PATH).resolve(files[i].getName()), zos);
            }
        }

        // Add doc
        ze = new ZipEntry(ATTACHMENT_PATH + "/");
        zos.putNextEntry(ze);
        zos.closeEntry();
        files = homePath.resolve(ATTACHMENT_PATH).toFile().listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                addFileToZip(files[i].toString(), Paths.get(ATTACHMENT_PATH).resolve(files[i].getName()), zos);
            }
        }

        // Add woa
        ze = new ZipEntry(WOA_PATH + "/");
        zos.putNextEntry(ze);
        zos.closeEntry();
        files = homePath.resolve(WOA_PATH).toFile().listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                addFileToZip(files[i].toString(), Paths.get(WOA_PATH).resolve(files[i].getName()), zos);
            }
        }

        zos.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static byte[] compressXMLFiles(XMLFileVO[] xmlFiles) throws TransformerException, IOException {
    byte[] compressedFiles = null;

    ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
    ZipOutputStream zipOS = new ZipOutputStream(byteOS);
    for (XMLFileVO xmlFile : xmlFiles) {
        ZipEntry entry = new ZipEntry(xmlFile.getName());
        zipOS.putNextEntry(entry);//from ww w.  ja v a  2s  .  co m
        byte[] xml = documentToByte(xmlFile.getXml());
        ByteArrayInputStream byteIS = new ByteArrayInputStream(xml);
        IOUtils.copy(byteIS, zipOS);
        byteIS.close();
        zipOS.closeEntry();
    }
    zipOS.close();

    compressedFiles = byteOS.toByteArray();

    return compressedFiles;
}

From source file:com.mosso.client.cloudfiles.sample.FilesCopy.java

/**
 *
 * @param folder//from  w w w  .  j a  v a2s.  c o  m
 * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped.
 * @throws IOException
 */
public static File zipFolder(File folder) throws IOException {
    byte[] buf = new byte[1024];
    int len;

    // Create the ZIP file
    String filenameWithZipExt = folder.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));

    if (folder.isDirectory()) {
        File[] files = folder.listFiles();

        for (File f : files) {
            if (!f.isDirectory()) {
                FileInputStream in = new FileInputStream(f);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(f.getName()));

                // Transfer bytes from the file to the ZIP file
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            } else
                logger.warn("Skipping nested folder: " + f.getAbsoluteFile());
        }

        out.flush();
        out.close();
    } else {
        logger.warn("The folder name supplied is not a folder!");
        System.err.println("The folder name supplied is not a folder!");
        return null;
    }

    return zippedFile;
}

From source file:ObjectLabEnterpriseSoftware.FileManager.java

public static void zipFile(File inputFile, String parentName, ZipOutputStream zipOutputStream)
        throws IOException {

    // A ZipEntry represents a file entry in the zip archive
    // We name the ZipEntry after the original file's name

    ZipEntry zipEntry = new ZipEntry(parentName + inputFile.getName());

    zipOutputStream.putNextEntry(zipEntry);

    FileInputStream fileInputStream = new FileInputStream(inputFile);

    byte[] buf = new byte[1024];

    int bytesRead;

    // Read the input file by chucks of 1024 bytes
    // and write the read bytes to the zip stream
    while ((bytesRead = fileInputStream.read(buf)) > 0) {

        zipOutputStream.write(buf, 0, bytesRead);
    }//from w w  w  .  j a v  a  2 s  .co m
    // close ZipEntry to store the stream to the file
    zipOutputStream.closeEntry();

    // System.out.println("Regular file :" + parentName+inputFile.getName() +" is zipped to archive :"+ZIPPED_FOLDER);
}

From source file:org.apache.jetspeed.portlets.site.PortalSiteManagerUtil.java

public static void zipFiles(File cpFile, String sourcePath, ZipOutputStream cpZipOutputStream) {
    if (cpFile.isDirectory()) {
        File[] fList = cpFile.listFiles();
        for (int i = 0; i < fList.length; i++) {
            zipFiles(fList[i], sourcePath, cpZipOutputStream);
        }//w ww . ja  v a  2 s.c o  m
    } else {
        FileInputStream cpFileInputStream = null;

        try {
            String strAbsPath = cpFile.getAbsolutePath();
            String strZipEntryName = strAbsPath.substring(sourcePath.length() + 1, strAbsPath.length());
            cpFileInputStream = new FileInputStream(cpFile);
            ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
            cpZipOutputStream.putNextEntry(cpZipEntry);
            IOUtils.copy(cpFileInputStream, cpZipOutputStream);
            cpZipOutputStream.closeEntry();
        } catch (Exception e) {
            logger.error("Unexpected exception during zipping files.", e);
        } finally {
            if (cpFileInputStream != null) {
                try {
                    cpFileInputStream.close();
                } catch (Exception ce) {
                }
            }
        }
    }
}

From source file:com.edgenius.core.util.ZipFileUtil.java

private static void addEntry(ZipOutputStream zop, File entry, String entryName) throws IOException {
    if (StringUtils.isBlank(entryName))
        return;/*from  ww w  .  j a  v a  2s.c o  m*/

    BufferedInputStream source = null;
    if (entry != null) {
        source = new BufferedInputStream(new FileInputStream(entry));
    } else {
        //to make this as directory
        if (!entryName.endsWith("/"))
            entryName += "/";
    }
    ZipEntry zipEntry = new ZipEntry(entryName);
    zop.putNextEntry(zipEntry);

    if (entry != null) {
        //transfer bytes from file to ZIP file
        byte[] data = new byte[BUFFER_SIZE];
        int length;
        while ((length = source.read(data)) > 0) {
            zop.write(data, 0, length);
        }
        IOUtils.closeQuietly(source);
    }
    zop.closeEntry();

}

From source file:io.druid.java.util.common.CompressionUtils.java

/**
 * Zips the contents of the input directory to the output stream. Sub directories are skipped
 *
 * @param directory The directory whose contents should be added to the zip in the output stream.
 * @param out       The output stream to write the zip data to. Caller is responsible for closing this stream.
 *
 * @return The number of bytes (uncompressed) read from the input directory.
 *
 * @throws IOException//w w  w.  j  ava  2 s.  c om
 */
public static long zip(File directory, OutputStream out) throws IOException {
    if (!directory.isDirectory()) {
        throw new IOE("directory[%s] is not a directory", directory);
    }

    final ZipOutputStream zipOut = new ZipOutputStream(out);

    long totalSize = 0;
    for (File file : directory.listFiles()) {
        log.info("Adding file[%s] with size[%,d].  Total size so far[%,d]", file, file.length(), totalSize);
        if (file.length() >= Integer.MAX_VALUE) {
            zipOut.finish();
            throw new IOE("file[%s] too large [%,d]", file, file.length());
        }
        zipOut.putNextEntry(new ZipEntry(file.getName()));
        totalSize += Files.asByteSource(file).copyTo(zipOut);
    }
    zipOut.closeEntry();
    // Workaround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
    zipOut.flush();
    zipOut.finish();

    return totalSize;
}