Pessoal estou com o seguinte problema:
Tenho uma classe que é responsável por gerar documentos preenchidos que gera os mesmos através de um método que receber uma String com o caminho do template, uma String com o destino do File que será gerado e um List para obter as informações e inserí-las nos File de destino.
Ele esta funcionado normalmente, só que toda vez que eu gerar o File devo inseri-lo em um List que depois será utilizado para gerar um .zip contendo todos os Files em um único .zip.
Minha dúvida é:
Como implemento este método que irá gerar o .zip contendo todos os Files a partir deste List ?
Aguardo a ajuda de todos.
Abraço
Olá amigo veja se ajuda. Ja fiz isso:
/**
* Metodo que compacta o arquivo csv gerado
*
* @param fileToExport
* @return {@String} fileName
* @throws Exception
* @author paulo.leite
*/
private String zipFileToFinalize(File toGroupFiles, File... filesToExport) throws Exception {
byte[] buffer = new byte[18024];
// Nome do arquivo ZIP
String rootPath = fileToExport.getParent();
if (rootPath.lastIndexOf("/") != rootPath.length() - 1) {
rootPath += "/";
}
String zipFileName = rootPath + toGroupFiles.getName().replace(".csv", ".zip"); //se for csv usa mesmo nome.zip
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
// Ajusta modo de compressão
out.setLevel(Deflater.BEST_COMPRESSION);
for(File fileToAdd : filesToExport) {
FileInputStream in = new FileInputStream(fileToAdd);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileToAdd.getName()));
// Transfer bytes from the current file to the ZIP file
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
// Fecha o Entry
out.closeEntry();
// Close the current file input stream
in.close();
}
// Close the ZipOutPutStream
out.close();
} catch (IllegalArgumentException iae) {
throw new AdminExportDataException(iae);
} catch (Exception e) {
throw new AdminExportDataException(e);
}
return zipFileName;
}
Espero ter ajudado fallow