1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| String folderPath = new File("").getAbsolutePath() + "/backups/";
File folder = new File(folderPath);
if (!folder.exists()) {
if (folder.mkdir()) {
getLogger().info("Backup folder created!");
} else {
getLogger().info("Failed to create backup folder...");
}
}
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd - HH mm ss");
String zipFile = new File("").getAbsolutePath() + "/backups/" + dateFormat.format(cal.getTime()) + ".zip";
File zip = new File(zipFile);
String srcDir = new File("").getAbsolutePath();
try {
byte[] buffer = new byte[1024];
File dir = new File(srcDir);
File[] files = dir.listFiles();
try {
zip.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = new FileOutputStream(zip);
ZipOutputStream zos = new ZipOutputStream(fos);
for (int i = 0; i < files.length; i++) {
if(!files[i].getName().contains("backups")) {
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
}
zos.close();
}
catch (IOException ioe) {
System.out.println("Error creating zip file" + ioe);
} |
Partager