此类实现了一个输出流过滤器,用于以ZIP文件格式写入文件。包括对压缩和未压缩项的支持。 建造师:
null
- ZipoutStream(OutputStream out): 创建一个新的ZIP输出流。
- ZipOutputStream(OutputStream out,Charset Charset): 创建一个新的ZIP输出流。
方法:
- void close(): 关闭ZIP输出流以及正在过滤的流。
Syntax :public void close() throws IOException Overrides: close in class DeflaterOutputStream Throws: ZipException IOException
- void closeEntry(): 关闭当前ZIP条目并定位流以写入下一个条目。
Syntax :public void closeEntry() throws IOException Throws: ZipException IOException
- void finish(): 在不关闭底层流的情况下完成ZIP输出流内容的写入。对同一输出流连续应用多个过滤器时,请使用此方法。
Syntax :public void finish() throws IOException Overrides: finish in class DeflaterOutputStream Throws: ZipException IOException
- 无效Putnestry(ZipEntry e): 开始写入新的ZIP文件条目,并将流定位到条目数据的开头。如果仍处于活动状态,则关闭当前条目。如果没有为条目指定压缩方法,将使用默认压缩方法;如果条目没有设置修改时间,将使用当前时间。
Syntax :public void putNextEntry(ZipEntry e) throws IOException Parameters: e - the ZIP entry to be written Throws: ZipException IOException
- void setComment(字符串注释): 设置ZIP文件注释。
Syntax :public void setComment(String comment) Parameters: comment - the comment string Throws: IllegalArgumentException
- 无效设置级别(整数级别): 为随后被压缩的条目设置压缩级别。默认设置为默认压缩。
Syntax :public void setLevel(int level) Parameters: level - the compression level (0-9) Throws: IllegalArgumentException
- void setMethod(int方法): 设置后续条目的默认压缩方法。只要未为单个ZIP文件条目指定压缩方法,并且最初设置为DEFLATED,就会使用此默认值。
Syntax :public void setMethod(int method) Parameters: method - the default compression method Throws: IllegalArgumentException
- 无效写入(字节[]b,int off,int len) :将字节数组写入当前ZIP条目数据。此方法将阻塞,直到写入所有字节。
Syntax :public void write(byte[] b, int off, int len) Parameters: b - the data to be written off - the start offset in the data len - the number of bytes that are written Throws: ZipException IOException
节目:
//Java program demonstrating ZipOutputStream methods import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; class ZipOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream( "zipfile" ); ZipOutputStream zos = new ZipOutputStream(fos); //illustrating setMethod() zos.setMethod( 8 ); //illustrating setLevel method zos.setLevel( 5 ); ZipEntry ze1 = new ZipEntry( "ZipEntry1" ); //illustrating putNextEntry method zos.putNextEntry(ze1); //illustrating setComment zos.setComment( "This is my first comment" ); //illustrating write() for ( int i = 0 ; i < 10 ; i++) zos.write(i); //illustrating write(byte b[], int off, int len) byte b[] = { 11 , 12 , 13 }; zos.write(b); //illustrating closeEntry() zos.closeEntry(); //Finishes writing the contents of the ZIP output stream // without closing the underlying stream zos.finish(); //closing the stream zos.close(); FileInputStream fin = new FileInputStream( "zipfile" ); ZipInputStream zin = new ZipInputStream(fin); //Reads the next ZIP file entry ZipEntry ze = zin.getNextEntry(); //the name of the entry. System.out.println(ze.getName()); //illustrating getMethod System.out.println(ze.getMethod()); //Reads up to byte.length bytes of data from this input stream // into an array of bytes. byte c[] = new byte [ 13 ]; if (zin.available() == 1 ) zin.read(c); System.out.print(Arrays.toString(c)); //closes the current ZIP entry zin.closeEntry(); //closing the stream zin.close(); } } |
输出:
ZipEntry1 8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13]
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END