leonardo 2015-11-16 16:49:48 +00:00
parent 8c3b74df60
commit c8abc8469c
2 changed files with 85 additions and 68 deletions

View File

@ -1,6 +1,23 @@
package com.rjconsultores.ventaboletos.web.utilerias.menu.item.relatorios;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.activation.MimetypesFileTypeMap;
import javax.sql.DataSource;
import org.zkoss.zul.Filedownload;
import com.rjconsultores.integracaoreceitadespesa.Arquivo;
import com.rjconsultores.integracaoreceitadespesa.dao.Totalbus;
import com.rjconsultores.ventaboletos.web.utilerias.menu.DefaultItemMenuSistema;
import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;
public class ItemMenuRelatorioArquivoBGM extends DefaultItemMenuSistema {
@ -14,72 +31,72 @@ public class ItemMenuRelatorioArquivoBGM extends DefaultItemMenuSistema {
public String getClaveMenu() {
return "COM.RJCONSULTORES.ADMINISTRACION.GUI.RELATORIOS.MENU.RELATORIOARQUIVOBGM";
}
//
// @Override
// public void ejecutar() {
// DataSource ds = null;
// try {
// ds = (DataSource)AppContext.getApplicationContext().getBean("dataSource");
// Totalbus totalbus = new Totalbus(ds.getConnection());
// String fileName = System.getProperty("jboss.server.log.dir") + "/arquivobgm.txt";
// String fileZip = System.getProperty("jboss.server.log.dir") + "/arquivobgm.zip";
// FileInputStream inputStream;
// Arquivo.GravaArquivo(fileName, totalbus.getDespesasReceitas(null, null));
//
// excluirArquivosZip();
// compactarArquivo(fileZip, fileName);
// File file = new File(fileZip);
// if (file.exists()) {
// inputStream = new FileInputStream(file);
// Filedownload.save(inputStream, new MimetypesFileTypeMap().getContentType(file), file.getName());
// }
// file.delete();
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// ds.getConnection().close();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
//
// public static void compactarArquivo(String arqSaida,String arqEntrada) throws IOException{
// int cont;
// byte[] dados = new byte[TAMANHO_BUFFER];
// BufferedInputStream origem = null;
// FileInputStream streamDeEntrada = null;
// FileOutputStream destino = null;
// ZipOutputStream saida = null;
// ZipEntry entry = null;
// try {
// destino = new FileOutputStream(new File(arqSaida));
// saida = new ZipOutputStream(new BufferedOutputStream(destino));
// File file = new File(arqEntrada);
// streamDeEntrada = new FileInputStream(file);
// origem = new BufferedInputStream(streamDeEntrada, TAMANHO_BUFFER);
// entry = new ZipEntry(file.getName());
// saida.putNextEntry(entry);
// while((cont = origem.read(dados, 0, TAMANHO_BUFFER)) != -1) {
// saida.write(dados, 0, cont);
// }
// origem.close();
// saida.close();
// } catch(IOException e) {
// throw new IOException(e.getMessage());
// }
// }
//
// private void excluirArquivosZip(){
// File pasta = new File(System.getProperty("jboss.server.log.dir"));
// File[] arquivos = pasta.listFiles();
//
// for(File arquivo : arquivos) {
// if(arquivo.getName().endsWith("zip")) {
// arquivo.delete();
// }
// }
// }
@Override
public void ejecutar() {
DataSource ds = null;
try {
ds = (DataSource) AppContext.getApplicationContext().getBean("dataSource");
Totalbus totalbus = new Totalbus(ds.getConnection());
String fileName = System.getProperty("jboss.server.log.dir") + "/arquivobgm.txt";
String fileZip = System.getProperty("jboss.server.log.dir") + "/arquivobgm.zip";
FileInputStream inputStream;
Arquivo.GravaArquivo(fileName, totalbus.getDespesasReceitas(null, null));
excluirArquivosZip();
compactarArquivo(fileZip, fileName);
File file = new File(fileZip);
if (file.exists()) {
inputStream = new FileInputStream(file);
Filedownload.save(inputStream, new MimetypesFileTypeMap().getContentType(file), file.getName());
}
file.delete();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ds.getConnection().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void compactarArquivo(String arqSaida,String arqEntrada) throws IOException{
int cont;
byte[] dados = new byte[TAMANHO_BUFFER];
BufferedInputStream origem = null;
FileInputStream streamDeEntrada = null;
FileOutputStream destino = null;
ZipOutputStream saida = null;
ZipEntry entry = null;
try {
destino = new FileOutputStream(new File(arqSaida));
saida = new ZipOutputStream(new BufferedOutputStream(destino));
File file = new File(arqEntrada);
streamDeEntrada = new FileInputStream(file);
origem = new BufferedInputStream(streamDeEntrada, TAMANHO_BUFFER);
entry = new ZipEntry(file.getName());
saida.putNextEntry(entry);
while((cont = origem.read(dados, 0, TAMANHO_BUFFER)) != -1) {
saida.write(dados, 0, cont);
}
origem.close();
saida.close();
} catch(IOException e) {
throw new IOException(e.getMessage());
}
}
private void excluirArquivosZip(){
File pasta = new File(System.getProperty("jboss.server.log.dir"));
File[] arquivos = pasta.listFiles();
for(File arquivo : arquivos) {
if(arquivo.getName().endsWith("zip")) {
arquivo.delete();
}
}
}
}