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

global.namespace.archive.io.commons.compress.Compress Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2013-2018 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package global.namespace.archive.io.commons.compress;

import global.namespace.archive.io.api.ArchiveFileInput;
import global.namespace.archive.io.api.ArchiveFileOutput;
import global.namespace.archive.io.api.ArchiveFileStore;
import global.namespace.fun.io.api.Socket;
import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;

import java.io.File;
import java.io.FileOutputStream;

import static java.util.Objects.requireNonNull;

/**
 * Provides access to JAR and ZIP files using Apache Commons Compress.
 *
 * @author Christian Schlichtherle
 */
public class Compress {

    private Compress() { }

    /** Returns an archive file store for the given JAR file. */
    public static ArchiveFileStore jar(final File file) {
        requireNonNull(file);
        return new ArchiveFileStore() {

            @Override
            public Socket> input() {
                return () -> new ZipFileAdapter(new ZipFile(file));
            }

            @Override
            public Socket> output() {
                return () -> new JarArchiveOutputStreamAdapter(new JarArchiveOutputStream(new FileOutputStream(file)));
            }
        };
    }

    /** Returns an archive file store for the given ZIP file. */
    public static ArchiveFileStore zip(final File file) {
        requireNonNull(file);
        return new ArchiveFileStore() {

            @Override
            public Socket> input() {
                return () -> new ZipFileAdapter(new ZipFile(file));
            }

            @Override
            public Socket> output() {
                return () -> new ZipArchiveOutputStreamAdapter(new ZipArchiveOutputStream(file));
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy