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.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 {/*from   w w w .j av a 2  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.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);//  w  w  w. ja  v  a  2 s  .c  o  m
    zos.putNextEntry(entry);
    zos.write(input);
    zos.closeEntry();
    zos.close();
    return baos.toByteArray();
}

From source file:ai.serotonin.backup.Backup.java

private static void addZipEntry(final ZipOutputStream zip, final String prefix, final String path)
        throws IOException {
    final ZipEntry e = new ZipEntry(path);
    zip.putNextEntry(e);/*from w w w .  j  av a 2  s  .com*/

    try (FileInputStream in = new FileInputStream(new File(prefix, path))) {
        final long len = IOUtils.copy(in, zip);
        if (LOG.isDebugEnabled())
            LOG.debug("Wrote " + path + ", " + len + " bytes ");
    }

    zip.closeEntry();
}

From source file:com.splout.db.common.CompressorUtil.java

public static void createZip(File dir, File out, IOFileFilter filefilter, IOFileFilter dirFilter)
        throws IOException {
    Collection<File> files = FileUtils.listFiles(dir, filefilter, dirFilter);

    out.delete();/*from  www.j  av  a 2  s.c  om*/
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(getRelativePath(f, dir));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:org.craftercms.commons.zip.ZipUtils.java

/**
 * Adds a file to the current zip output stream
 *
 * @param path the path of the parent folder in the zip
 * @param file the file to be added//from  ww w .  j  a  v  a 2s .co m
 * @param zos  the current zip output stream
 * @throws FileNotFoundException
 * @throws IOException
 */
private static void addFileToZip(String path, File file, ZipOutputStream zos) throws IOException {
    String currentPath = StringUtils.isNotEmpty(path) ? path + "/" + file.getName() : file.getName();

    zos.putNextEntry(new ZipEntry(currentPath));

    InputStream is = new BufferedInputStream(new FileInputStream(file));
    try {
        IOUtils.copy(is, zos);
    } finally {
        IOUtils.closeQuietly(is);
    }

    zos.closeEntry();
}

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

private static Object _serializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField,
        Session session) {//from w w w .j av a2s. c  o 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:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Recursive method for adding contents (also sub folders) of a folder to a Zip file.
 * Code comes from:// w  w  w . java2 s . c  o m
 * <tt>http://www.java2s.com/Code/Java/File-Input-Output/
 * Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm</tt>.
 * @param basePath The base path of the Zip folder (for creating relative folders inside the Zip archive).
 * @param directory The current directory which shall be zipped into the Zip archive.
 * @param out A Zip archive.
 * @throws IOException if an I/O error has occurred
 */
private static void addDir(String basePath, File directory, ZipOutputStream out) throws IOException {
    File[] files = directory.listFiles();

    for (int i = 0; i < files.length; i++) {
        String entryName = makeRelative(basePath, files[i]);
        if (null != entryName && !entryName.isEmpty()) {
            if (files[i].isDirectory()) {
                out.putNextEntry(new ZipEntry(entryName));
                out.closeEntry();
                addDir(basePath, files[i], out);
            } else {
                out.putNextEntry(new ZipEntry(entryName));
                FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
                try {
                    IOUtils.copy(in, out);
                    in.close();
                } catch (IOException e1) {
                    IOUtils.closeQuietly(in);
                    throw e1;
                }
                out.closeEntry();
            }
        }
    }
}

From source file:ZipHandler.java

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

public static void makeZip(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);
    String bpath = "c:\\";
    FileInputStream fisBLOB = createInputStream(bpath);
    zos.putNextEntry(new ZipEntry("blob.lob"));
    writeInOutputStream(fisBLOB, zos);

    zos.closeEntry();
    String cpath = "c:\\";
    FileInputStream fisCLOB = createInputStream(cpath);
    zos.putNextEntry(new ZipEntry("clob.lob"));
    writeInOutputStream(fisCLOB, zos);
    zos.closeEntry();
    fis.close();
    fisCLOB.close();
    fisBLOB.close();
    zos.close();
    fos.close();

}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();//w w w.ja  v a  2  s. c o m
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

private static void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            addFolderToZip(file, zip, baseName);
        } else {/*w ww  .  jav a  2  s .co m*/
            String name = file.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name);
            zip.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(file), zip);
            zip.closeEntry();
        }
    }
}