package vin.jar;
/**
Saturday, February 17, 2001
Manipulate jar (and zip) files programmatically
----------------------------------------------
What do we want?
1. create new empty jarfile (easy)
public static File create(String path)
2. add entry to this file
public static void add(File jarFile, File jarEntryFile)
3. delete entry from this file (work in progress)
public static void delete(File jarFile, File jarEntry)
4. update an entry in this file (work in progress)
public static void update(File jarFile, File jarEntry)
5. get an entry (work in progress)
public static File get(File jarFile, String jarEntryName)
*/
import java.io.*;
import java.util.jar.*;
import java.util.zip.*;
class Jar{
/* testfile: test1.txt
*/
static String testFilePath ="src/vin/jar";
public static void main( String args[] ){
//create new empty jarfile
File jarFile = create(testFilePath + "/" + "test.jar");
//add entry to this file
try{
File jarEntryFile = new File( testFilePath + "/" + "test1.txt" );
add(jarFile, jarEntryFile );
} catch (Exception ioe){
System.out.println("Error creating jarEntryFile in main()");
ioe.printStackTrace();
}
}
/** Adds a file to a jar file */
public static void add(File jarFile, File jarEntryFile){
String name =null;
try{
// open jos to jar file
JarOutputStream jos = new JarOutputStream( new FileOutputStream(jarFile) );
// read bytes of file to be jarred
long len = jarEntryFile.length();
byte ar[] = new byte[(int) len];
System.out.println("Entry-length before jar=" + len);
DataInputStream dis = new DataInputStream( new FileInputStream(jarEntryFile));
dis.readFully( ar, 0, ar.length);
// create new jar entry
// ie include path to the file name, like:
//name = aRelativePath + "/" + jarEntryFile.getName();
name = jarEntryFile.getName();
JarEntry jarEntry = new JarEntry( name );
// put into jar file
jos.putNextEntry( jarEntry );
// write bytes to open entry of jar file
jos.write( ar, 0, ar.length);
// close jos
jos.close();
} catch (Exception ioe){
System.out.println("IOError writing to jarfile");
ioe.printStackTrace();
}
}
/** Creates new empty jarfile or overwrites existing one */
public static File create(String path){
File jarFile=null;
try{
jarFile = new File( path );
JarOutputStream jos = new JarOutputStream( new FileOutputStream(jarFile) );
} catch (Exception ioe){
System.out.println("Error creating jarfile");
ioe.printStackTrace();
}
return jarFile;
}
public static File get(File file, String jarEntryName){
if((null==jarEntryName) || (jarEntryName.length()<1) ){
System.out.println("Error Jar.get() - received invalid jar entry name with 2nd argument");
return null;
}
if ( (null==file) || (!file.exists()) ){
System.out.println("Error Jar.get() - received invalid file with 1st argument");
return null;
}
File jarEntryFile=null;
JarInputStream jis =null;
ZipEntry je=null; //JarEntry je = null;
try{
jis = new JarInputStream( new FileInputStream(file) );
if ( (null==jis) || (0==jis.available())){
System.out.println("Error Jar.get() - could not get valid JarInputStream from " + file.getName());
return null;
}
JarFile jf = new JarFile(file);
je = jf.getEntry(jarEntryName) ;
long len = je.getSize(); //the uncompressed size of the entry data, or -1 if not known
InputStream is = jf.getInputStream(je); // positions cursor at 1st byte of je
jis = new JarInputStream(is);
byte ar[] = new byte[(int) len];
jis.read(ar, 0, ar.length);
System.out.println("Array length:" + ar.length);
/*in progress:
file has invalid byte format,
use different File/IO
find out
*/
RandomAccessFile raf = new RandomAccessFile(je.getName(), "rw");
//raf.write(ar, 0, ar.length);
raf.writeChars( new String(ar,"UTF-8"));
raf.close();
} catch(IOException ioe){
System.out.println("Error getting entry from jarEntryFile in get()");
ioe.printStackTrace(System.out);
} catch (Exception e){
System.out.println("Error (unforeseen) getting entry from jarEntryFile in get()");
e.printStackTrace(System.out);
} finally{
try{
if (null!=jis) jis.close();
} catch (Throwable t){}
}
return jarEntryFile;
}
// to be implemented yet
// -------------------------------------
// -------------------------------------
public static void delete(File jarFile, File jarEntry){
}
public static void update(File jarFile, File jarEntry){
}
}
*** Release all IDE prisoners now! Use TextPad! ***