Example usage for java.util.zip ZipOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.util.VFSZipper.java

public static void zip(FileObject root, List<FileObject> files, OutputStream out) throws IOException {
    String basePath = root.getName().getPath();
    Closer closer = Closer.create();/*from  w w w. j  av  a2s . co m*/
    try {
        ZipOutputStream zos = new ZipOutputStream(out);
        closer.register(zos);
        for (FileObject fileToCopy : files) {
            ZipEntry zipEntry = zipEntry(basePath, fileToCopy);
            zos.putNextEntry(zipEntry);
            copyFileContents(fileToCopy, zos);
            zos.flush();
            zos.closeEntry();
        }
    } catch (IOException e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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

/**
 * Zip folder/*  w w w.  j  ava 2s.  c o m*/
 * @param srcFolderPath source folder path
 * @param outputZipPath output zip file path
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void zipFolder(String srcFolderPath, String outputZipPath)
        throws FileNotFoundException, IOException {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    // Create the output stream to zip file result
    fileWriter = new FileOutputStream(outputZipPath);
    zip = new ZipOutputStream(fileWriter);

    // Add the folder to the zip
    addFolderToZip("", srcFolderPath, zip);

    // Close the zip objects
    zip.flush();
    zip.close();
}

From source file:edu.kit.dama.util.ZipUtils.java

/**
 * Write all files located in pDirectory into a zip file specified by
 * pZipOut. The provided base path is used to keep the structure defined by
 * pDirectory within the zip file./*from w  w w  .  ja  va  2 s  . c  o  m*/
 *
 * Due to the fact, that we have only one single base path, pDirectory must
 * start with pBasePath to avoid unexpected zip file entries.
 *
 * @param pFiles The directory containing the input files
 * @param pBasePath The base path the will be removed from all file paths
 * before creating a new zip entry
 * @param pZipOut The zip output file
 * @throws IOException If something goes wrong, in most cases if there are
 * problems with reading the input files or writing into the output file
 */
public static void zip(File[] pFiles, String pBasePath, File pZipOut) throws IOException {
    ZipOutputStream zipOut = null;
    try {
        zipOut = new ZipOutputStream(new FileOutputStream(pZipOut));
        zip(pFiles, pBasePath, zipOut);
        zipOut.finish();
        zipOut.flush();
    } finally {
        try {
            if (zipOut != null) {
                zipOut.close();
            }
        } catch (IOException ignored) {
        }
    }
}

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();//ww w.  j  a  v a  2 s.  co m
    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: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();/*from   w w w . jav  a 2s.c om*/
    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:org.geoserver.backuprestore.utils.BackupUtils.java

/**
 * Compress {@code sourceFolder} to the archive file {@code archiveFile}; both shall previously exist.
 * //from   w w w.j  a v a 2  s. c o  m
 * @param sourceFolder
 * @param archiveFile
 * @throws IOException
 */
public static void compressTo(Resource sourceFolder, Resource archiveFile) throws IOException {
    // See https://commons.apache.org/proper/commons-vfs/filesystems.html
    // for the supported filesystems

    FileSystemManager manager = VFS.getManager();

    FileObject sourceDir = manager
            .createVirtualFileSystem(manager.resolveFile(sourceFolder.dir().getAbsolutePath()));

    try {
        if ("zip".equalsIgnoreCase(FileUtils.getExtension(archiveFile.path()))) {
            // apache VFS does not support ZIP as writable FileSystem

            OutputStream fos = archiveFile.out();

            // Create access to zip.
            ZipOutputStream zos = new ZipOutputStream(fos);

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            fos.close();
        } else {
            // Create access to archive.
            FileObject zipFile = manager.resolveFile(resolveArchiveURI(archiveFile));
            zipFile.createFile();
            ZipOutputStream zos = new ZipOutputStream(zipFile.getContent().getOutputStream());

            // add entry/-ies.
            for (FileObject sourceFile : sourceDir.getChildren()) {
                writeEntry(zos, sourceFile, null);
            }

            // Close streams
            zos.flush();
            zos.close();
            zipFile.close();
            manager.closeFileSystem(zipFile.getFileSystem());
        }
    } finally {
        manager.closeFileSystem(sourceDir.getFileSystem());
    }
}

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 ava 2  s.  co 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:org.agnitas.util.ZipUtilities.java

/**
 * Close an open ZipOutputStream without errormessages
 * // w  ww . java  2s  .c o  m
 * @param zipOutputStream
 * @throws IOException
 */
public static void closeZipOutputStreamQuietly(ZipOutputStream zipOutputStream) {
    try {
        zipOutputStream.finish();
        zipOutputStream.flush();
        zipOutputStream.close();
        zipOutputStream = null;
    } catch (IOException e) {
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (Exception e) {
            }
            zipOutputStream = null;
        }
    }
}

From source file:org.agnitas.util.ZipUtilities.java

/**
 * Close an open ZipOutputStream/*from w w  w. j  a  va  2  s.  co  m*/
 * 
 * @param zipOutputStream
 * @throws IOException
 */
public static void closeZipOutputStream(ZipOutputStream zipOutputStream) throws IOException {
    try {
        zipOutputStream.finish();
        zipOutputStream.flush();
        zipOutputStream.close();
        zipOutputStream = null;
    } catch (IOException e) {
        throw e;
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (Exception e) {
            }
            zipOutputStream = null;
        }
    }
}

From source file:org.flowerplatform.core.CoreUtils.java

public static void zipFiles(List<String> files, String zipFilePath, String rootFolderName) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(zipFilePath);
    zip = new ZipOutputStream(fileWriter);
    zip.putNextEntry(new ZipEntry(rootFolderName + "/"));

    for (String path : files) {
        File file = new File(path);
        if (file.isFile()) {
            addFileToZip(rootFolderName, path, zip, false);
        } else {/* ww w .ja  v  a  2  s.c  om*/
            addFolderToZip(rootFolderName, path, zip);
        }
    }
    zip.flush();
    zip.close();
}