iscomar001
11/30/2018 - 11:16 PM

Crear ZIP

Crear archivo comprimido

public static void toZip(File fileOut, List<File> listaArchivos) {

    byte[] buffer = new byte[1024];

    try {

        FileOutputStream fos = new FileOutputStream(fileOut.getAbsolutePath());
        ZipOutputStream zos = new ZipOutputStream(fos);

        System.out.println("Output to Zip : " + fileOut.getAbsolutePath());

        for (File file : listaArchivos) {

            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(file);
            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            in.close();
        }

        zos.closeEntry();
        //remember close it
        zos.close();

    } catch (IOException ex) {
        showErrorMessage("Error al zip: " + ex.getMessage());
    }

}