mirror of
https://e.coding.net/circlecloud/Residence.git
synced 2025-11-25 21:56:06 +00:00
@@ -12,52 +12,68 @@ import java.util.zip.Deflater;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
|
||||
import com.bekvon.bukkit.residence.Residence;
|
||||
|
||||
public class DataBackup {
|
||||
private File BackupDir = new File(Residence.getDataLocation(), "Backup");
|
||||
private final File BackupDir;
|
||||
|
||||
public static void run() throws IOException {
|
||||
DataBackup backup = new DataBackup();
|
||||
Residence plugin;
|
||||
|
||||
public DataBackup(final Residence plugin) {
|
||||
this.plugin = plugin;
|
||||
BackupDir = new File(plugin.getDataLocation(), "Backup");
|
||||
}
|
||||
|
||||
public static void run(final Residence plugin) throws IOException {
|
||||
final DataBackup backup = new DataBackup(plugin);
|
||||
backup.backup();
|
||||
}
|
||||
|
||||
public void backup() throws IOException {
|
||||
try {
|
||||
BackupDir.mkdir();
|
||||
Date date = new Date();
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
|
||||
File fileZip = new File(BackupDir, dateFormat.format(date) + ".zip");
|
||||
final Date date = new Date();
|
||||
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
|
||||
final File fileZip = new File(BackupDir, dateFormat.format(date) + ".zip");
|
||||
|
||||
// Create the Source List, and add directories/etc to the file.
|
||||
List<File> sources = new ArrayList<File>();
|
||||
final List<File> sources = new ArrayList<File>();
|
||||
|
||||
File saveFolder = new File(Residence.getDataLocation(), "Save");
|
||||
File worldFolder = new File(saveFolder, "Worlds");
|
||||
final File saveFolder = new File(plugin.getDataLocation(), "Save");
|
||||
final File worldFolder = new File(saveFolder, "Worlds");
|
||||
if (!saveFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
File saveFile;
|
||||
for (World world : Residence.getServ().getWorlds()) {
|
||||
for (final World world : Bukkit.getServer().getWorlds()) {
|
||||
saveFile = new File(worldFolder, "res_" + world.getName() + ".yml");
|
||||
if (saveFile.isFile()) {
|
||||
sources.add(saveFile);
|
||||
}
|
||||
}
|
||||
packZip(fileZip, sources);
|
||||
} catch (Exception e) {
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void packZip(File output, List<File> sources) throws IOException {
|
||||
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
|
||||
private String buildPath(final String path, final String file) {
|
||||
if (path == null || path.isEmpty()) {
|
||||
return file;
|
||||
}
|
||||
|
||||
return path + File.separator + file;
|
||||
}
|
||||
|
||||
private void packZip(final File output, final List<File> sources) throws IOException {
|
||||
final ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
|
||||
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
|
||||
|
||||
for (File source : sources) {
|
||||
for (final File source : sources) {
|
||||
if (source.isDirectory()) {
|
||||
zipDir(zipOut, "", source);
|
||||
} else {
|
||||
@@ -69,23 +85,15 @@ public class DataBackup {
|
||||
zipOut.close();
|
||||
}
|
||||
|
||||
private String buildPath(String path, String file) {
|
||||
if (path == null || path.isEmpty()) {
|
||||
return file;
|
||||
}
|
||||
|
||||
return path + File.separator + file;
|
||||
}
|
||||
|
||||
private void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
|
||||
private void zipDir(final ZipOutputStream zos, String path, final File dir) throws IOException {
|
||||
if (!dir.canRead()) {
|
||||
return;
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles();
|
||||
final File[] files = dir.listFiles();
|
||||
path = buildPath(path, dir.getName());
|
||||
|
||||
for (File source : files) {
|
||||
for (final File source : files) {
|
||||
if (source.isDirectory()) {
|
||||
zipDir(zos, path, source);
|
||||
} else {
|
||||
@@ -94,15 +102,15 @@ public class DataBackup {
|
||||
}
|
||||
}
|
||||
|
||||
private void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
|
||||
private void zipFile(final ZipOutputStream zos, final String path, final File file) throws IOException {
|
||||
if (!file.canRead()) {
|
||||
return;
|
||||
}
|
||||
|
||||
zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
|
||||
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] buffer = new byte[4092];
|
||||
final FileInputStream fis = new FileInputStream(file);
|
||||
final byte[] buffer = new byte[4092];
|
||||
int byteCount = 0;
|
||||
|
||||
while ((byteCount = fis.read(buffer)) != -1) {
|
||||
|
||||
Reference in New Issue
Block a user