package net.minecraft.launcher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class PackageEditor {
   public static void main(String[] args) throws IOException {
      unpackJar("./src/dest", "./src/a.jar");
   }

   public static void unpackJar(String destinationDir, String jarPath) throws IOException {
      File file = new File(jarPath);
      JarFile jar = new JarFile(file);
      Enumeration enums = jar.entries();

      JarEntry entry;
      String fileName;
      File f;
      while(enums.hasMoreElements()) {
         entry = (JarEntry)enums.nextElement();
         fileName = destinationDir + File.separator + entry.getName();
         f = new File(fileName);
         if (fileName.endsWith("/")) {
            f.mkdirs();
         }
      }

      enums = jar.entries();

      while(true) {
         do {
            if (!enums.hasMoreElements()) {
               return;
            }

            entry = (JarEntry)enums.nextElement();
            fileName = destinationDir + File.separator + entry.getName();
            f = new File(fileName);
         } while(fileName.endsWith("/"));

         InputStream is = jar.getInputStream(entry);
         FileOutputStream fos = new FileOutputStream(f);

         while(is.available() > 0) {
            fos.write(is.read());
         }

         fos.close();
         is.close();
      }
   }
}
