Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

how to extract jarfile

Thread view: 
JerryXiao - 03 Oct 2005 22:01 GMT
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?
ericshai1900@yahoo.ca

Thanks
Rationem - 04 Oct 2005 00:17 GMT
Use the API for jar files....

http://java.sun.com/j2se/1.5.0/docs/api/java/util/jar/JarFile.html

which extends:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipFile.html
Roedy Green - 04 Oct 2005 09:01 GMT
>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

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.