All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.freefair.gradle.plugins.compress.tasks.GZip Maven / Gradle / Ivy

There is a newer version: 8.11
Show newest version
package io.freefair.gradle.plugins.compress.tasks;

import lombok.Getter;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipParameters;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

import java.io.BufferedOutputStream;
import java.io.IOException;

@Getter
public abstract class GZip extends CompressorTask {

    @Input
    @Optional
    public abstract Property getCompressionLevel();

    @Input
    @Optional
    public abstract Property getComment();

    @Input
    public abstract Property getAddFilename();

    public GZip() {
        getFileExtension().convention("gz");
    }

    @Override
    protected Class getWorkAction() {
        return GZipWorkAction.class;
    }

    @Override
    protected void fillParameters(GZipWorkParameters parameters) {
        parameters.getCompressionLevel().set(getCompressionLevel());
        parameters.getComment().set(getComment());
        parameters.getAddFilename().set(getAddFilename());
    }

    public interface GZipWorkParameters extends CompressorWorkParameters {
        Property getCompressionLevel();

        Property getComment();

        Property getAddFilename();
    }

    public static abstract class GZipWorkAction extends CompressorWorkAction {

        @Override
        protected GzipCompressorOutputStream createOutputStream(BufferedOutputStream outputStream) throws IOException {
            GzipParameters parameters = new GzipParameters();

            if (getParameters().getCompressionLevel().isPresent()) {
                parameters.setCompressionLevel(getParameters().getCompressionLevel().get());
            }

            if (getParameters().getComment().isPresent()) {
                parameters.setComment(getParameters().getComment().get());
            }

            if (getParameters().getAddFilename().getOrElse(false)) {
                parameters.setFilename(getParameters().getSourceFile().getAsFile().get().getName());
            }

            return new GzipCompressorOutputStream(outputStream, parameters);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy