package com.rjconsultores.ventaboletos.utilerias;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

	public static void compactarArquivos(File arquivoDeSaida, File... arquivosEOuPastas) throws IOException {
		if (arquivosEOuPastas != null && arquivoDeSaida != null) {
			byte[] buf = new byte[1024];
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(arquivoDeSaida));

			for (File f : arquivosEOuPastas) {
				FileInputStream in = new FileInputStream(f.getPath());
				out.putNextEntry(new ZipEntry(f.getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
				f.delete();
			}
			out.close();
		}
	}

	public static void compactarArquivos(File arquivoDeSaida, List<File> arquivosEOuPastas) throws IOException {
		if (arquivosEOuPastas != null && arquivoDeSaida != null) {
			byte[] buf = new byte[1024];
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(arquivoDeSaida));

			for (File f : arquivosEOuPastas) {
				FileInputStream in = new FileInputStream(f.getPath());
				out.putNextEntry(new ZipEntry(f.getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
				f.delete();
			}
			out.close();
		}
	}

}
