有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

文件如何使用Java下载和解压缩zip存档?

我为自己和我的一个朋友做了一个小JPanel工具,上面有图像图标。它从用户的“图像”文件夹中加载图像。home,但我希望它在打开时检查该目录是否存在,如果不存在,下载包含该文件夹的zip存档,并将其解压缩给用户。家有些人告诉我这根本不可能,但我不这么认为。我就是想不出办法。有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    实际上非常简单,只需在项目中添加Apache Commons IO和zip4j作为依赖项,以便使用FileUtils和Zip实用程序

    你可以用Maven或者任何你想用的东西

    只需将所需内容分为三步,检查目录是否存在,如果不下载文件,则将其解压缩即可

    String home = System.getProperty("user.home");
    File imagesPath = new File(home + "/Images");
    boolean exists = imagesPath.exists();
    if (!exists) {
        // create directory
        imagesPath.mkdir();
        // download
        String zipPath = home + "/Images.zip";
        FileUtils.copyURLToFile(new URL("http://url/Images.zip"), new File(zipPath));
        // unzip
        try {
             new ZipFile(zipPath).extractAll(home + "/Images");
        } catch (ZipException e) {
             // do something useful
             e.printStackTrace();
        }
    }