Java.io.压缩文件


//压缩文件
//先用输入流读取文件,之后再用压缩流输出文件
public static void main(String[] args) throws Exception {
File file = new File("d:" + File.separator + "demo.pdf");// 源文件
File zipFile = new File("d:" + file.separator + "1.zip");// 被压缩文件的路径

InputStream fileReader = new FileInputStream(file);// 需要读取源文件
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));

// 每一个压缩文件的子文件都用ZipEntry表示
// 需要为每一个压缩的文件,设置名称
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
zipOutputStream.setComment("这是一个压缩的pdf");// 压缩文件的注释

int temp = 0;
while ((temp = fileReader.read()) != -1) {// 读取内容
zipOutputStream.write(temp);// 压缩输出内容
}
fileReader.close();//关闭输入流
zipOutputStream.close();//关闭压缩流
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值