Java代码解压ZIP文件:将'/data/local/bootanimation.zip'解压缩到指定目录
// 定义待解压的文件路径 final String backgroundPath = '/data/local/bootanimation.zip';
// 创建ZipInputStream对象 ZipInputStream zis = new ZipInputStream(new FileInputStream(backgroundPath));
// 遍历Zip包中的文件 ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // 如果当前文件是目录,则跳过 if (entry.isDirectory()) { continue; }
// 获取当前文件的名称和路径
String name = entry.getName();
String path = '/data/local/' + name;
// 创建文件输出流,用于写入当前文件
FileOutputStream fos = new FileOutputStream(path);
// 读取当前文件,并写入到输出流中
byte[] buffer = new byte[1024];
int len = 0;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
// 关闭输出流
fos.close();
}
// 关闭输入流 zis.close();
// 解压完成,输出提示信息 System.out.println('解压完成!');
// 上述代码实现了从指定路径的zip包中解压文件,并将解压后的文件放置到指定目录下。其中,ZipInputStream是Java中用于读取Zip包的类,ZipEntry代表Zip包中的一个文件或目录,FileOutputStream是Java中用于写入文件的类。在遍历Zip包中的文件时,如果当前文件是目录,则跳过;否则,获取当前文件的名称和路径,创建文件输出流,读取当前文件,并写入到输出流中,最后关闭输出流。
原文地址: https://www.cveoy.top/t/topic/lxhe 著作权归作者所有。请勿转载和采集!