[izpack-devel] patch for accessing packs in the setup directory in case of a web installer

Stefan Wachter stefan.wachter at gmx.de
Thu Nov 16 08:43:56 CET 2006


Hi all,

I generated a web installer in order to have the various packs separated 
from the setup jar. In the class com.izforge.izpack.installer.Unpacker I 
found the following comment in the getPackAsStream() method (at line 933 
of IzPack Version 3.9.0):

// TODO: Look first in same directory as primary jar
// This may include prompting for changing of media

I implemented to look in the same directory as the primary jar (but did 
not implement a check for the correct media). The inserted lines are:

// start code

File setupDir = getSetupDir();
File packFile = new File(setupDir, idata.info.getInstallerBase() + 
".pack" + n + ".jar");
if (packFile.exists()) {
     URL packUrl = packFile.toURL();
     URL url = new URL("jar:" + packUrl + "!/packs/pack" + n);
     URLConnection connection = url.openConnection();
     in = connection.getInputStream(); // just to make
     if (in != null) return in;
}

// end code

This snippet checks if a file for the pack to be accessed exists in the 
setup directory. If the check suceedes then a corresponding stream is 
returned. The method getSetupDir() is implemented by:

// start code

public File getSetupDir() throws Exception {

   URL jarUrl = 
getClass().getResource("/com/izforge/izpack/installer/Unpacker.class");
   String stringForm = jarUrl.toString();
   String fileForm = jarUrl.getFile();

   File jarFile = null;
   int endIdx = stringForm.indexOf("!/");
   if (endIdx == -1) {
     throw new RuntimeException("Can't locate the jar file - url: '" + 
stringForm + "'");
   }
   String unescaped = null;
   String fileNamePart = stringForm.substring("jar:file:".length(), endIdx);
   jarFile = new File(fileNamePart);
   if (!jarFile.exists()) {
     // try to unescape encase the URL Handler has escaped the " " to %20
     unescaped = unescape(fileNamePart);
     jarFile = new File(unescaped);
     if (!jarFile.exists()) {
       throw new Exception("Can't locate the jar file - tried: '" + 
fileNamePart + "' and '" + unescaped + "'");
     }
   }

   return jarFile.getParentFile();

}

private static String unescape(final String s) {
   StringBuffer sb = new StringBuffer(s.length());

   for (int i = 0; i < s.length(); i++) {
     char c = s.charAt(i);
     switch (c) {
       case '%': {
         try {
           sb.append( (char) Integer.parseInt(s.substring(i + 1, i + 3), 
16));
           i += 2;
           break;
         } catch (NumberFormatException nfe) {
           throw new IllegalArgumentException();
         } catch (StringIndexOutOfBoundsException siob) {
           String end = s.substring(i);
           sb.append(end);
           if (end.length() == 2) i++;
         }
         break;
       }
       default: {
         sb.append(c);
         break;
       }
     }
   }
   return sb.toString();
}

// end code

Is this code of public interest? Will someone integrate it into IzPack?

Cheers,
--Stefan





More information about the izpack-devel mailing list