Generally, our project jar package uses the jar program that comes with java, or uses IDE such as eclipse build. xml, or ant for packaging.
In some cases, dynamic packaging is required. For example, you can package the unique information or adaptation to the jar according to the client request. Below is an example program of read/write I wrote, no exception handling and multithreading are added. This is basically related to the business.
The Code is as follows:
Read Jar
Package test;
Import java. io. FileInputStream;
Import java. util. Iterator;
Import java. util. jar. Attributes;
Import java. util. jar. JarInputStream;
Import java. util. jar. Manifest;
Import java.util.zip. ZipEntry;
/**
* Read the test program
* @ Author pony
* @ Blog http://pony.cnblogs.com
* @ Date 2010-02-24
* Problem: the entry file name may contain garbled characters if unicode is used.
*/
Public class JarReadTest {
/**
* Write the file to the jar package.
* In the example, read a file and store the file in the jar package.
* Create a new file at the same time
* @ Param inputFileName
* @ Param outputFileName
* @ Throws Exception
*/
Public static void Readjar (String inputFileName) throws Exception {
JarInputStream in = new JarInputStream (new FileInputStream (inputFileName ));
Manifest manifest = in. getManifest ();
Attributes atts = manifest. getMainAttributes ();
// Enter all manifest information
Iterator ite = atts. keySet (). iterator ();
While (ite. hasNext ())
{
Object key = ite. next ();
System. out. println (key + ":" + atts. getValue (key. toString ()));
}
ZipEntry entry = null;
While (entry = in. getNextEntry ())! = Null)
{
// Enter the name of each file
System. out. println (entry. getName ());
Byte [] buffer = new byte [1024];
Int read;
While (read = in. read (buffer ))! =-1)
{// Output file content
System. out. println (new String (buffer ));
}
In. closeEntry ();
}
In. close ();
}
Public static void main (String [] args ){
Try {
Readjar ("c :\\ package. jar ");
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Write jar
Package test;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. util. jar. JarEntry;
Import java. util. jar. JarOutputStream;
Import java. util. jar. Manifest;
/**
* Testing program for packaging the target file
* @ Author pony
* @ Blog http://pony.cnblogs.com
* @ Date 2010-02-24
* Problem: the entry file name may contain garbled characters if unicode is used.
*/
Public class JarWriteTest {
/**
* Write the file to the jar package.
* In the example, read a file and store the file in the jar package.
* Create a new file at the same time
* @ Param inputFileName
* @ Param outputFileName
* @ Throws Exception
*/
Public static void Writejar (String inputFileName, String outputFileName) throws Exception {
// Mainifest is a jar package-specific description file, which cannot be written manually.
// It can help you implement the META-INF directory to save a file named MANIFEST. MF, record version, Entry Program and other information
Manifest manifest = new Manifest ();
Manifest. getMainAttributes (). putValue ("Manifest-Version", "1.0 ");
Manifest. getMainAttributes (). putValue ("author", "pony ");
Manifest. getMainAttributes (). putValue ("blog", "http://pony.cnblogs.com ");
// JarOutputStream and JarInputStream are encapsulated streams when jar packages are generated.
File outfile = new File (outputFileName );
JarOutputStream out = new JarOutputStream (new FileOutputStream (outfile), manifest );
File f = new File (inputFileName );
/************************ Write the input file to the jar outputstream ******* ***************************/
// JarEntry is a jar package Directory class
JarEntry entry = new JarEntry (new String ("file1". getBytes (), "UTF-8 "));
// Add the directory to the out
Out. putNextEntry (entry );
FileInputStream in = new FileInputStream (f );
Byte [] buffer = new byte [1024];
Int n = in. read (buffer );
While (n! =-1 ){
Out. write (buffer, 0, n );
N = in. read (buffer );
}
In. close ();
Out. closeEntry (); // close the Directory
// Create another file
JarEntry entry2 = new JarEntry ("file 2 ");
Out. putNextEntry (entry2 );
Out. write ("Hello! ". GetBytes ());
Out. closeEntry ();
Out. flush ();
// Close the output file stream
Out. close ();
}
Public static void main (String [] args ){
Try {
Writejar ("c :\\ 1.txt"," c :\\ package. jar ");
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}