![JAR search and dependency download from the Maven repository](/logo.png)
eu.infomas.annotation.ZipFileIterator Maven / Gradle / Ivy
Show all versions of annotation-detector Show documentation
/* ZipFileIterator.java
*
* Created: 2011-10-10 (Year-Month-Day)
* Character encoding: UTF-8
*
****************************************** LICENSE *******************************************
*
* Copyright (c) 2011 - 2013 XIAM Solutions B.V. (http://www.xiam.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.infomas.annotation;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* {@code ZipFileIterator} is used to iterate over all entries in a {@code zip} or {@code jar}
* file and returning the {@link InputStream} of these entries.
*
* The most efficient way of iterating is used, see benchmark in test classes.
*
* @author Ronald K. Muller
* @since annotation-detector 3.0.0
*/
final class ZipFileIterator {
private final ZipFile zipFile;
private final Enumeration extends ZipEntry> entries;
private ZipEntry current;
ZipFileIterator(final File file) throws IOException {
zipFile = new ZipFile(file);
entries = zipFile.entries();
}
public ZipEntry getEntry() {
return current;
}
public InputStream next() throws IOException {
while (entries.hasMoreElements()) {
current = entries.nextElement();
if (!current.isDirectory()) {
return zipFile.getInputStream(current);
}
}
// no more entries in this ZipFile, so close ZipFile
try {
// zipFile is never null here
zipFile.close();
} catch (IOException ex) { // SUPPRESS CHECKSTYLE EmptyBlockCheck
// suppress IOException, otherwise close() is called twice
}
return null;
}
}