Java Java类
此类实现了一个输入流过滤器,用于读取 ZIP 文件格式的文件。包括对压缩和未压缩条目的支持。
构造函数:
- ZipInputStream(InputStream in) :创建一个新的 ZIP 输入流。
- ZipInputStream(InputStream in, Charset charset) :创建一个新的 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
- protected ZipEntry createZipEntry(String name) :为指定的条目名称创建一个新的 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(byte[] 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
- long skip(long 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]