package br.com.elotech.websaude.integracao.esus.util;

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

public class Compactador {

  //Constantes
  static final int TAMANHO_BUFFER = 4096; // 4kb
  public static List<String> ARQUIVOS = new ArrayList<>();

  //método para compactar arquivo
  public static void compactarParaZip(String arqSaida)
    throws IOException {

    if (ARQUIVOS.size() > 0) {

      byte[] buf = new byte[1024];

      try {
        // Create the ZIP file

        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(arqSaida));

        // Compress the files
        for (int i = 0; i < ARQUIVOS.size(); i++) {
          File file = new File("arqs/" + ARQUIVOS.get(i));
          FileInputStream in = new FileInputStream(file.getPath());

          // Add ZIP entry to output stream.
          out.putNextEntry(new ZipEntry(ARQUIVOS.get(i)));
          // Transfer bytes from the file to the ZIP file
          int len;
          while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
          }

          // Complete the entry
          out.closeEntry();
          in.close();
          file.deleteOnExit();
        }

        // Complete the ZIP file
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
