JarInputStream类用于从任何输入流读取JAR文件的内容。它扩展了java类。util。拉链ZipInputStream支持读取可选清单条目。清单可用于存储有关JAR文件及其条目的元信息。 建设者
null
- JarInputStream(InputStream in): 创建一个新的JarInputStream并读取可选清单。
- JarInputStream(InputStream in,布尔验证): 创建一个新的JarInputStream并读取可选清单。
方法:
- 受保护的ZipEntry createZipEntry(字符串名称): 为指定的JAR文件条目名创建一个新的JarEntry(ZipEntry)。指定JAR文件条目名称的清单属性将复制到新的JAR条目中。
- Manifest getManifest(): 返回此JAR文件的清单,如果没有,则返回null。
Syntax :public Manifest getManifest() Returns: the Manifest for this JAR file, or null if none.
- ZipEntry getNextEntry(): 读取下一个ZIP文件条目,并将流定位在条目数据的开头。如果已启用验证,则在为下一个条目定位流时检测到的任何无效签名将导致异常。
Syntax :public ZipEntry getNextEntry() throws IOException Overrides: getNextEntry in class ZipInputStream Returns: 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 Returns: the next JAR file entry, or null if there are no more entries Throws: ZipException IOException SecurityException
- int read(字节[]b,int off,int len): 从当前JAR文件项读取字节数组。如果len不为零,则该方法将阻塞,直到有一些输入可用;否则,不读取字节,返回0。如果已启用验证,则当前条目上的任何无效签名将在到达条目末尾之前的某个时间点报告。
Syntax :public int read(byte[] b, int off, int len) throws IOException Overrides: read in class ZipInputStream Parameters: 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 Returns: 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]
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END