写一个把 "/data/local/bootanimation.zip"路径下的zip包解压并输出到/data/local/目录下读取的java代码。中文解释
以下是Java代码,实现将位于“/data/local/bootanimation.zip”路径下的zip包解压并输出到“/data/local/”目录下读取。
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipFile {
public static void main(String[] args) {
String zipFilePath = "/data/local/bootanimation.zip";
String outputFolderPath = "/data/local/";
unzip(zipFilePath, outputFolderPath);
}
private static void unzip(String zipFilePath, String outputFolderPath) {
byte[] buffer = new byte[1024];
try {
// 创建zip输入流
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
// 获取zip文件列表
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(outputFolderPath + File.separator + fileName);
// 创建文件目录
new File(newFile.getParent()).mkdirs();
if (zipEntry.isDirectory()) {
// 如果是文件夹,则创建文件夹
newFile.mkdir();
} else {
// 如果是文件,则解压文件
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
int len;
while ((len = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
}
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
System.out.println("解压完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码中的unzip()方法接收两个参数,一个是zip文件的路径,另一个是要输出的文件夹路径。该方法使用ZipInputStream类来读取zip文件,并将其解压到指定目录下。对于每个zip文件条目,如果它是目录,则创建一个目录,否则将其解压到指定目录下。
在main()方法中,我们将zip文件的路径设置为“/data/local/bootanimation.zip”,将输出文件夹的路径设置为“/data/local/”,并调用unzip()方法来执行解压过程。
最后,当解压完成后,控制台将输出“解压完成”。
原文地址: https://www.cveoy.top/t/topic/qkZ 著作权归作者所有。请勿转载和采集!