package com.ejie.ad74a.util.comun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.tools.zip.ZipEntry;
/**
 * Clase de utilidades para agrupar funcionalidades relacionadas con ficheros ZIP.
 * @author jbhg
 */
public class UtilZip {

	/*
	 * Constructor privado para clase de utilidades
	 */
	private UtilZip() {
	}

	/**
	 * Comprime una lista de ficheros en el fichero indicado
	 * 
	 * @param filesToBeZipped
	 * @param zipFile
	 * @param deleteAfterZipped
	 * @throws IOException
	 */
	public static void zipFiles(List<File> filesToBeZipped, File zipFile, boolean deleteAfterZipped)
			throws IOException {
		try (final FileOutputStream fos = new FileOutputStream(zipFile);
				ZipOutputStream zipOut = new ZipOutputStream(fos)) {
			for (File file : filesToBeZipped) {
				try (FileInputStream fis = new FileInputStream(file)) {
					ZipEntry zipEntry = new ZipEntry(file.getName());
					zipOut.putNextEntry(zipEntry);
					IOUtils.copy(fis, zipOut);
				} finally {
					if (deleteAfterZipped) {
						Files.delete(file.toPath());
					}
				}
			}
		}
	}
}
