Apache Lucene open-source search software
LUCENE-5945: Nuke one more ZipFile usage
Uwe Schindler737acac742a7
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.javaindex ca07da982f9..bc19b5ef36d 100644--- a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java+++ b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java@@ -295,8 +295,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {oldIndexDirs = new HashMap<>();for (String name : names) {Path dir = createTempDir(name);- Path dataFile = Paths.get(TestBackwardsCompatibility.class.getResource("index." + name + ".zip").toURI());- TestUtil.unzip(dataFile, dir);+ TestUtil.unzip(TestBackwardsCompatibility.class.getResourceAsStream("index." + name + ".zip"), dir);oldIndexDirs.put(name, newFSDirectory(dir));}}@@ -435,7 +434,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {System.out.println("TEST: index " + unsupportedNames[i]);}Path oldIndexDir = createTempDir(unsupportedNames[i]);- TestUtil.unzip(getDataPath("unsupported." + unsupportedNames[i] + ".zip"), oldIndexDir);+ TestUtil.unzip(getDataInputStream("unsupported." + unsupportedNames[i] + ".zip"), oldIndexDir);BaseDirectoryWrapper dir = newFSDirectory(oldIndexDir);// don't checkindex, these are intentionally not supporteddir.setCheckIndexOnClose(false);@@ -1086,8 +1085,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {try {for (String name : oldIndexDirs.keySet()) {Path dir = createTempDir(name);- Path dataFile = Paths.get(TestBackwardsCompatibility.class.getResource("index." + name + ".zip").toURI());- TestUtil.unzip(dataFile, dir);+ TestUtil.unzip(getDataInputStream("index." + name + ".zip"), dir);String path = dir.toAbsolutePath().toString();@@ -1197,7 +1195,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {public void testMoreTerms() throws Exception {Path oldIndexDir = createTempDir("moreterms");- TestUtil.unzip(getDataPath(moreTermsIndex), oldIndexDir);+ TestUtil.unzip(getDataInputStream(moreTermsIndex), oldIndexDir);Directory dir = newFSDirectory(oldIndexDir);// TODO: more testsTestUtil.checkIndex(dir);@@ -1236,7 +1234,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {public void testDocValuesUpdates() throws Exception {Path oldIndexDir = createTempDir("dvupdates");- TestUtil.unzip(getDataPath(dvUpdatesIndex), oldIndexDir);+ TestUtil.unzip(getDataInputStream(dvUpdatesIndex), oldIndexDir);Directory dir = newFSDirectory(oldIndexDir);verifyDocValues(dir);diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.javaindex 6a4162b3c0f..d3c55388b78 100644--- a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java+++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java@@ -344,7 +344,7 @@ public class TrecContentSourceTest extends LuceneTestCase {*/public void testTrecFeedDirAllTypes() throws Exception {Path dataDir = createTempDir("trecFeedAllTypes");- TestUtil.unzip(getDataPath("trecdocs.zip"), dataDir);+ TestUtil.unzip(getDataInputStream("trecdocs.zip"), dataDir);TrecContentSource tcs = new TrecContentSource();Properties props = new Properties();props.setProperty("print.props", "false");diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex b20fd2cf668..d9dd7c99963 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -20,6 +20,7 @@ package org.apache.lucene.util;import java.io.Closeable;import java.io.FileNotFoundException;import java.io.IOException;+import java.io.InputStream;import java.io.PrintStream;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;@@ -138,6 +139,7 @@ import org.junit.Test;import org.junit.rules.RuleChain;import org.junit.rules.TestRule;import org.junit.runner.RunWith;+import com.carrotsearch.randomizedtesting.JUnit4MethodProvider;import com.carrotsearch.randomizedtesting.LifecycleScope;import com.carrotsearch.randomizedtesting.MixWithSuiteName;@@ -1673,6 +1675,14 @@ public abstract class LuceneTestCase extends Assert {}}+ protected InputStream getDataInputStream(String name) throws IOException {+ try {+ return this.getClass().getResourceAsStream(name);+ } catch (Exception e) {+ throw new IOException("Cannot find resource: " + name);+ }+ }+public void assertReaderEquals(String info, IndexReader leftReader, IndexReader rightReader) throws IOException {assertReaderStatisticsEquals(info, leftReader, rightReader);assertFieldsEquals(info, leftReader, MultiFields.getFields(leftReader), MultiFields.getFields(rightReader), true);diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.javaindex 49e675b5667..1b723f77308 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java@@ -38,7 +38,7 @@ import java.util.concurrent.TimeUnit;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;import java.util.zip.ZipEntry;-import java.util.zip.ZipFile;+import java.util.zip.ZipInputStream;import org.apache.lucene.codecs.Codec;import org.apache.lucene.codecs.DocValuesFormat;@@ -99,39 +99,33 @@ public final class TestUtil {/*** Convenience method unzipping zipName into destDir, cleaning up- * destDir first.+ * destDir first.+ * Closes the given InputStream after extracting!*/- public static void unzip(Path zipName, Path destDir) throws IOException {+ public static void unzip(InputStream in, Path destDir) throws IOException {IOUtils.rm(destDir);Files.createDirectory(destDir);- ZipFile zipFile = new ZipFile(zipName.toFile());- Enumeration<? extends ZipEntry> entries = zipFile.entries();-- while (entries.hasMoreElements()) {- ZipEntry entry = entries.nextElement();-- InputStream in = zipFile.getInputStream(entry);- Path targetFile = destDir.resolve(entry.getName());-- // be on the safe side: do not rely on that directories are always extracted- // before their children (although this makes sense, but is it guaranteed?)- Files.createDirectories(targetFile.getParent());- if (!entry.isDirectory()) {- OutputStream out = Files.newOutputStream(targetFile);+ try (ZipInputStream zipInput = new ZipInputStream(in)) {+ ZipEntry entry;+ while ((entry = zipInput.getNextEntry()) != null) {+ Path targetFile = destDir.resolve(entry.getName());- byte[] buffer = new byte[8192];- int len;- while((len = in.read(buffer)) >= 0) {- out.write(buffer, 0, len);+ // be on the safe side: do not rely on that directories are always extracted+ // before their children (although this makes sense, but is it guaranteed?)+ Files.createDirectories(targetFile.getParent());+ if (!entry.isDirectory()) {+ OutputStream out = Files.newOutputStream(targetFile);+ byte[] buffer = new byte[8192];+ int len;+ while((len = zipInput.read(buffer)) >= 0) {+ out.write(buffer, 0, len);+ }+ out.close();}-- in.close();- out.close();+ zipInput.closeEntry();}}-- zipFile.close();}public static void syncConcurrentMerges(IndexWriter writer) {