Cant extract downloaded zip file using java

血红的双手。 提交于 2020-04-30 06:57:25

问题


I need to download a zip file from url, save it and extract it for getting data.

So i had a method for download zip file using Rest Template. When i download data and save file as a zip, i can extract zip file when right click and extract but i cant extract using java. i tried zip4j and java util zip but both of them throw an exception.

With zip4j i get: zip headers not found. probably not a zip file with ZipException.

My Sample code for download and save data as zip:

    String body = gson.toJson(new Request());

    RequestCallback requestCallback = request -> {
        request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
        request.getBody().write(body.getBytes());
    };

    ResponseExtractor<Void> responseExtractor = response -> {
        Path path = Paths.get(System.getProperty("user.home") + "/Desktop/LiableList/execute.zip");
        Files.copy(response.getBody(), path);
        return null;
    };

    template.execute(url, HttpMethod.POST, requestCallback, responseExtractor);

extract process with zip4j:

  ZipFile file = new ZipFile("C:/Users/ALTPRO/Desktop/LiableList/execute.zip");
  file.extractAll("C:/Users/ALTPRO/Desktop/LiableList");

Thanks for your responses.

来源:https://stackoverflow.com/questions/61271801/cant-extract-downloaded-zip-file-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!