diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java index afe5e56ebd..cc780561df 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java @@ -44,7 +44,11 @@ public final class ZipArchiveFakeEntry extends ZipArchiveEntry implements Closea private static int MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE; public static void setMaxEntrySize(int maxEntrySize) { - MAX_ENTRY_SIZE = maxEntrySize; + if(maxEntrySize < 0) { + MAX_ENTRY_SIZE = DEFAULT_MAX_ENTRY_SIZE; + } else { + MAX_ENTRY_SIZE = maxEntrySize; + } } public static int getMaxEntrySize() { @@ -61,7 +65,7 @@ public final class ZipArchiveFakeEntry extends ZipArchiveEntry implements Closea final long entrySize = entry.getSize(); final int threshold = ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles(); - if (threshold >= 0 && entrySize >= threshold) { + if (threshold >= 0 && (entrySize >= threshold || entrySize == -1)) { if (ZipInputStreamZipEntrySource.shouldEncryptTempFiles()) { encryptedTempData = new EncryptedTempData(); try (OutputStream os = encryptedTempData.getOutputStream()) { diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java index 8afaaecb2d..484d37b6a3 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java @@ -28,6 +28,9 @@ import java.io.IOException; import java.math.BigInteger; import java.util.List; +import org.apache.poi.openxml4j.util.ZipArchiveFakeEntry; +import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Units; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange; @@ -149,22 +152,22 @@ class TestXWPFBugs { } } - /** - * Removing a run needs to take into account position of run if paragraph contains hyperlink runs - */ - @Test - void test58618() throws IOException { - try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx")) { - XWPFParagraph para = (XWPFParagraph) doc.getBodyElements().get(0); - assertNotNull(para); - assertEquals("Some text some hyper links link link and some text.....", para.getText()); - XWPFRun run = para.insertNewRun(para.getRuns().size()); - run.setText("New Text"); - assertEquals("Some text some hyper links link link and some text.....New Text", para.getText()); - para.removeRun(para.getRuns().size() - 2); - assertEquals("Some text some hyper links link linkNew Text", para.getText()); - } - } + /** + * Removing a run needs to take into account position of run if paragraph contains hyperlink runs + */ + @Test + void test58618() throws IOException { + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx")) { + XWPFParagraph para = (XWPFParagraph) doc.getBodyElements().get(0); + assertNotNull(para); + assertEquals("Some text some hyper links link link and some text.....", para.getText()); + XWPFRun run = para.insertNewRun(para.getRuns().size()); + run.setText("New Text"); + assertEquals("Some text some hyper links link link and some text.....New Text", para.getText()); + para.removeRun(para.getRuns().size() - 2); + assertEquals("Some text some hyper links link linkNew Text", para.getText()); + } + } @Test void test59378() throws IOException { @@ -329,7 +332,7 @@ class TestXWPFBugs { } } - private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){ + private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id) { // create a numbering scheme CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance(); // give the scheme an ID @@ -340,4 +343,27 @@ class TestXWPFBugs { documentNumbering.addNum(abstractNumID); } + + @Test + void testBug69628() throws IOException { + final int expectedParagraphs = 24; + // bug69628.docx has -1 entry sizes in the zip data + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) { + assertEquals(expectedParagraphs, doc.getParagraphs().size()); + } + // test again with smaller byte array max + ZipArchiveFakeEntry.setMaxEntrySize(30 * 1024 * 1024); + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) { + assertEquals(expectedParagraphs, doc.getParagraphs().size()); + } finally { + ZipArchiveFakeEntry.setMaxEntrySize(-1); + } + // test again but temp files enabled + ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(1000); + try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) { + assertEquals(expectedParagraphs, doc.getParagraphs().size()); + } finally { + ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(-1); + } + } } diff --git a/test-data/document/bug69628.docx b/test-data/document/bug69628.docx new file mode 100644 index 0000000000..359a3b3608 Binary files /dev/null and b/test-data/document/bug69628.docx differ