Using Spring To Download a Zip File, Extract It, and Upload It to Cloud Storage Without Storing Files Locally in the Container
There are use cases in which, as part of the integration work, you might need to download the zip file from one of your partners, extract the zip file content and then move the extracted files to cloud storage. We had a similar need to download the uploaded ID (Driver’s license) images (Front/Back) from one of the leading ID verification service providers, which persists in the organization’s cloud storage. The challenge lies in downloading a zip file, extracting its contents, and uploading them to cloud storage — all in a transient manner without creating any temporary files on the container of your microservice.
Downloading the Zipped File Content From Third-Party Service
Below is the code snippet to get the zipped file from the partner service.
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set(“Content-Type”, “application/zip”);
headers.set(“Authorization”, “Bearer ” + accessToken);
HttpEntity request = new HttpEntity(headers);
String url = this.baseUrl + “/documents/” + documentUUID + “?imagequality=original”;
ResponseEntity<Resource> response = restTemplate.exchange(url, HttpMethod.GET, request, Resource.class);
Resource zipFileContent = response.getBody();
HashMap<String, byte[]> map = new HashMap<>();
if (zipFileContent != null) {
ZipInputStream zipInputStream = new ZipInputStream(zipFileContent.getInputStream());
ZipEntry zipEntry = null;
byte[] buff = new byte[4096];
while ((zipEntry = zipInputStream.getNextEntry()) != null)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// consume all the data from this entry
while (zipInputStream.available() > 0) {
int byteLength = 0;
while ((byteLength = zipInputStream.read(buff)) > 0) {
byteArrayOutputStream.write(buff, 0, byteLength);
}
}
byteArrayOutputStream.close();
map.put(zipEntry.getName(), byteArrayOutputStream.toByteArray());
}
zipInputStream.close();
}
for (String fileName :
map.keySet()) {
byte[] fileBytes = map.get(fileName);
ByteArrayResource byteArrayResource = this.getDocumentByteArray(fileBytes, fileName);
}
private ByteArrayResource getDocumentByteArray(byte[] bytes, String fileName) {
try {
final ByteArrayResource byteArrayResource = new ByteArrayResource(bytes) {
@Override
public String getFilename() {
return fileName;
}
};
return byteArrayResource;
} catch (Exception ex) {
logger.error(“Exception – getDocumentByteArray – Error while getting the uploaded images byte array content, detail error : “, ex);
}
return null;
}