zipentry java_java中的ZipEntry是什么意思?

展开全部

ZipEntry 类是java.util.zip包下的一个类,

ZipEntry 类用于表示 ZIP 文件条32313133353236313431303231363533e58685e5aeb931333337396230目。

利用这个类压缩和解压zip文件

具体压缩的例子如下:import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

* 压缩程序

* @author young

*

*/

public class SingleFileZip {

public static void main(String[] args) {

File file = new File("e:/test.txt");

FileInputStream fis = null;

ZipOutputStream zos = null;

try {

fis = new FileInputStream(file);

zos = new ZipOutputStream(new FileOutputStream("e:/my.zip"));

// 创建压缩文件中的条目

ZipEntry entry = new ZipEntry(file.getName());

// 将创建好的条目加入到压缩文件中

zos.putNextEntry(entry);

// 写入当前条目所对应的具体内容

byte[] buff = new byte[1024];

int len = 0;

while ((len = fis.read(buff)) != -1) {

zos.write(buff, 0, len);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

fis.close();

zos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

解压例子如下:import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

/**

* 解压程序

* @author young

*

*/

public class SingleFileUnZip {

public static void main(String[] args) {

FileOutputStream fos = null;

ZipInputStream zis = null;

InputStream is = null;

try {

ZipFile zf = new ZipFile("e:/my.zip");

zis = new ZipInputStream(new FileInputStream("e:/my.zip"));

fos = new FileOutputStream("e:/unzip.txt");

// 从压缩文件中获取一个条目

ZipEntry entry = zis.getNextEntry();

// 获得该条目对象的数据流

is = zf.getInputStream(entry);

byte[] buff = new byte[1024];

int len = 0;

while ((len = is.read(buff)) != -1) {

fos.write(buff, 0, len);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

is.close();

zis.close();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值