Skip to content

Instantly share code, notes, and snippets.

@h3nrique
Created December 14, 2012 03:04
Show Gist options
  • Save h3nrique/4282353 to your computer and use it in GitHub Desktop.
Save h3nrique/4282353 to your computer and use it in GitHub Desktop.
Classe para fazer backup de diretórios.
# Linux
infolder /home/h3nrique/Documents/test_folder_backup
# Windows
# infolder C:\\Folder\\To\Backup
# Linux
outfolder /home/h3nrique/Desktop
# Windows
# outfolder C:\\Folder\\To\Put\\Zip\\File
outfile backupTestFolder.zip
datelimitfile 30
package com.package.utils;
/*
* This code is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This code is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Classe para fazer backup de diretórios.
*
* @version -1 (TODO : Organizar o código.)
*
* @author <a href="mailto:paulohenriqueas13@gmail.com">Paulo H3nrique Alves</a>
*/
public class ZipApplication {
private static Properties properties = new Properties();
private static String DESTINATION_FOLDER;
private static String SOURCE_FOLDER;
private static Integer LIMIT_DATE;
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH'h'mm'min'");
public static void main(String[] args) {
String zipFileName = null;
byte[] buffer = new byte[1024];
File file3 = null;
File file4 = null;
// START - Carrega arquivo properties
try {
properties.load(new BufferedReader(new FileReader("config.properties")));
} catch (FileNotFoundException e) {
System.err.println("Arquivo de propriedades não encontrado");
System.exit(1);
} catch (IOException e) {
System.err.println("Erro ao ler arquivo de propriedades.");
System.exit(1);
}
try {
DESTINATION_FOLDER = properties.get("outfolder").toString();
SOURCE_FOLDER = properties.get("infolder").toString();
zipFileName = properties.get("outfile").toString();
LIMIT_DATE = Integer.valueOf(properties.get("datelimitfile").toString());
if(!new File(DESTINATION_FOLDER).isDirectory() || !new File(SOURCE_FOLDER).isDirectory()) {
new Exception("Diretório inválido");
}
file3 = new File(DESTINATION_FOLDER);
file4 = new File(SOURCE_FOLDER);
} catch (Exception e) {
System.err.println("Arquivo de propriedades inválido.");
System.exit(1);
}
// START - Carrega arquivo properties
// START - Remove os arquivos que expirou o prazo de backup
if (file3.isDirectory()) {
String[] files = file3.list();
for (String file : files) {
try {
File file2 = new File(file3.getAbsolutePath() + File.separator + file);
Long fileDateInMillis = file2.lastModified();
Calendar dataRemocao = Calendar.getInstance();
dataRemocao.add(Calendar.DAY_OF_MONTH, -LIMIT_DATE);
Long dataRemocaoInMillis = dataRemocao.getTimeInMillis();
if (file2.isFile() && file2.getName().replace(".zip", "").contains(zipFileName) && fileDateInMillis.compareTo(dataRemocaoInMillis) < 0 && file2.canWrite()) {
System.out.println("Excluindo arquivo :: " + file2);
file2.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// END - Remove os arquivos que expirou o prazo de backup
// START - Cria lista de arquivos a serem zipados
List<String> listaDeArquivos = criarListaDeArquivos(file4);
// END - Cria lista de arquivos a serem zipados
// START - Zipa os arquivos
try {
zipFileName = zipFileName.replace(".zip", "");
String dateAsString = simpleDateFormat.format(new Date());
zipFileName = zipFileName + "_" + dateAsString + ".zip";
File file2 = new File(DESTINATION_FOLDER);
FileOutputStream fos = new FileOutputStream(file2.getAbsolutePath() + File.separator + zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
System.out.println("Criando zip :: " + file2.getAbsolutePath() + File.separator + zipFileName);
for (String file : listaDeArquivos) {
System.out.println("Adicionando arquivo :: " + file);
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// START - Zipa os arquivos
System.out.println("Concluido");
System.exit(0);
}
public static List<String> criarListaDeArquivos(File file) {
List<String> fileListReturn = new ArrayList<String>();
if (file.isFile()) {
String absoluteFile = file.getAbsoluteFile().toString();
fileListReturn.add(absoluteFile.substring(SOURCE_FOLDER.length() + 1, absoluteFile.length()));
}
if (file.isDirectory()) {
String[] fileListB = file.list();
for (String file2 : fileListB) {
fileListReturn.addAll(criarListaDeArquivos(new File(file, file2)));
}
}
return fileListReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment