Search in sources :

Example 6 with ZipParameters

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;
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 7 with ZipParameters

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);
}
Also used : ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 8 with ZipParameters

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());
    }
}
Also used : ZipException(net.lingala.zip4j.exception.ZipException) CatalogCommandRuntimeException(org.codice.ddf.commands.util.CatalogCommandRuntimeException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 9 with ZipParameters

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;
}
Also used : HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ZipException(net.lingala.zip4j.exception.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) ObjectOutputStream(java.io.ObjectOutputStream) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FileInputStream(java.io.FileInputStream) Result(ddf.catalog.data.Result) ZipParameters(net.lingala.zip4j.model.ZipParameters) ZipInputStream(java.util.zip.ZipInputStream) ZipFile(net.lingala.zip4j.core.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 10 with ZipParameters

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());
    }
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) ZipException(net.lingala.zip4j.exception.ZipException) ConverterRuntimeException(com.odysseusinc.arachne.portal.exception.ConverterRuntimeException) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Aggregations

ZipParameters (net.lingala.zip4j.model.ZipParameters)10 ZipFile (net.lingala.zip4j.core.ZipFile)8 ZipException (net.lingala.zip4j.exception.ZipException)3 BinaryContent (ddf.catalog.data.BinaryContent)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 File (java.io.File)2 IOException (java.io.IOException)2 SysUploadVO (com.netsteadfast.greenstep.vo.SysUploadVO)1 ConverterRuntimeException (com.odysseusinc.arachne.portal.exception.ConverterRuntimeException)1 Result (ddf.catalog.data.Result)1 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Resource (ddf.catalog.resource.Resource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ArrayList (java.util.ArrayList)1