how to extract jarfile

Discussion in 'Java' started by JerryXiao, Oct 3, 2005.

  1. JerryXiao

    JerryXiao Guest

    Hello,

    my goal is to download a jar file from a webserver and extract it to
    local disk programmatically using java.
    through JarUrlConnection you can get JarFile, however could find any
    way to extract the Jarfile to local disk. BTW the jar file could
    contain many files and they should be extracted to the local directory
    as needed.

    Any clue how to do that?


    Thanks
     
    JerryXiao, Oct 3, 2005
    #1
    1. Advertising

  2. JerryXiao

    Rationem Guest

    1. Advertising

  3. JerryXiao

    Roedy Green Guest

    On 3 Oct 2005 14:01:50 -0700, "JerryXiao" <>
    wrote or quoted :

    >my goal is to download a jar file from a webserver and extract it to
    >local disk programmatically using java.
    >through JarUrlConnection you can get JarFile, however could find any
    >way to extract the Jarfile to local disk. BTW the jar file could
    >contain many files and they should be extracted to the local directory
    >as needed.


    If you are using an Applet, it needs to be signed. If you are using
    JAWS your installer might contain code like this:

    This code unpacks a jar called dicts.jar.

    Inside are files called kasxejo.dict nekonato.dict etc.

    It opens them with getResourceAsStream and then writes the contents
    to local disk with FileOutputStream.

    the advantage of using JAWS is your jar is automatically on the
    classpath and JAWS assigns you some permanent storage space.

    The strange names are in Esperanto.

    If you don't like this code, download the Replicator. It contains code
    to download jars itself and unpack them itself. Look in the class
    com.mindprod.replicator.Fetch

    See http://mindprod.com/products1.html#REPLICATOR


    --------------------------------------------------

    package com.mindprod.esper;
    import javax.jnlp.DownloadService;
    import javax.jnlp.ServiceManager;

    /**
    * run once on install to unpack the jar files
    */
    public class Installer {

    /**
    * main class for the installer, unpacks dictionaries from jars to
    separate
    * files.
    *
    * @param args
    * not used
    */
    public static void main ( String[] args )
    {

    // copy from jar to separate file:

    // cache of words previously looked up
    LocalDict kasxejo;

    // words we tried looking up before and failed
    LocalDict nekonato;

    // local dictionary, from Roedy, Geneva, Ergane
    LocalDict vortaro;

    // cache of words previously looked up
    LocalDict cache;

    // words we tried looking up before and failed
    LocalDict unknown;

    // local dictionary from Roedy, Geneva, Ergane
    LocalDict mini;

    // prefixes, not currently used
    LocalDict prefiksoj;

    // suffixes, not currently used
    LocalDict sufiksoj;



    System.out.println( "unpacking dicts.jar file..." );

    kasxejo = new LocalDict( "kasxejo.dict", true );
    kasxejo.open();
    kasxejo.save();
    kasxejo.close();

    nekonato = new LocalDict( "nekonato.dict", true );
    nekonato.open();
    nekonato.save();
    nekonato.close();

    vortaro = new LocalDict( "vortaro.dict", true );
    vortaro.open();
    vortaro.save();
    vortaro.close();

    unknown = new LocalDict( "unknown.dict", true );
    unknown.open();
    unknown.save();
    unknown.close();

    cache = new LocalDict( "cache.dict", true );
    cache.open();
    cache.save();
    cache.close();

    mini = new LocalDict( "mini.dict", true );
    mini.open();
    mini.save();
    mini.close();

    prefiksoj = new LocalDict( "prefiksoj.dict", true );
    prefiksoj.open();
    prefiksoj.save();
    prefiksoj.close();

    sufiksoj = new LocalDict( "sufiksoj.dict", true );
    sufiksoj.open();
    sufiksoj.save();
    sufiksoj.close();

    try
    {
    DownloadService ds = (DownloadService)ServiceManager
    .lookup( "javax.jnlp.DownloadService" );
    // no longer need associated Jar file
    ds.removeResource( new java.net.URL( "dicts.jar" ), null
    );
    }
    // all kinds could go wrong, UnavailableServiceException,
    // MalformedURLException, IOExeception,
    // no web start classes available.
    catch ( Exception e )
    {
    }
    }
    }

    ---------------------------------------


    package com.mindprod.esper;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io_OutputStream;
    import java.util.Properties;

    /**
    * locally resident dictionary, implemented as a Property Hashtable
    */
    public class LocalDict implements Lookup

    {

    /**
    * have there been any changes to the in-RAM copy of the dict
    since it was
    * loaded?
    */
    boolean changed = false;

    /**
    * name of dictionary on disk or in jar, e.g. vortaro.dict
    */
    String dictName;

    /**
    * Property Hastable to hold the dictionary in RAM
    */
    Properties h;

    /**
    * true if the dictionary data can be found in the jar file, false
    in the
    * default directory.
    */
    boolean inJar;

    /**
    * Save any changes to this dictionary, and disconnect from it.
    */
    public void close ()
    {
    if ( changed )
    {
    save();
    }
    h = null;
    } // end close

    /**
    * Add a definition to the dictionary.
    *
    * @param word
    * The word to be defined in the source language. If word
    already
    * exists in dictionary, new definition replaces.
    * @param definition
    * The defininition of the word in the target language. If
    null,
    * removes existing definition.
    */
    public void define ( String word, String definition )
    {
    if ( !inJar )
    { // would not be able to save back to the jar.
    h.put( word, definition );
    changed = true;
    }
    }

    /**
    * Is it possible to define new words in this dictionary?
    *
    * @return true if you can add definitions, false otherwise.
    */
    public boolean definePossible ()
    {
    return !inJar;
    }

    /**
    * Looks up a word in a dictionary. It does not do prefix or
    suffix
    * splitting to help find words. That is the responsibility of
    other levels.
    *
    * @param word
    * Word to translate. Lead and trailing blanks removed.
    accents
    * rendered with trailing x convention, at least for now.
    Eventually
    * will switch to unicode.
    * @return word or phrase for the word translated, possibly with
    valid HTML,
    * but no excess garbage characters. null if no
    translation found.
    */
    public String lookup ( String word )
    {
    return (String)h.get( word );
    }

    /**
    * Open a connection to this dictionary.
    */
    public void open ()
    {
    h = new Properties();
    try
    {

    InputStream is;

    if ( inJar )
    {
    // look in jar
    is = LocalDict.class.getResourceAsStream( dictName );
    }
    else
    { // look in default directory
    is = new FileInputStream( dictName );
    }

    if ( is != null )
    {
    // load entire dictionary into RAM
    h.load( is );
    is.close();
    }
    }
    catch ( IOException e )
    {
    // just have an empty dictionary
    return;
    }
    } // end open

    /**
    * save dictionary in a separate file on disk, whether or not it
    has
    * changed.
    */
    public void save ()
    {
    try
    {
    OutputStream os = new FileOutputStream( dictName );
    if ( os != null )
    {
    // save entire dictionary
    h.store( os, null );
    os.close();
    }
    }
    catch ( IOException e )
    {
    System.err.println( "unable to save " + dictName );
    }
    }

    /**
    * public constructor
    *
    * @param dictName
    * name of dictionary on disk.
    * @param inJar
    * true if dictionary data can be found in the jar, false
    if in the
    * current directory.
    */
    public LocalDict( String dictName, boolean inJar )
    {
    this.dictName = dictName;
    this.inJar = inJar;
    }

    } // end LocalDict

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pasci

    JarFile Problem

    Pasci, Oct 16, 2003, in forum: Java
    Replies:
    1
    Views:
    346
    Paul Lutus
    Oct 17, 2003
  2. Ola Rennemo

    webservice client jarfile

    Ola Rennemo, May 12, 2004, in forum: Java
    Replies:
    0
    Views:
    343
    Ola Rennemo
    May 12, 2004
  3. No6
    Replies:
    7
    Views:
    431
  4. Kannan

    JarFile to Javadoc

    Kannan, May 13, 2005, in forum: Java
    Replies:
    1
    Views:
    428
    bugbear
    May 13, 2005
  5. learner9
    Replies:
    3
    Views:
    19,840
    learner9
    Apr 6, 2006
Loading...

Share This Page