Zip文件无效?

时间:2014-03-03 01:48:43

标签: java

所以下面的代码是针对Bukkit服务器的。它的作用是压缩服务器目录中的所有文件,并将zip文件放在“backups”文件夹中。它在某种程度上有效。这些文件确实被复制和压缩,但是如果我点击该文件,其中没有任何内容(即使我知道因为它显示了它旁边的文件大小),当我尝试解压缩它时,窗口给了我一个错误说明zip文件无效。有什么想法吗?谢谢:))

    public class Backup extends Thread{

    private static Backup instance;

    public static Backup getInstance(){
        return instance;
    }

    public static void newRef(){
        instance = new Backup();
    }

    public void backup(final CommandSender sender) {
        new Thread() {
            public void run() {
                sender.sendMessage(MessageManager.getChatPrefix() + "Starting backup...");
                Backup.this.startBackup();
                sender.sendMessage(MessageManager.getChatPrefix() + "Done!");
            }
        }.start();
    }

    public void backup() {
        new Thread() {
            public void run() {
                Backup.this.startBackup();
            }
        }.start();
    }

    public void zipDir(String dir2zip, ZipOutputStream zos){ 
        try{
          File zipDir = new File(dir2zip);
          String[] dirList = zipDir.list();

          byte[] readBuffer = new byte[2156];
          int bytesIn = 0;

          for (String file : dirList) {

              File f = new File(zipDir, file);
              if (f.isDirectory()) {
                  String filePath = f.getPath();
                  zipDir(filePath, zos);
              }else{
                FileInputStream fis = new FileInputStream(f);
                ZipEntry anEntry = new ZipEntry(f.getPath());
                zos.putNextEntry(anEntry);

                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }

                fis.close();
            }

          }
        }catch(Exception e){

        }}

    public void startBackup(){
        try {
            File root = new File(".");

            File bfolder = new File(root.getAbsolutePath() + "/backup/");

            if (!bfolder.exists())
                bfolder.mkdir();

            File backup = new File(bfolder.getAbsolutePath() + "/backup.zip");

            if (!backup.exists())
                backup.createNewFile();

            try{
                ZipOutputStream zs = new ZipOutputStream(new FileOutputStream(backup));
                System.out.println(MessageManager.getConsolePrefix() + "Zipping files...");
                zipDir(root.getAbsolutePath(), zs);
                zs.close();
                System.out.println(MessageManager.getConsolePrefix() +"Done!");         
            }catch (Exception e){}

        }catch(Exception e){
            e.printStackTrace();
        }

         DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
         java.util.Date dateTime = new java.util.Date();
         String date = dateFormat.format(dateTime);

         List<World> worlds = Bukkit.getWorlds();
         Object[] theWorlds = worlds.toArray();
         String path = new File("").getAbsolutePath();

         for(int i=0; i<theWorlds.length; i++){
             World w = (World) theWorlds[i];
             try {
                 w.save();
             } catch (Exception e1) {}

             String wNam = w.getName();
             File srcFolder = new File(path + File.separator + wNam);
             File destFolder = new File(Main.getInstance().getDataFolder().getAbsolutePath() + File.separator + "World Backups" + File.separator + date + File.separator + wNam);
             destFolder.mkdirs();
             if(srcFolder.exists()){
                 try{
                     Copier.copyFolder(srcFolder,destFolder);
                 }catch(IOException e){}
             }
         }
    }
}
class Copier{
    public static void copyFolder(File src, File dest) throws IOException{
        if(src.isDirectory()){
            if(!dest.exists())
                dest.mkdir();

            String files[] = src.list();
            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);

                copyFolder(srcFile,destFile);
            }
        }else{
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 
            byte[] buffer = new byte[1024];
            int length;

            while ((length = in.read(buffer)) > 0)
                out.write(buffer, 0, length);
            in.close();
            out.close();
        }
    }
}

0 个答案:

没有答案
相关问题