java closeutils_java.util.zip.ZipOutputStream.close()方法示例

java.util.zip.ZipOutputStream.close()方法关闭ZIP输出流以及被过滤的流。

声明

以下是java.util.zip.ZipOutputStream.close()方法的声明。

public void close() throws IOException

异常

ZipException – 如果发生ZIP文件错误。

IOException – 如果发生I/O错误。

前提条件

在D:/test>目录下用以下内容创建一个文件Hello.txt。

This is an example.

示例

以下示例显示了java.util.zip.ZipOutputStream.close()方法的用法。

package com.yiibai; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.zip.Adler32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipOutputStreamDemo { private static String SOURCE_FILE = "D:\test\Hello.txt"; private static String TARGET_FILE = "D:\test\Hello.zip"; public static void main(String[] args) { try { createZipFile(); readZipFile(); } catch(IOException ioe) { System.out.println("IOException : " + ioe); } } private static void createZipFile() throws IOException{ FileOutputStream fout = new FileOutputStream(TARGET_FILE); CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32()); ZipOutputStream zout = new ZipOutputStream(checksum); FileInputStream fin = new FileInputStream(SOURCE_FILE); ZipEntry zipEntry = new ZipEntry(SOURCE_FILE); zout.putNextEntry(zipEntry); int length; byte[] buffer = new byte[1024]; while((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); fin.close(); zout.close(); } private static void readZipFile() throws IOException{ ZipInputStream zin = new ZipInputStream(new FileInputStream(TARGET_FILE)); ZipEntry entry; while((entry = zin.getNextEntry())!=null){ System.out.printf("File: %s Modified on %TD %n", entry.getName(), new Date(entry.getTime())); extractFile(entry, zin); System.out.printf("Zip file %s extracted successfully.", SOURCE_FILE); zin.closeEntry(); } zin.close(); } private static void extractFile(final ZipEntry entry, ZipInputStream is) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(entry.getName()); while(is.available() != 0){ fos.write(is.read()); } } catch (IOException ioex) { fos.close(); } } }

执行上面示例代码,得到以下结果 –

Zip File: D:testHello.zip, Contains 1 file(s). File: D:testHello.txt Size 1026 Modified on 05/22/17 Zip file D:testHello.txt extracted successfully.

¥ 我要打赏   纠错/补充 收藏

哥,这回真没有了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值