Here you can find the source of zip(String[] sFl, String sZip)
Parameter | Description |
---|---|
sFile | Array file yang akan di-zip |
sZip | Nama file zip |
public static void zip(String[] sFl, String sZip)
//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()); } } }