问题
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