Java Zip File List zip(String[] sFl, String sZip)

Here you can find the source of zip(String[] sFl, String sZip)

Description

Zip array file

License

Open Source License

Parameter

Parameter Description
sFile Array file yang akan di-zip
sZip Nama file zip

Declaration

public static void zip(String[] sFl, String sZip) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**//  ww w.j a  v a 2 s .  co  m
     * Zip array file
     * @param sFile Array file yang akan di-zip
     * @param sZip Nama file zip
     */
    public static void zip(String[] sFl, String sZip) {
        // Menciptakan buffer untuk pembacaan file
        byte[] buf = new byte[1024];

        try {
            // Menciptakan file Zip
            ZipOutputStream zout = new ZipOutputStream(
                    new FileOutputStream(sZip));

            // Iterasi dan kompresi file
            for (String s : sFl) {
                FileInputStream in = new FileInputStream(s);
                ZipEntry ze = new ZipEntry(s);

                // Tambahkan Zip entry ke output stream
                zout.putNextEntry(ze);

                // Transfer byte dari file ke file Zip
                int len;
                while ((len = in.read(buf)) > 0) {
                    zout.write(buf, 0, len);
                }

                // Tutup entry dan bersihkan resource
                zout.closeEntry();
                System.out.println("Zip " + ze.getName() + " [ok]");
                in.close();
            }

            System.out.println("Simpan ke " + sZip + " [OK]");
            // Bersihkan resource
            zout.close();

        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Related

  1. zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)
  2. zip(List fileNames, List fileContents)
  3. zip(List fileNames, String outFileName)
  4. zip(List srcFiles, OutputStream os)
  5. zip(String[] filename, String outname)
  6. zip(String[] sourceFiles, String zipFile, String directory)
  7. zip(String[] srcFiles, String dstFile, String comment)
  8. zipFileList(List files, File destinationDir)
  9. zipFiles(ArrayList files, String destZipFile)