/** G4blData.java * * Downloads the Geant4 data files from CERN. Un-zips and un-tars * each file directly from the web. * * Requires the tar classes from ant-1.8.2.jar (org.apache.tools.tar.*). * * Implements a special URL in the Help.html that runs the G4blData * program: file:/G4blData . **/ import java.io.*; import java.net.URL; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import org.apache.tools.tar.*; import java.util.zip.GZIPInputStream; public class G4blData extends WindowAdapter implements ActionListener, Runnable { static boolean verbose = false; static String g4bl_dir = null; static String destination = null; static JLabel status=null; JTextField baseURLField=null; JTextField destinationField=null; String baseURL = "http://geant4.cern.ch/support/source/"; Vector urls = new Vector(); Vector cbs = new Vector(); Vector stat = new Vector(); Vector labels = new Vector(); Vector dirs = new Vector(); JButton button1=null; JButton button2=null; public G4blData() { // this is for Geant4.9.5 // For each data set, need an entry in labels, urls, and dirs. labels.add("Needed by all physics lists (101MB):"); urls.add("PhotonEvaporation.2.2.tar.gz"); dirs.add("PhotonEvaporation2.2"); labels.add(null); urls.add("G4EMLOW.6.23.tar.gz"); dirs.add("G4EMLOW6.23"); labels.add("Needed by ABLA lists (0.5MB):"); urls.add("G4ABLA.3.0.tar.gz"); dirs.add("G4ABLA3.0"); labels.add("Needed by specialized hardonic lists (35MB):"); urls.add("G4PII.1.3.tar.gz"); dirs.add("G4PII1.3"); labels.add(null); urls.add("G4RadioactiveDecay.3.4.tar.gz"); dirs.add("RadioactiveDecay3.4"); labels.add("Needed by optical photons (21MB):"); urls.add("RealSurface.1.0.tar.gz"); dirs.add("RealSurface1.0"); labels.add("Needed by _HP physics lists (1.4GB):"); urls.add("G4NEUTRONXS.1.1.tar.gz"); dirs.add("G4NEUTRONXS1.1"); labels.add(null); urls.add("G4NDL.4.0.tar.gz"); dirs.add("G4NDL4.0"); g4bl_dir = Util.findG4blDir(); if(g4bl_dir == null) { JOptionPane.showMessageDialog(null, "Cannot determine install directory.\n"); System.exit(1); } destination = Util.findOrCreateGeant4Data(); if(destination == null) { JOptionPane.showMessageDialog(null, "Cannot create Geant4Data directory.\n"); System.exit(1); } // Make sure that baseURL ends with "/" if(!baseURL.endsWith("/")) baseURL = baseURL + "/"; JFrame frame = new JFrame("G4blData"); String icon = g4bl_dir + "/share/g4beamline/G4blDataIcon.png"; if(new File(icon).isFile()) frame.setIconImage(Toolkit.getDefaultToolkit().getImage(icon)); frame.addWindowListener(this); Box content = Box.createVerticalBox(); content.add(Box.createVerticalStrut(10)); Box row = Box.createHorizontalBox(); row.add(Box.createHorizontalGlue()); JLabel title = new JLabel(" G4blData - Download data files for G4beamline "); title.setFont(title.getFont().deriveFont(Font.BOLD,24)); row.add(title); row.add(Box.createHorizontalGlue()); content.add(row); content.add(new JLabel(" ")); row = Box.createHorizontalBox(); row.add(Box.createHorizontalStrut(10)); JTextArea instr = new JTextArea(4,60); instr.setMargin(new Insets(10,10,10,10)); instr.append("This program will download data files from CERN and unpack them "); instr.append("into the Geant4Data directory of your installation.\n"); instr.append("Normally the BaseURL and Destination should be left unchanged.\n"); instr.append("The physics lists used in your simulations determine which data sets are required.\n"); instr.append("If you're not sure which you need, and have the space, download them all."); instr.setEditable(false); row.add(instr); row.add(Box.createHorizontalStrut(10)); content.add(row); content.add(new JLabel(" ")); row = Box.createHorizontalBox(); row.add(Box.createHorizontalStrut(10)); row.add(new JLabel(" Base URL:")); baseURLField = new JTextField(40); baseURLField.setText(baseURL); row.add(baseURLField); row.add(Box.createHorizontalStrut(10)); content.add(row); row = Box.createHorizontalBox(); row.add(Box.createHorizontalStrut(10)); row.add(new JLabel("Destination:")); destinationField = new JTextField(40); destinationField.setText(destination); row.add(destinationField); row.add(Box.createHorizontalStrut(10)); content.add(row); Box col = Box.createVerticalBox(); for(int i=0; i&- 2>&- && pwd`\n"); out.print("CYGPATH=echo\n"); out.print("if type cygpath >&- 2>&-; then CYGPATH=\"cygpath -m\"; fi\n"); out.print("export G4ABLADATA=`$CYGPATH $DIR/G4ABLA3.0`\n"); out.print("if ! test -d $G4ABLADATA; then unset G4ABLADATA; fi\n"); out.print("export G4LEDATA=`$CYGPATH $DIR/G4EMLOW6.23`\n"); out.print("if ! test -d $G4LEDATA; then unset G4LEDATA; fi\n"); out.print("export G4NEUTRONHPDATA=`$CYGPATH $DIR/G4NDL4.0`\n"); out.print("if ! test -d $G4NEUTRONHPDATA; then unset G4NEUTRONHPDATA; fi\n"); out.print("export G4NEUTRONXSDATA=`$CYGPATH $DIR/G4NEUTRONXS1.1`\n"); out.print("if ! test -d $G4NEUTRONXSDATA; then unset G4NEUTRONXSDATA; fi\n"); out.print("export G4PIIDATA=`$CYGPATH $DIR/G4PII1.3`\n"); out.print("if ! test -d $G4PIIDATA; then unset G4PIIDATA; fi\n"); out.print("export G4LEVELGAMMADATA=`$CYGPATH $DIR/PhotonEvaporation2.2`\n"); out.print("if ! test -d $G4LEVELGAMMADATA; then unset G4LEVELGAMMADATA; fi\n"); out.print("export G4RADIOACTIVEDATA=`$CYGPATH $DIR/RadioactiveDecay3.4`\n"); out.print("if ! test -d $G4RADIOACTIVEDATA; then unset G4RADIOACTIVEDATA; fi\n"); out.print("export G4REALSURFACEDATA=`$CYGPATH $DIR/RealSurface1.0`\n"); out.print("if ! test -d $G4REALSURFACEDATA; then unset G4REALSURFACEDATA; fi\n"); out.close(); } catch(Exception e) { } } public static void untarFile(String path) throws Exception { File tarFile = new File(path); // 1) check for valid file if (!tarFile.isFile()) { throw new Exception("Cannot open file '"+path+"'."); } untarInputStream(new FileInputStream(tarFile),path.endsWith("z")); } public static void untarURL(String url) throws Exception { URL tarURL = new URL(url); InputStream stream=null; try { stream = tarURL.openStream(); } catch(Exception e) { throw new Exception("Cannot open URL '"+url+"'."); } try { untarInputStream(stream,url.endsWith("z")); } catch(Exception e) { if(stream != null) stream.close(); throw e; } } // Files and directories from the tar stream are put into the destination directory. public static void untarInputStream(InputStream stream, boolean zip) throws Exception { TarInputStream inputStream=null; OutputStream outputStream=null; long totalBytes = 0; long printBytes = 0; int nErrors=0; try { if(zip) inputStream = new TarInputStream(new GZIPInputStream(stream)); else inputStream = new TarInputStream(stream); // For each entry in the tar, extract and save the entry to the file system TarEntry entry; while (null != (entry = inputStream.getNextEntry())) { // for each entry to be extracted int bytesRead; // if the entry is a directory, create the directory if (entry.isDirectory()) { File fileOrDir = new File(destination+"/"+entry.getName()); fileOrDir.mkdirs(); if(!fileOrDir.isDirectory()) { ++nErrors; System.err.printf("ERROR: cannot create directory '%s'\n", destination+"/"+entry.getName()); } if(verbose) System.out.println("Created dir " + entry.getName()); continue; } // write to file byte[] buf = new byte[4096]; outputStream = new FileOutputStream(destination+"/"+entry.getName()); if(outputStream == null) { ++nErrors; System.err.printf("ERROR: cannot create file '%s'\n", destination+"/"+entry.getName()); } else try { while ((bytesRead = inputStream.read(buf, 0, buf.length)) > -1) { outputStream.write(buf, 0, bytesRead); totalBytes += bytesRead; if(totalBytes > printBytes+10000) { printBytes = totalBytes; status.setText(""+totalBytes); } } if(verbose) System.out.println("Extracted " + entry.getName()); } catch(Exception e) { ++nErrors; System.err.printf("ERROR: while writing '%s'\n", destination+"/"+entry.getName()); } finally { outputStream.close(); } }// while } catch (Exception e) { e.printStackTrace(); throw new Exception("An error occurred while extracting file. Error " + e.getMessage()); } finally { // close I/O streams if (inputStream != null) try { inputStream.close(); } catch (IOException e) { } if (outputStream != null) try { outputStream.close(); } catch (IOException e) { } } if(nErrors == 0) status.setText("Complete."); else status.setText("Errors: "+nErrors); } public static void main(String[] args) throws Exception { //untarURL(args[0]); new G4blData(); } };