Pax 4 Posted January 18, 2017 Report Share Posted January 18, 2017 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(); } } } } Link to comment Share on other sites More sharing options...
OfficialLegend 12 Posted February 15, 2017 Report Share Posted February 15, 2017 Looks nice man. Good work. Link to comment Share on other sites More sharing options...
'Kyle 1 Posted November 18, 2018 Report Share Posted November 18, 2018 Thanks, I am going to use this! Link to comment Share on other sites More sharing options...
ReignersXV 1 Posted April 4, 2020 Report Share Posted April 4, 2020 I plan on using this, thank you very much. Link to comment Share on other sites More sharing options...
Artur 0 Posted April 6, 2020 Report Share Posted April 6, 2020 Thank you for providing this code! Link to comment Share on other sites More sharing options...
bowlcutgod 16 Posted May 6, 2020 Report Share Posted May 6, 2020 need to fill my posts looks nice ………. Link to comment Share on other sites More sharing options...
nick491 0 Posted July 7, 2023 Report Share Posted July 7, 2023 thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now