Posted January 18, 20178 yr comment_8306 This is a program that backups up the listed folders, I've made some example below in the String array, over how you'd list the folders, very simple to use, there is no task schedule (data consuming), but upon initiating it, it'll determine wherever or not the zip of the date has been created. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class BackupManager { /** * @author: Pax M */ /** * The directories that are being zipped */ private static String[] FOLDERS = { "./source/", "./storage/accounts/" }; /** * The location where the zipped directories are being stored */ private static String[] ZIPPED = { "./storage/backups/source/", "./storage/backups/accounts/" }; /** * Initiates the backup manager class */ public static void init() { for (int folders = 0; folders < FOLDERS.length; folders++) { File input = new File(FOLDERS[folders]); File output = new File(ZIPPED[folders] + "/" + new SimpleDateFormat("MM.dd.yy").format(new Date()) + ".zip"); if (!output.exists()) { try { if (input.list().length == 0) return; ZipOutputStream zipOutputStream = new ZipOutputStream( new FileOutputStream(output)); zip(input, input, zipOutputStream); zipOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } else System.err.println(output.getName() + " directory is empty."); } } /** * Zip. * * @param directory * the directory * @param base * the base * @param zipOutputStream * the zos * @throws IOException * Signals that an I/O exception has occurred. */ private static final void zip(File directory, File base, ZipOutputStream zipOutputStream) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[20000]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) zip(files[i], base, zipOutputStream); else { FileInputStream in = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath().substring( base.getPath().length() + 1)); zipOutputStream.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { zipOutputStream.write(buffer, 0, read); } in.close(); } } } }
Create an account or sign in to comment