这个类实现了一个输入流过滤器,用于读取ZIP文件格式的文件。包括对压缩和未压缩项的支持。 施工人员:
null
- ZipInputStream(输入流输入): 创建一个新的ZIP输入流。
- ZipInputStream(输入流输入,字符集字符集): 创建一个新的ZIP输入流
方法:
- int available(): 在EOF达到当前条目的数据后返回0,否则始终返回。 程序不应依赖此方法返回实际字节数 可以在没有阻塞的情况下读取。
Syntax :public int available() throws IOException Overrides: available in class InflaterInputStream Returns: 1 before EOF and 0 after EOF has reached for current entry. Programs should not count on this method to return the actual number of bytes that could be read without blocking. Throws: IOException
- void close(): 关闭此输入流并释放与该流关联的所有系统资源。
Syntax :public void close() throws IOException Overrides: close in class InflaterInputStream Throws: IOException
- void closeEntry(): 关闭当前ZIP条目并定位流以读取下一个条目。
Syntax :public void closeEntry() throws IOException Throws: ZipException IOException
- 受保护的ZipEntry createZipEntry(字符串名称): 为指定的条目名创建新的ZipEntry对象。
Syntax :protected ZipEntry createZipEntry(String name) Parameters: name - the ZIP file entry name Returns: the ZipEntry just created
- ZipEntry getNextEntry(): 读取下一个ZIP文件条目,并将流定位在条目数据的开头。
Syntax :public ZipEntry getNextEntry() throws IOException Returns: the next ZIP file entry, or null if there are no more entries Throws: ZipException IOException
- int read(字节[]b,int off,int len): 将当前ZIP条目读入字节数组。如果len不为零,则该方法将阻塞,直到有一些输入可用;否则,不读取字节,返回0。
Syntax :public int read(byte[] b, int off, int len) throws IOException 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 read Returns: the actual number of bytes read, or -1 if the end of the entry is reached Throws: NullPointerException IndexOutOfBoundsException ZipException IOException
- 长跳跃(长n): 跳过当前ZIP条目中指定的字节数。
Syntax :public long skip(long n) throws IOException Parameters: n - the number of bytes to skip Returns: the actual number of bytes skipped Throws: ZipException IOException IllegalArgumentException
//Java program demonstrating ZipInputStream 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; import java.util.zip.ZipInputStream; class ZipInputStreamDemo extends ZipInputStream { public ZipInputStreamDemo(InputStream in) { super (in); } public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream( "Awesome CV.zip" ); ZipInputStream zis = new JarInputStream(fis); ZipInputStreamDemo obj = new ZipInputStreamDemo(zis); //illustrating createZipEntry() ZipEntry ze = obj.createZipEntry( "ZipEntry" ); System.out.println(ze.getName()); //illustrating getNextEntry() ZipEntry je = zis.getNextEntry(); System.out.println(je.getName()); //illustrating skip() method zis.skip( 3 ); //illustrating closeEntry() method zis.closeEntry(); zis.getNextEntry(); byte b[] = new byte [ 10 ]; //illustrating available() method //Reads up to byte.length bytes of data from this input stream if (zis.available() == 1 ) zis.read(b); System.out.println(Arrays.toString(b)); //closing the stream zis.close(); } } |
输出:
ZipEntry awesome-cv.cls [35, 32, 65, 119, 101, 115, 111, 109, 101, 32]
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END