import java.util.zip.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.DecimalFormat;
import javax.swing.filechooser.FileView;
public class backup{
public backup(){
JFileChooser fc = new JFileChooser("c:\\");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDragEnabled(false);
fc.setApproveButtonMnemonic('S');
fc.setApproveButtonToolTipText("Salvar Backup");
fc.setApproveButtonText("Salvar");
fc.setDialogTitle("Backup do Sistema");
int result =fc.showSaveDialog(null);
if( result == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile(); //local onde quero que salve o backup
if(file != null){
byte[] buf = new byte[1024];
try {
File f = new File("C:\\Teste"); // Pega todos os arquivos desse diretorio
String []filenames = f.list();
ZipOutputStream out = new ZipOutputStream(new FileOutputStream( file + getData() + ".zip"));
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream("C:\\Teste"+"//"+filenames[i]);
out.putNextEntry(new ZipEntry(filenames[i]));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
JOptionPane.showMessageDialog(null, "Backup Criado com Sucesso", "Criando um Backup", JOptionPane.QUESTION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "No Possivel Criar Backup - Tente Novamente", "", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
}
public String getData(){
Calendar cal = new GregorianCalendar();
DecimalFormat dig = new DecimalFormat("00");
return ("_" + dig.format( cal.get(Calendar.DAY_OF_MONTH)) + "_" + dig.format( cal.get(Calendar.MONTH) + 1) + "_" + dig.format( cal.get(Calendar.YEAR) - 2000));
}
}
oq isso faz, ele abre um JFileChooser e vc indica o caminho que irá gerar o arquivo zipado..beleza ele faz isso, o problema que ele gera o arquivos com o nome do diretorio selecionado ou aquele q vc colocar lá no JFileChooser, portanto eu naum quero isso quero q nao seja permitido colocar nome de arquivo e que o arquivo seje gerado com o nome definido sempre q executar o processo
essa linhaFile file = fc.getSelectedFile();
ZipOutputStream out = new ZipOutputStream(new FileOutputStream( file + getData() + ".zip"));
alguem sabe me dizer como posso resolver isso...
obrigado[/code]