global.namespace.archive.io.commons.compress.ZipFileAdapter 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.ArchiveEntrySink;
import global.namespace.archive.io.api.ArchiveEntrySource;
import global.namespace.archive.io.api.ArchiveFileInput;
import global.namespace.fun.io.api.Socket;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
/**
* Adapts a {@link ZipFile} to an {@link ArchiveFileInput}.
*
* @author Christian Schlichtherle
*/
final class ZipFileAdapter implements ArchiveFileInput {
private final ZipFile zip;
ZipFileAdapter(final ZipFile input) { this.zip = requireNonNull(input); }
public Iterator> iterator() {
return new Iterator>() {
final Enumeration en = zip.getEntries();
public boolean hasNext() { return en.hasMoreElements(); }
public ArchiveEntrySource next() { return source(en.nextElement()); }
};
}
public Optional> source(String name) {
return Optional.ofNullable(zip.getEntry(name)).map(this::source);
}
private ArchiveEntrySource source(ZipArchiveEntry entry) {
return new ArchiveEntrySource() {
public String name() { return entry.getName(); }
public boolean isDirectory() { return entry.isDirectory(); }
public ZipArchiveEntry entry() { return entry; }
public Socket input(ArchiveEntrySink> sink) { return input(); }
public Socket input() { return () -> zip.getInputStream(entry); }
};
}
@Override
public void close() throws IOException { zip.close(); }
}