Example usage for java.util.zip ZipOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

From source file:Main.java

final static private boolean createZipFile(String out, String... in) {
    InputStream is = null;//  w  w w.j av  a 2 s  . co m
    ZipOutputStream zos = null;

    try {
        zos = new ZipOutputStream(new FileOutputStream(out));
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }
    try {
        for (int i = 0; i < in.length; i++) {
            is = new FileInputStream(in[i]);
            ZipEntry ze = new ZipEntry(in[i]);
            zos.putNextEntry(ze);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            is.close();
            zos.closeEntry();
        }
        zos.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static String compress(String filename) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096];
    int bytesRead;
    String[] entries = { ".mp4" };
    String zipfile = filename.replace(".mp4", ".zip");
    if (!(new File(zipfile)).exists()) {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        out.setLevel(Deflater.BEST_COMPRESSION);
        out.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < entries.length; i++) {
            File f = new File(filename.replace(".mp4", entries[i]));
            if (f.exists()) {
                FileInputStream in = new FileInputStream(f);
                ZipEntry entry = new ZipEntry(f.getName());
                out.putNextEntry(entry);
                while ((bytesRead = in.read(buffer)) != -1)
                    out.write(buffer, 0, bytesRead);
                in.close();/*from www.  j  ava2  s . com*/
            }
        }
        out.close();
    }
    return zipfile;
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

public static void zip(File file, String zipFilePath) throws IOException {
    if (zipFilePath == null) {
        zipFilePath = file.getAbsolutePath() + ".zip"; //$NON-NLS-1$
    }//from w  w  w .  j  av  a 2 s  . co m

    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath));
    try {
        if (file.isDirectory()) {
            JoboxUtil.zipContents(file, file.getName(), zos);
        } else {
            try {
                JoboxUtil.zip(file, file.getName(), zos);
            } catch (Exception e) {
                throw new JoboxException(e);
            }
        }
    } finally {
        zos.close();
    }
}

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  ww  .j  a  va 2s  .  c om*/
    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:Utils.java

/**
 * Zip a list of file into one zip file.
 * /*from  w  w  w  .  jav a  2  s .c o m*/
 * @param files
 *          files to zip
 * @param targetZipFile
 *          target zip file
 * @throws IOException
 *           IO error exception can be thrown when copying ...
 */
public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(targetZipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[128];
        for (int i = 0; i < files.length; i++) {
            File currentFile = files[i];
            if (!currentFile.isDirectory()) {
                ZipEntry entry = new ZipEntry(currentFile.getName());
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                fis.close();
            }
        }
        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found : " + e);
    }

}

From source file:Main.java

public static void zipMutiCompress(File[] srcfile, File destFile) {
    byte[] buf = new byte[BUFFERED_SIZE];
    try {/* www  .j  a va 2 s  .  c om*/
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
        if (null != srcfile && srcfile.length > 0) {
            for (int i = 0; i < srcfile.length; i++) {
                File file = srcfile[i];
                if (null != file) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }
            }
        }
        out.close();
    } catch (IOException e) {

    }
}

From source file:org.openmrs.module.omodexport.util.OmodExportUtil.java

/**
 * Utility method for zipping a list of files
 * //ww  w  .  jav a2  s . c  o m
 * @param files -The list of files to zip
 * @param out
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void zipFiles(List<File> files, ZipOutputStream out) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    for (File f : files) {
        if (f.isDirectory())
            continue;// Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        out.putNextEntry(new ZipEntry(f.getName())); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }

    out.close();
}

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 {/* ww  w  .  j a va2  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:de.fu_berlin.inf.dpp.netbeans.feedback.ErrorLogManager.java

/**
 * Convenience wrapper method to upload an error log file to the server. To
 * save time and storage space, the log is compressed to a zip archive with
 * the given zipName.//from   www  . jav  a 2  s.c  o m
 * 
 * @param zipName
 *            a name for the zip archive, e.g. with added user ID to make it
 *            unique, zipName must be at least 3 characters long!
 * @throws IOException
 *             if an I/O error occurs
 */
private static void uploadErrorLog(String zipName, File file, IProgressMonitor monitor) throws IOException {

    if (ERROR_LOG_UPLOAD_URL == null) {
        log.warn("error log upload url is not configured, cannot upload error log file");
        return;
    }

    File archive = new File(System.getProperty("java.io.tmpdir"), zipName + ".zip");

    ZipOutputStream out = null;
    FileInputStream in = null;

    byte[] buffer = new byte[8192];

    try {

        in = new FileInputStream(file);

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

        out.putNextEntry(new ZipEntry(file.getName()));

        int read;

        while ((read = in.read(buffer)) > 0)
            out.write(buffer, 0, read);

        out.finish();
        out.close();

        FileSubmitter.uploadFile(archive, ERROR_LOG_UPLOAD_URL, monitor);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        archive.delete();
    }
}

From source file:com.cenrise.test.azkaban.Utils.java

public static void zipFolderContent(final File folder, final File output) throws IOException {
    final FileOutputStream out = new FileOutputStream(output);
    final ZipOutputStream zOut = new ZipOutputStream(out);
    try {/*from  ww  w. j av a  2  s  .c o  m*/
        final File[] files = folder.listFiles();
        if (files != null) {
            for (final File f : files) {
                zipFile("", f, zOut);
            }
        }
    } finally {
        zOut.close();
    }
}