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

com.softicar.platform.common.io.zip.entry.ZipFileEntryIterator Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.io.zip.entry;

import com.softicar.platform.common.core.exceptions.SofticarIOException;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileEntryIterator implements Iterator {

	private final ZipFile zipFile;
	private final Enumeration entries;
	private boolean onlyFiles;
	private ZipFileEntry next;

	public ZipFileEntryIterator(File file) {

		this.zipFile = openZipFile(file);
		this.entries = zipFile.entries();
		this.onlyFiles = false;
		this.next = fetchNext();
	}

	public ZipFileEntryIterator setOnlyFiles(boolean onlyFiles) {

		this.onlyFiles = onlyFiles;
		return this;
	}

	@Override
	public boolean hasNext() {

		if (next == null) {
			closeZipFile();
		}

		return next != null;
	}

	@Override
	public ZipFileEntry next() {

		ZipFileEntry entry = next;
		this.next = fetchNext();
		return entry;
	}

	private ZipFileEntry fetchNext() {

		ZipFileEntry current = null;
		while (entries.hasMoreElements()) {
			current = new ZipFileEntry(zipFile, entries.nextElement());
			if (isAccepted(current)) {
				return current;
			}
		}
		return null;
	}

	private boolean isAccepted(ZipFileEntry entry) {

		return !(onlyFiles && entry.isDirectory());
	}

	private static ZipFile openZipFile(File file) {

		try {
			return new ZipFile(file);
		} catch (IOException exception) {
			throw new SofticarIOException(exception);
		}
	}

	private void closeZipFile() {

		try {
			zipFile.close();
		} catch (IOException exception) {
			throw new SofticarIOException(exception);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy