2021-11-26 16:04:31 +01:00
|
|
|
package eu.openaire.urls_worker.util;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.*;
|
2021-12-03 03:09:40 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-11-26 16:04:31 +01:00
|
|
|
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<String> filesToZip, String baseDirectory)
|
|
|
|
{
|
2021-12-03 03:09:40 +01:00
|
|
|
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.
|
2021-11-26 16:04:31 +01:00
|
|
|
|
2021-12-03 03:09:40 +01:00
|
|
|
File zipFile = new File(zipFilename);
|
|
|
|
try ( ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile), StandardCharsets.UTF_8) )
|
|
|
|
{
|
|
|
|
for ( String file : filesToZip ) {
|
2021-11-26 16:04:31 +01:00
|
|
|
zipAFile(file, zos, baseDirectory);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2021-12-03 03:09:40 +01:00
|
|
|
logger.error("Exception when creating the zip-file: " + zipFilename, e);
|
2021-11-26 16:04:31 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return zipFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-03 03:09:40 +01:00
|
|
|
private static final int BUFFER_SIZE = 3145728; // 3MB (average fullText-size)
|
|
|
|
private static final byte[] dataBuffer = new byte[BUFFER_SIZE];
|
|
|
|
|
|
|
|
// This method is "synchronized" to avoid any future problems with shared-buffer, if the requests are asynchronous.
|
|
|
|
private static synchronized boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir)
|
2021-11-26 16:04:31 +01:00
|
|
|
{
|
|
|
|
String fullFileName = baseDir + fileName;
|
2021-12-03 03:09:40 +01:00
|
|
|
try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullFileName), BUFFER_SIZE) ) {
|
2021-11-26 16:04:31 +01:00
|
|
|
zos.putNextEntry(new ZipEntry(fileName));
|
|
|
|
int count;
|
2021-12-03 03:09:40 +01:00
|
|
|
while ( (count = bis.read(dataBuffer, 0, BUFFER_SIZE)) != -1 ) {
|
|
|
|
zos.write(dataBuffer, 0, count);
|
2021-11-26 16:04:31 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|