From: Nick Burch Date: Wed, 15 May 2013 00:27:59 +0000 (+0000) Subject: Where possible, allow fetching of the size of the OPC Package Part (-1 if not) X-Git-Tag: 3.10-beta1~47 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a5c69d394b03b3e879fb7caaa14d1182e871d22e;p=poi.git Where possible, allow fetching of the size of the OPC Package Part (-1 if not) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1482647 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java index 6371c13ce5..56f6a66e91 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java @@ -624,6 +624,13 @@ public abstract class PackagePart implements RelationshipSource { public void setDeleted(boolean isDeleted) { this._isDeleted = isDeleted; } + + /** + * @return The length of the part in bytes, or -1 if not known + */ + public long getSize() { + return -1; + } @Override public String toString() { diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java index e20311787b..8c0e18502a 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java @@ -113,6 +113,11 @@ public class ZipPackagePart extends PackagePart { return null; } + @Override + public long getSize() { + return zipEntry.getSize(); + } + @Override public boolean save(OutputStream os) throws OpenXML4JException { return new ZipPartMarshaller().marshall(this, os); diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java index f950e2ca3b..b6c56949d4 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java @@ -102,6 +102,11 @@ public final class MemoryPackagePart extends PackagePart { return new MemoryPackagePartOutputStream(this); } + @Override + public long getSize() { + return length; + } + public void clear() { data = null; length = 0; diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index b6bc97c185..b4ac8bd7c6 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -30,6 +30,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; import org.apache.poi.openxml4j.opc.internal.FileHelper; +import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.util.TempFile; import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogFactory; @@ -523,6 +524,35 @@ public final class TestPackage extends TestCase { assertTrue(selected.containsKey("/word/theme/theme1.xml")); assertTrue(selected.containsKey("/word/webSettings.xml")); } + + public void testGetPartSize() throws Exception { + String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ); + + int checked = 0; + for (PackagePart part : pkg.getParts()) { + // Can get the size of zip parts + if (part.getPartName().getName().equals("/word/document.xml")) { + checked++; + assertEquals(ZipPackagePart.class, part.getClass()); + assertEquals(6031l, part.getSize()); + } + if (part.getPartName().getName().equals("/word/fontTable.xml")) { + checked++; + assertEquals(ZipPackagePart.class, part.getClass()); + assertEquals(1312l, part.getSize()); + } + + // But not from the others + if (part.getPartName().getName().equals("/docProps/core.xml")) { + checked++; + assertEquals(PackagePropertiesPart.class, part.getClass()); + assertEquals(-1, part.getSize()); + } + } + // Ensure we actually found the parts we want to check + assertEquals(3, checked); + } public void testReplaceContentType() throws Exception { InputStream is = OpenXML4JTestDataSamples.openSampleStream("sample.xlsx");