java解压zip代码_java解压zip文件的代码

这段代码展示了如何使用Java进行ZIP文件的解压缩。通过ZipInputStream读取ZIP文件,遍历每个条目,如果是目录则创建对应文件夹,如果是文件则将其写入到指定的输出路径。
摘要由CSDN通过智能技术生成

java解压zip文件的代码

/**

* 解压zip格式的压缩包

*

* @param filePath

*            压缩文件路径

* @param outPath

*            输出路径

* @return 解压成功或失败标志

*/

public static Boolean unZip(String filePath, String outPath) {

String unzipfile = inPath; // 解压缩的文件名

try {

ZipInputStream zin = new ZipInputStream(new FileInputStream(

unzipfile));

ZipEntry entry;

// 创建文件夹

while ((entry = zin.getNextEntry()) != null) {

if (entry.isDirectory()) {

File directory = new File(outPath, entry.getName());

if (!directory.exists()) {

if (!directory.mkdirs()) {

System.exit(0);

}

}

zin.closeEntry();

} else {

File myFile = new File(entry.getName());

FileOutputStream fout = new FileOutputStream(outPath

+ myFile.getPath());

DataOutputStream dout = new DataOutputStream(fout);

byte[] b = new byte[1024];

int len = 0;

while ((len = zin.read(b)) != -1) {

dout.write(b, 0, len);

}

dout.close();

fout.close();

zin.closeEntry();

}

}

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值