com.adobe.platform.operation.internal.service.ExtractDataZipper Maven / Gradle / Ivy
The newest version!
package com.adobe.platform.operation.internal.service;
import com.adobe.platform.operation.internal.cpf.dto.StructuredData;
import com.adobe.platform.operation.internal.cpf.dto.response.ExtractPDFOutput;
import com.adobe.platform.operation.internal.cpf.dto.response.ExtractRenditionOutput;
import com.adobe.platform.operation.internal.util.FileUtil;
import com.adobe.platform.operation.internal.util.PathUtil;
import com.adobe.platform.operation.internal.util.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ExtractDataZipper {
private static void zipExtractedRenditions(ZipOutputStream zipOutputStream,
List extractRenditionOutputs) throws IOException {
if(CollectionUtils.isNotEmpty(extractRenditionOutputs)) {
for (ExtractRenditionOutput extractRenditionOutput : extractRenditionOutputs) {
String fileName = PathUtil.getFileNameWithExtension(extractRenditionOutput.getFileName(),
extractRenditionOutput.getRenditionExtension());
String folderName = "renditions";
if (StringUtil.isBlank(fileName)) {
fileName = FileUtil.getRandomFileName(extractRenditionOutput.getRenditionExtension());
}
if (extractRenditionOutput.getPdfElementType() != null) {
folderName = extractRenditionOutput.getPdfElementType().name().toLowerCase();
}
fileName = PathUtil.getSubPath(folderName, fileName);
FileUtil.addZipEntry(zipOutputStream, fileName, extractRenditionOutput.getInputStream());
}
}
}
private static void zipExtractedStructuredData(ZipOutputStream zipOutputStream, StructuredData structuredData)
throws IOException {
String fileName = structuredData.getFileName();
FileUtil.addZipEntry(zipOutputStream, fileName, structuredData.getData().toString().getBytes());
}
public static void zipExtractOutput(ExtractPDFOutput extractPDFOutput, String destinationPath) throws IOException {
ZipOutputStream zipOutputStream = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(destinationPath);
zipOutputStream = new ZipOutputStream(fileOutputStream);
zipExtractedStructuredData(zipOutputStream, extractPDFOutput.getStructuredElementData());
zipExtractedRenditions(zipOutputStream, extractPDFOutput.getExtractedRenditions());
} catch (IOException ex) {
throw ex;
} finally{
if (zipOutputStream != null) {
zipOutputStream.close();
}
}
}
}