Bug 56957: Avoid error if Workbook with empty SharedStringTable is written

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-10-26 07:54:06 +00:00
parent 9a080e3666
commit bcaab496b6
3 changed files with 46 additions and 0 deletions

View File

@ -38,6 +38,7 @@ import org.apache.poi.openxml4j.opc.internal.ZipHelper;
import org.apache.poi.util.DocumentHelper; import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -64,6 +65,12 @@ public final class ZipPartMarshaller implements PartMarshaller {
// exception // exception
} }
// check if there is anything to save for some parts. We don't do this for all parts as some code
// might depend on empty parts being saved, e.g. some unit tests verify this currently.
if(part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
return true;
}
ZipOutputStream zos = (ZipOutputStream) os; ZipOutputStream zos = (ZipOutputStream) os;
ZipEntry partEntry = new ZipEntry(ZipHelper ZipEntry partEntry = new ZipEntry(ZipHelper
.getZipItemNameFromOPCName(part.getPartName().getURI() .getZipItemNameFromOPCName(part.getPartName().getURI()

View File

@ -40,10 +40,12 @@ import org.apache.poi.POIXMLProperties;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.opc.ContentTypes; import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart; import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
@ -1019,5 +1021,42 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
finally { finally {
IOUtils.closeQuietly(wb); IOUtils.closeQuietly(wb);
} }
}
@Test
public void testBug56957CloseWorkbook() throws Exception {
File file = TempFile.createTempFile("TestBug56957_", ".xlsx");
try {
// as the file is written to, we make a copy before actually working on it
FileHelper.copyFile(new File("test-data/spreadsheet/56957.xlsx"), file);
assertTrue(file.exists());
// read-only mode works!
Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
workbook.close();
workbook = null;
// now check read/write mode
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
workbook.close();
workbook = null;
} finally {
assertTrue(file.exists());
assertTrue(file.delete());
}
} }
} }

Binary file not shown.