public ResponseEntity<byte[]> downloadFolderAsZip(@RequestParam("id") Long id) throws IOException, InvalidKeyException,
InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException,
InternalException, XmlParserException, ErrorResponseException {
{
// Set the response headers for the ZIP file
List<File> files = folderRepository.findAllByParentFolderById(id);
List<Folder> folders = folderRepository.findAllByParentFolder(id);
Folder folderById = folderRepository.findById(id).get();
// Create ByteArrayOutputStream to hold ZIP file contents
ByteArrayOutputStream zipBytes = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(zipBytes);
// Iterate over each file and add to ZIP
for (com.numeryx.minioaccess.model.File file : files) {
String fileName = file.getName();
String filePath = file.getPath();
// Get file from Minio
InputStream inputStream = ConnectedUserMinioSession.minioClient.getObject(
GetObjectArgs.builder()
.bucket("reports")
.object(file.getPathUuid())
.build());
// Add file to ZIP
zipOut.putNextEntry(new ZipEntry(fileName));
IOUtils.copy(inputStream, zipOut);
zipOut.closeEntry();
inputStream.close();
}
if (!folders.isEmpty()) {
for (Folder folder : folders) {
addFolderToZip(zipOut, folder.getName() ,folder.getId(),"");
}
}
// Close ZIP stream
zipOut.finish();
zipOut.close();
// Set HTTP response headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "folder.zip");
// Return ZIP file as byte array
return new ResponseEntity<>(zipBytes.toByteArray(), headers, HttpStatus.OK);
}
}
private void addFolderToZip(ZipOutputStream zipOut, String folderName,Long id,String folderSupName) throws IOException,
InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException,
ServerException, InternalException, XmlParserException, ErrorResponseException {
if (folderSupName != "") {
String[] value = folderSupName.split("/", 4);
// Create a new ZIP entry for the folder
zipOut.putNextEntry(new ZipEntry(value[3] + folderName + "/"));
zipOut.closeEntry();
// Get all files and subfolders in the folder
//List<File> files = fileDBRepository.findAllByParentFolder(folderPathUuid);
List<File> files = folderRepository.findAllByParentFolderById(id);
List<Folder> folders = folderRepository.findAllByParentFolder(id);
Folder folderById = folderRepository.findById(id).get();
// Recursively add all files and subfolders to the ZIP
for (com.numeryx.minioaccess.model.File file : files) {
String fileName = file.getName();
String filePathUuid = file.getPathUuid();
// If the file is not a folder, add it to the ZIP
InputStream inputStream = ConnectedUserMinioSession.minioClient.getObject(
GetObjectArgs.builder()
.bucket("reports")
.object(filePathUuid)
.build());
// Add file to ZIP
zipOut.putNextEntry(new ZipEntry(value[3] + folderName + "/" + fileName));
IOUtils.copy(inputStream, zipOut);
zipOut.closeEntry();
inputStream.close();
}
if (!folders.isEmpty()) {
for (Folder folder : folders) {
addFolderToZip(zipOut, folder.getName(), folder.getId(), folderById.getPath());
}
}
} else {
zipOut.putNextEntry(new ZipEntry(folderSupName + folderName + "/"));
zipOut.closeEntry();
// Get all files and subfolders in the folder
List<File> files = folderRepository.findAllByParentFolderById(id);
List<Folder> folders = folderRepository.findAllByParentFolder(id);
Folder folderById = folderRepository.findById(id).get();
// Recursively add all files and subfolders to the ZIP
for (com.numeryx.minioaccess.model.File file : files) {
String fileName = file.getName();
String filePathUuid = file.getPathUuid();
// If the file is not a folder, add it to the ZIP
InputStream inputStream = ConnectedUserMinioSession.minioClient.getObject(
GetObjectArgs.builder()
.bucket("reports")
.object(filePathUuid)
.build());
// Add file to ZIP
zipOut.putNextEntry(new ZipEntry(folderSupName + folderName + "/" + fileName));
IOUtils.copy(inputStream, zipOut);
zipOut.closeEntry();
inputStream.close();
}
if (!folders.isEmpty()) {
for (Folder folder : folders) {
addFolderToZip(zipOut, folder.getName(), folder.getId(), folderById.getPath());
}
}
}
}
response.setContentType("application/zip");
? – Petronelrb.header("Content-Type", "application/zip");
– Petronel