解压zip文件到当前目录

这篇博客介绍了如何使用Java进行zip文件的解压缩操作。通过ZipFile类迭代zip档案中的条目,对每个条目创建目标文件并写入解压内容。过程包括读取条目、创建目标文件、写入数据并关闭流,从而完成解压到特定目录的任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static boolean unZipFile(String pathName, String toPath)
{
File desFile = null;
try
{
Log.v("unZipFile", "unzip start");
@SuppressWarnings("resource")
ZipFile zf = new ZipFile(pathName);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();)
{
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.isDirectory())
{
continue;
}
InputStream in = zf.getInputStream(entry);
String strEntry = entry.getName();
String strEntryFilename = strEntry.substring(strEntry.lastIndexOf("/") + 1);
String filePath = toPath + File.separator + strEntryFilename;
desFile = new File(filePath);
OutputStream out = new FileOutputStream(desFile);
byte buffer2[] = new byte[1024 * 1024];
int len = -1;
while ((len = in.read(buffer2)) != -1)
{
out.write(buffer2, 0, len);
Log.v("unZipFile", "unZipFile len =" + len);
}
out.close();
in.close();
//FileUtils.renameFile(desFile.getAbsolutePath(), filePath);
}
Log.v("unZipFile", "unzip finish");
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
finally
{
//FileUtils.deleteFile(new File(pathName));
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值