1
0
mirror of https://e.coding.net/circlecloud/Residence.git synced 2025-11-25 21:56:06 +00:00

move src path and modify config file...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092
2015-09-01 17:26:17 +08:00
parent 891e9834d6
commit 3fec9f6871
70 changed files with 230 additions and 317 deletions

View File

@@ -0,0 +1,115 @@
package com.bekvon.bukkit.residence.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.bukkit.World;
import com.bekvon.bukkit.residence.Residence;
public class DataBackup {
private File BackupDir = new File(Residence.getDataLocation(), "Backup");
public static void run() throws IOException {
DataBackup backup = new DataBackup();
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");
// Create the Source List, and add directories/etc to the file.
List<File> sources = new ArrayList<File>();
File saveFolder = new File(Residence.getDataLocation(), "Save");
File worldFolder = new File(saveFolder, "Worlds");
if (!saveFolder.isDirectory()) {
return;
}
File saveFile;
for (World world : Residence.getServ().getWorlds()) {
saveFile = new File(worldFolder, "res_" + world.getName() + ".yml");
if (saveFile.isFile()) {
sources.add(saveFile);
}
}
packZip(fileZip, sources);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private void packZip(File output, List<File> sources) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
for (File source : sources) {
if (source.isDirectory()) {
zipDir(zipOut, "", source);
} else {
zipFile(zipOut, "", source);
}
}
zipOut.flush();
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 {
if (!dir.canRead()) {
return;
}
File[] files = dir.listFiles();
path = buildPath(path, dir.getName());
for (File source : files) {
if (source.isDirectory()) {
zipDir(zos, path, source);
} else {
zipFile(zos, path, source);
}
}
}
private void zipFile(ZipOutputStream zos, String path, 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];
int byteCount = 0;
while ((byteCount = fis.read(buffer)) != -1) {
zos.write(buffer, 0, byteCount);
}
fis.close();
zos.closeEntry();
}
}