|
10 | 10 | import cn.idev.excel.support.ExcelTypeEnum; |
11 | 11 | import java.io.ByteArrayInputStream; |
12 | 12 | import java.io.File; |
| 13 | +import java.io.FileOutputStream; |
13 | 14 | import java.nio.charset.StandardCharsets; |
14 | 15 | import java.nio.file.Files; |
| 16 | +import java.util.zip.ZipOutputStream; |
15 | 17 | import org.junit.jupiter.api.Test; |
16 | 18 |
|
17 | 19 | /** |
@@ -61,4 +63,37 @@ void decryptedStreamProvided_throwsExcelCommonException() { |
61 | 63 | ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, decrypted)); |
62 | 64 | assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format")); |
63 | 65 | } |
| 66 | + |
| 67 | + @Test |
| 68 | + void emptyZipFile_throwsExcelCommonException() throws Exception { |
| 69 | + File tmp = File.createTempFile("empty-zip", ".xlsx"); |
| 70 | + try (FileOutputStream fos = new FileOutputStream(tmp); |
| 71 | + ZipOutputStream zos = new ZipOutputStream(fos)) { |
| 72 | + // write an empty zip with no entries |
| 73 | + } |
| 74 | + try { |
| 75 | + ReadWorkbook rw = new ReadWorkbook(); |
| 76 | + rw.setFile(tmp); |
| 77 | + XlsxReadContext ctx = new DefaultXlsxReadContext(rw, ExcelTypeEnum.XLSX); |
| 78 | + ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, null)); |
| 79 | + assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format")); |
| 80 | + } finally { |
| 81 | + try { |
| 82 | + Files.deleteIfExists(tmp.toPath()); |
| 83 | + } catch (Exception ignore) { |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void inputStream_nonMandatoryUseTempFileBranch_throwsExcelCommonException() { |
| 90 | + ReadWorkbook rw = new ReadWorkbook(); |
| 91 | + rw.setInputStream(new ByteArrayInputStream("not-xlsx".getBytes(StandardCharsets.UTF_8))); |
| 92 | + // mandatoryUseInputStream unset or false -> will write to temp file and then open |
| 93 | + rw.setMandatoryUseInputStream(false); |
| 94 | + XlsxReadContext ctx = new DefaultXlsxReadContext(rw, ExcelTypeEnum.XLSX); |
| 95 | + |
| 96 | + ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, null)); |
| 97 | + assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format")); |
| 98 | + } |
64 | 99 | } |
0 commit comments