- Sample1-利用Manifest文件读取jar中的文件
1.文件目录
test--
--a.text
--b.gif
2. Menifest文件内容:
Manifest-Version: 1.0
abc: test/a.txt
iconname: test/Anya.jpg
注意:manifest.mf文件最后一行要打一回车
Another Notification:
如果manifest文件内容是:
Manifest-Version: 1.0
Main-Class: com.DesignToolApp
Class-path: lib/client.jar lib/j2ee.jar
在MANIFEST.MF文件的最后,要留两个空行(也就是回车),才可以识别到Class-Path这一行,如果只有一个空行,那么只识别到Main- Class这一行。Class-Path中的库名用空格格开,使用和jar包相对的路径,发布时把jar包和其他用到的类库一起交给用户就可以了。
3.打jar包
test.jar
- Stringiconpath=jar.getManifest().getMainAttributes().getValue("abc");
- InputStreamin=jar.getInputStream(jar.getJarEntry(iconpath));
- //Imageimg=ImageIO.read(in);
- InputStreamReaderisr=newInputStreamReader(in);
- BufferedReaderreader=newBufferedReader(isr);
- Stringline;
- while((line=reader.readLine())!=null){
- System.out.println(line);
- }
- reader.close();
- Sample2,读取JAR 文件列表及各项的名称、大小和压缩后的大小
- publicclassJarFileInfoRead{
- publicstaticvoidmain(Stringargs[])throwsIOException{
- Stringjarpath="d://temp//test.jar";
- JarFilejarFile=newJarFile(jarpath);
- Enumerationenu=jarFile.entries();
- while(enu.hasMoreElements()){
- process(enu.nextElement());
- }
- }
- privatestaticvoidprocess(Objectobj){
- JarEntryentry=(JarEntry)obj;
- Stringname=entry.getName();
- longsize=entry.getSize();
- longcompressedSize=entry.getCompressedSize();
- System.out.println(name+"\t"+size+"\t"+compressedSize);
- }
- }
- Sample3,读取JAR中 文件的内容
- publicclassJarFileRead{
- publicstaticvoidmain(Stringargs[])
- throwsIOException{
- Stringjarpath="d://temp//test.jar";
- JarFilejarFile=newJarFile(jarpath);
- Enumerationenu=jarFile.entries();
- while(enu.hasMoreElements()){
- JarEntryentry=(JarEntry)enu.nextElement();
- Stringname=entry.getName();
- //System.out.println(name);
- if(name.equals("test/a.txt")){
- InputStreaminput=jarFile.getInputStream(entry);
- process(input);
- }
- }
- jarFile.close();
- }
- privatestaticvoidprocess(InputStreaminput)
- throwsIOException{
- InputStreamReaderisr=
- newInputStreamReader(input);
- BufferedReaderreader=newBufferedReader(isr);
- Stringline;
- while((line=reader.readLine())!=null){
- System.out.println(line);
- }
- reader.close();
- }
- }