package eu.openaire.urls_worker.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FilesZipper { private static final Logger logger = LoggerFactory.getLogger(FilesZipper.class); public static File zipMultipleFilesAndGetZip(long assignmentsCounter, int zipBatchCounter, List filesToZip, String baseDirectory) { File zipFile = null; ZipOutputStream zos = null; try { String zipFilename = baseDirectory + "assignments_" + assignmentsCounter + "_full-texts_" + zipBatchCounter + ".zip"; // For example: assignments_2_full-texts_4.zip | where < 4 > is referred to the 4th batch of files requested by the controller. zipFile = new File(zipFilename); zos = new ZipOutputStream(new FileOutputStream(zipFile)); // Iterate over the given full-texts and add them to the zip. for ( String file : filesToZip ) { zipAFile(file, zos, baseDirectory); } } catch (Exception e) { logger.error("", e); return null; } finally { try { if ( zos != null ) zos.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } return zipFile; } private static boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir) { final int BUFFER = 1048576; // 1 MB byte[] data = new byte[BUFFER]; BufferedInputStream bis = null; String fullFileName = baseDir + fileName; try { FileInputStream fis = new FileInputStream(fullFileName); bis = new BufferedInputStream(fis, BUFFER); zos.putNextEntry(new ZipEntry(fileName)); int count; while ( (count = bis.read(data, 0, BUFFER)) != -1 ) { zos.write(data, 0, count); } zos.closeEntry(); // close the entry here (not the ZipOutputStream) } catch (FileNotFoundException fnfe) { logger.error("Error zipping file: " + fullFileName, fnfe.getMessage()); return false; } catch (Exception e) { if ( ! e.getMessage().contains("duplicate") ) logger.error("Error zipping file: " + fullFileName, e); return false; } finally { try { if ( bis != null ) bis.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } return true; } }