java 從jar中讀取文件 三種方法

  • 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

Java代碼  收藏代碼
  1. String iconpath = jar.getManifest().getMainAttributes().getValue("abc");  
  2. InputStream in = jar.getInputStream(jar.getJarEntry(iconpath));  
  3.  //Image img = ImageIO.read(in);  
  4. InputStreamReader isr =  new InputStreamReader(in);  
  5. BufferedReader reader = new BufferedReader(isr);  
  6. String line;  
  7. while ((line = reader.readLine()) != null) {  
  8.       System.out.println(line);  
  9. }  
  10. reader.close();  
 

 

  • Sample2,讀取JAR 文件列表及各項的名稱、大小和壓縮後的大小

 

Java代碼  收藏代碼
  1. public class JarFileInfoRead {  
  2.  public static void main (String args[])  throws IOException {  
  3.   String jarpath="d://temp//test.jar";  
  4.   JarFile jarFile = new JarFile(jarpath);  
  5.   Enumeration enu = jarFile.entries();  
  6.   while (enu.hasMoreElements()) {  
  7.       process(enu.nextElement());  
  8.   }  
  9. }  
  10.   
  11. private static void process(Object obj) {  
  12.     JarEntry entry = (JarEntry)obj;  
  13.     String name = entry.getName();  
  14.     long size = entry.getSize();  
  15.     long compressedSize = entry.getCompressedSize();  
  16.     System.out.println(name + "\t" + size + "\t" + compressedSize);  
  17.   }  
  18. }  
 

 

  • Sample3,讀取JAR中 文件的內容
Java代碼  收藏代碼
  1. public class JarFileRead {  
  2.     public static void main (String args[])  
  3.         throws IOException {  
  4.       String jarpath="d://temp//test.jar";  
  5.       JarFile jarFile = new JarFile(jarpath);  
  6.         Enumeration enu = jarFile.entries();  
  7.         while (enu.hasMoreElements()) {  
  8.          JarEntry entry = (JarEntry)enu.nextElement();  
  9.             String name = entry.getName();  
  10.             //System.out.println(name);  
  11.             if(name.equals("test/a.txt")){  
  12.             InputStream input = jarFile.getInputStream(entry);  
  13.             process(input);  
  14.             }  
  15.         }         
  16.         jarFile.close();  
  17.     }  
  18.     private static void process(InputStream input)  
  19.         throws IOException {  
  20.         InputStreamReader isr =  
  21.             new InputStreamReader(input);  
  22.         BufferedReader reader = new BufferedReader(isr);  
  23.         String line;  
  24.         while ((line = reader.readLine()) != null) {  
  25.             System.out.println(line);  
  26.         }  
  27.         reader.close();  
  28.     }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章