use of net.lingala.zip4j.model.ZipParameters in project apex-core by apache.
the class StramTestSupport method createConfigPackageFile.
/**
* Create an confPackage zip using the sample confPackage located in
* src/test/resources/testConfPackage/testConfPackageSrc.
*
* @param file The file whose path will be used to create the confPackage zip
* @return The File object that can be used in the ConfigPackage constructor.
* @throws net.lingala.zip4j.exception.ZipException
*/
public static File createConfigPackageFile(File file) throws net.lingala.zip4j.exception.ZipException {
ZipFile zipFile = new ZipFile(file);
ZipParameters zipParameters = new ZipParameters();
zipParameters.setIncludeRootFolder(false);
zipFile.createZipFileFromFolder("src/test/resources/testConfigPackage/testConfigPackageSrc", zipParameters, false, Long.MAX_VALUE);
return file;
}
use of net.lingala.zip4j.model.ZipParameters in project ddf by codice.
the class ExportCommand method writeToZip.
private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, ExportItem exportItem, ResourceResponse resource) throws ZipException {
ZipParameters parameters = new ZipParameters();
parameters.setSourceExternalStream(true);
String id = exportItem.getId();
String path = getContentPath(id, resource);
parameters.setFileNameInZip(path);
zipFile.addStream(resource.getResource().getInputStream(), parameters);
}
use of net.lingala.zip4j.model.ZipParameters in project ddf by codice.
the class ExportCommand method writeToZip.
private void writeToZip(/*Mutable,IO*/
ZipFile zipFile, Result result) {
ZipParameters parameters = new ZipParameters();
parameters.setSourceExternalStream(true);
String id = result.getMetacard().getId();
parameters.setFileNameInZip(Paths.get("metacards", id.substring(0, 3), id, "metacard", id + ".xml").toString());
try {
BinaryContent binaryMetacard = transformer.transform(result.getMetacard(), Collections.emptyMap());
zipFile.addStream(binaryMetacard.getInputStream(), parameters);
} catch (ZipException e) {
LOGGER.error("Error processing result and adding to ZIP", e);
throw new CatalogCommandRuntimeException(e);
} catch (CatalogTransformerException e) {
LOGGER.warn("Could not transform metacard. Metacard will not be added to zip [{}]", result.getMetacard().getId());
console.printf("%sCould not transform metacard. Metacard will not be included in export. %s - %s%s%n", Ansi.ansi().fg(Ansi.Color.RED).toString(), result.getMetacard().getId(), result.getMetacard().getTitle(), Ansi.ansi().reset().toString());
}
}
use of net.lingala.zip4j.model.ZipParameters in project ddf by codice.
the class ZipCompression method transform.
/**
* Transforms a SourceResponse with a list of {@link Metacard}s into a {@link BinaryContent} item
* with an {@link InputStream}. This transformation expects a key-value pair "fileName"-zipFileName to be present.
*
* @param upstreamResponse - a SourceResponse with a list of {@link Metacard}s to compress
* @param arguments - a map of arguments to use for processing. This method expects "fileName" to be set
* @return - a {@link BinaryContent} item with the {@link InputStream} for the Zip file
* @throws CatalogTransformerException when the transformation fails
*/
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (upstreamResponse == null || CollectionUtils.isEmpty(upstreamResponse.getResults())) {
throw new CatalogTransformerException("No Metacards were found to transform.");
}
if (MapUtils.isEmpty(arguments) || !arguments.containsKey(ZipDecompression.FILE_PATH)) {
throw new CatalogTransformerException("No 'filePath' argument found in arguments map.");
}
ZipFile zipFile;
String filePath = (String) arguments.get(ZipDecompression.FILE_PATH);
try {
zipFile = new ZipFile(filePath);
} catch (ZipException e) {
LOGGER.debug("Unable to create zip file with path : {}", filePath, e);
throw new CatalogTransformerException(String.format("Unable to create zip file at %s", filePath), e);
}
List<Result> resultList = upstreamResponse.getResults();
Map<String, Resource> resourceMap = new HashMap<>();
resultList.stream().map(Result::getMetacard).forEach(metacard -> {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setSourceExternalStream(true);
zipParameters.setFileNameInZip(METACARD_PATH + metacard.getId());
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(new MetacardImpl(metacard));
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
zipFile.addStream(inputStream, zipParameters);
if (hasLocalResources(metacard)) {
resourceMap.putAll(getAllMetacardContent(metacard));
}
} catch (IOException | ZipException e) {
LOGGER.debug("Failed to add metacard with id {}.", metacard.getId(), e);
}
});
resourceMap.forEach((filename, resource) -> {
try {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setSourceExternalStream(true);
zipParameters.setFileNameInZip(filename);
zipFile.addStream(resource.getInputStream(), zipParameters);
} catch (ZipException e) {
LOGGER.debug("Failed to add resource with id {} to zip.", resource.getName(), e);
}
});
BinaryContent binaryContent;
try {
InputStream fileInputStream = new ZipInputStream(new FileInputStream(zipFile.getFile()));
binaryContent = new BinaryContentImpl(fileInputStream);
jarSigner.signJar(zipFile.getFile(), System.getProperty("org.codice.ddf.system.hostname"), System.getProperty("javax.net.ssl.keyStorePassword"), System.getProperty("javax.net.ssl.keyStore"), System.getProperty("javax.net.ssl.keyStorePassword"));
} catch (FileNotFoundException e) {
throw new CatalogTransformerException("Unable to get ZIP file from ZipInputStream.", e);
}
return binaryContent;
}
use of net.lingala.zip4j.model.ZipParameters in project ArachneCentralAPI by OHDSI.
the class AnalysisHelper method compressAndSplit.
public void compressAndSplit(ArrayList<File> files, File zipArchive) {
try {
ZipFile zipFile = new ZipFile(zipArchive.getAbsoluteFile());
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.createZipFile(files, parameters, true, maximumSize);
} catch (ZipException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new ConverterRuntimeException(ex.getMessage());
}
}
Aggregations