zipentry java_ZipEntry getNextEntry()

ZipEntry getNextEntry()

描述 (Description)

java.util.zip.ZipInputStream.getNextEntry()方法读取下一个ZIP文件条目,并将流定位在条目数据的开头。

声明 (Declaration)

以下是java.util.zip.ZipInputStream.getNextEntry()方法的声明。public ZipEntry getNextEntry()

throws IOException

返回值 (Returns)

下一个ZIP文件条目,如果没有更多条目,则返回null。

异常 (Exceptions)ZipException - 如果发生ZIP文件错误。

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

先决条件

在D:》 test 》目录中创建一个文件Hello.txt,其中包含以下内容。This is an example.

例子 (Example)

以下示例显示了java.util.zip.ZipInputStream.getNextEntry()方法的用法。package com.iowiki;

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 ZipInputStreamDemo {

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();

}

}

}

让我们编译并运行上面的程序,这将产生以下结果 -File: D:\test\Hello.txt Modified on 05/22/17

Zip file D:\test\Hello.txt extracted successfully.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值