
ai.h2o.mojos.runtime.api.backend.ZipFileReaderBackend Maven / Gradle / Ivy
package ai.h2o.mojos.runtime.api.backend;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Access to zip-compressed fileset */
public class ZipFileReaderBackend implements ReaderBackend {
private static final Logger log = LoggerFactory.getLogger(ZipFileReaderBackend.class);
private final ZipFile zipFile;
private final ResourceInfo.Cache cache = new ResourceInfo.Cache();
private ZipFileReaderBackend(ZipFile zipFile) {
this.zipFile = zipFile;
}
public static ReaderBackend open(File zippedMojoFile) throws IOException {
log.info("Opening mojo file: {}", zippedMojoFile);
return new ZipFileReaderBackend(new ZipFile(zippedMojoFile));
}
@Override
public ResourceInfo getResourceInfo(final String resourceName) throws IOException {
ResourceInfo info = cache.get(resourceName);
if (info == null) {
final ZipEntry entry = zipFile.getEntry(resourceName);
if (entry == null) {
throw new FileNotFoundException(resourceName);
}
info = new ResourceInfo(entry.getSize(), "ZIPCRC:" + entry.getCrc());
}
return info;
}
@Override
public InputStream getInputStream(String resourceName) throws IOException {
final ZipEntry entry = zipFile.getEntry(resourceName);
if (entry == null) {
throw new FileNotFoundException(resourceName);
}
return zipFile.getInputStream(entry);
}
@Override
public boolean exists(String resourceName) {
return zipFile.getEntry(resourceName) != null;
}
@Override
public Collection list() {
final List result = new ArrayList<>(zipFile.size());
final Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
result.add(zipEntry.getName());
}
return result;
}
@Override
public void close() throws IOException {
zipFile.close();
}
@Override
public String toString() {
return String.format("%s[%s]", getClass().getSimpleName(), zipFile.getName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy