以下是一个Java代码示例,可以实现将位于“/data/local/bootanimation.zip”路径下的Zip包解压并读取:

import java.io.*;
import java.util.zip.*;

public class UnzipExample {

    public static void main(String[] args) throws IOException {
        String zipFilePath = "/data/local/bootanimation.zip";
        String destDirectory = "/data/local/unzip";
        unzip(zipFilePath, destDirectory);
    }

    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }

        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();

        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                extractFile(zipIn, filePath);
            } else {
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }

    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[4096];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

上述代码中,我们使用了Java的ZipInputStream和ZipEntry类,以及File和FileOutputStream类来读取和解压Zip包。具体来说,我们首先指定Zip文件路径和目标解压目录,然后创建一个ZipInputStream对象,并使用getNextEntry()方法来获取压缩包中的每一个文件或目录。接着,我们对于每一个文件或目录,使用extractFile()方法来将其解压到目标目录中。最后,我们在循环结束后关闭ZipInputStream对象。

写一个把

原文地址: https://www.cveoy.top/t/topic/qln 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录