當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.jar.JarInputStream用法及代碼示例

JarInputStream 類用於從任何輸入流讀取 JAR 文件的內容。它擴展了 java.util.zip.ZipInputStream 類,支持讀取可選的 Manifest 條目。 Manifest 可用於存儲有關 JAR 文件及其條目的元信息。
構造函數

  • JarInputStream(輸入流中):創建新的 JarInputStream 並讀取可選清單。
  • JarInputStream(InputStream in, 布爾驗證):創建新的 JarInputStream 並讀取可選清單。

方法:

  • protected ZipEntry createZipEntry(字符串名稱):為指定的 JAR 文件條目名稱創建新的 JarEntry (ZipEntry)。指定的 JAR 文件條目名稱的清單屬性將複製到新的 JarEntry。
  • 清單getManifest():返回此 JAR 文件的清單,如果沒有,則返回 null。
    Syntax :public Manifest getManifest()
    返回:
    the Manifest for this JAR file, or null if none.
  • ZipEntry getNextEntry():讀取下一個 ZIP 文件條目並將流定位在條目數據的開頭。如果已啟用驗證,則在定位下一個條目的流時檢測到的任何無效簽名都將導致異常。
    Syntax :public ZipEntry getNextEntry()
                          throws IOException
    Overrides:
    getNextEntry in class ZipInputStream
    返回:
    the next ZIP file entry, or null if there are no more entries
    Throws:
    ZipException 
    IOException 
    SecurityException
  • JarEntry getNextJarEntry():讀取下一個 JAR 文件條目並將流定位在條目數據的開頭。如果已啟用驗證,則在定位下一個條目的流時檢測到的任何無效簽名都將導致異常。
    Syntax :public JarEntry getNextJarEntry()
                             throws IOException
    返回:
    the next JAR file entry, or null if there are no more entries
    Throws:
    ZipException 
    IOException 
    SecurityException
  • int 讀取(字節[] b,int 關閉,int len):從當前 JAR 文件條目讀取到字節數組中。如果 len 不為零,則該方法將阻塞,直到某些輸入可用為止;否則,不會讀取任何字節並返回 0。如果已啟用驗證,則在到達條目末尾之前的某個時間點將報告當前條目上的任何無效簽名。
    Syntax :public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Overrides:
    read in class ZipInputStream
    參數:
    b - the buffer into which the data is read
    off - the start offset in the destination array b
    len - the maximum number of bytes to read
    返回:
    the actual number of bytes read, or -1 if the end of the entry is reached
    Throws:
    NullPointerException 
    IndexOutOfBoundsException 
    ZipException 
    IOException 
    SecurityException 

//Java program demonstrating JarInputStream methods 
  
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Arrays; 
import java.util.jar.JarInputStream; 
import java.util.zip.ZipEntry; 
  
class JarInputStreamDemo extends JarInputStream 
{ 
  
    public JarInputStreamDemo(InputStream in) throws IOException  
    { 
        super(in); 
    } 
    public static void main(String[] args) throws IOException 
    { 
        FileInputStream is = new FileInputStream("codechecker.jar"); 
        JarInputStream jis = new JarInputStream(is); 
        JarInputStreamDemo obj=new JarInputStreamDemo(jis); 
  
        //illustrating createZipEntry() method 
        ZipEntry ze1=obj.createZipEntry("ZipEntry"); 
        System.out.println(ze1.getName()); 
  
        //illustrating getNextEntry() method 
        ZipEntry ze=jis.getNextEntry(); 
        System.out.println(ze.getName()); 
  
        //illustrating getManifest(); 
        System.out.println(jis.getManifest()); 
  
        // Reading from the current JAR file entry 
        // into an array of 10 bytes 
        byte b[] = new byte[10]; 
  
        //illustrating getNextJarEntry() 
        //illustrating read(byte b[],int off,int length) 
        while(jis.getNextJarEntry()!= null) 
            jis.read(b); 
        System.out.print(Arrays.toString(b)); 
  
        //closing the stream 
        jis.close(); 
    } 
} 

輸出:

Zipentry
Attention-64.png
java.util.jar.Manifest@513ee0c5
[-119, 80, 78, 71, 13, 10, 26, 10, 0, 0]


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.util.jar.JarInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。