Java8 Files和Path 解压zip文件(SpringBoot解压zip文件)

Java8 Files 解压zip文件


vue.zip压缩包内文件结构
在这里插入图片描述
不需要更多解释,全在注释里了。

    /**
     * 解压Zip
     * 代码比较简单,运行后通过打印日志能有更深的理解
     */
    public void unZip() {
        try {
            // 此代码是在SpringBoot项目中摘过来的。sourcePath请根据实际情况设置
            Path sourcePath = Paths.get("static", "dist", "vue.zip");
            FileSystem fs = FileSystems.newFileSystem(sourcePath, (ClassLoader) null);
            // 重点:fs.getPath("/")  具体为什么不清楚,如果有知道的可以底下评论
            Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
                // 此方法返回Zip包内全部文件
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    System.out.println(">>> " + file.toString());  // 日志:>>> /vue/css/app.css
                    if (file.startsWith("/__MACOSX")) {  // 由于我使用Mac生成的Zip包,解压的时候选择过滤掉。
                        System.out.println("过滤:/__MACOSX");
                        return FileVisitResult.CONTINUE;
                    }
                    // 解压到的目录是:static/dist 文件夹下(sourcePath.getParent().toString()="static/dist")
                    // 需求是解压到当前目录下,所以我们要把vue文件夹截取掉(file.subpath(1, file.getNameCount()).toString()="css/app.css")
                    // 如果你是直接压缩的多个文件外层无vue文件夹,不需要截取代码
                    Path target = Paths.get(sourcePath.getParent().toString(), file.subpath(1, file.getNameCount()).toString());
                    System.out.println("目标:" + target.toString());  // 日志:目标:static/dist/css/app.css
                    if (Files.notExists(target.getParent())) {  // 判断是否有css文件夹,如果没有创建文件夹
                        Files.createDirectories(target.getParent());
                    }
                    Files.copy(file, target);  // 最后写入文件
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

点赞哦👍

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Javazip文件并将指定文件类型保存到本地的示例代码: ```java import java.io.*; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipExample { public static void main(String[] args) throws IOException { String zipFilePath = "path/to/zip/file.zip"; // zip文件路径 String fileType = ".txt"; // 要提取的文件类型 String outputFolderPath = "path/to/output/folder"; // 输出文件夹路径 unzipFiles(zipFilePath, fileType, outputFolderPath); } public static void unzipFiles(String zipFilePath, String fileType, String outputFolderPath) throws IOException { File outputFolder = new File(outputFolderPath); if (!outputFolder.exists()) { outputFolder.mkdir(); } InputStream inputStream = new FileInputStream(zipFilePath); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); if (fileName.toLowerCase().endsWith(fileType.toLowerCase())) { String filePath = outputFolderPath + File.separator + fileName; OutputStream outputStream = new FileOutputStream(filePath); byte[] buffer = new byte[1024]; int length; while ((length = zipInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); } zipEntry = zipInputStream.getNextEntry(); } zipInputStream.closeEntry(); zipInputStream.close(); inputStream.close(); } } ``` 在该示例中,我们首先定义了要zip文件的路径,要提取的文件类型和输出文件夹的路径。然后,将这些参数传递给`unzipFiles()`方法,该方法执行以下步骤: 1. 创建输出文件夹(如果还不存在)。 2. 读取zip文件并使用`ZipInputStream`逐个读取其条目。 3. 对于每个zip条目,检查其文件名是否以指定文件类型结尾。 4. 如果文件名匹配,则创建该文件的输出流,并将zip数据写入该文件。 5. 关闭输出流和zip条目。 6. 最后,关闭zip输入流和文件输入流。 请注意,该示例假定zip文件中的条目不包含文件夹,只包含文件。如果zip文件中包含文件夹,则需要相应地调整代码来处理它们。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值