]> source.dussan.org Git - poi.git/commitdiff
Where possible, allow fetching of the size of the OPC Package Part (-1 if not)
authorNick Burch <nick@apache.org>
Wed, 15 May 2013 00:27:59 +0000 (00:27 +0000)
committerNick Burch <nick@apache.org>
Wed, 15 May 2013 00:27:59 +0000 (00:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1482647 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java

index 6371c13ce5309e4812449fa8df556d9da4619019..56f6a66e915cdd731be23c53ea0af567af42a2b4 100644 (file)
@@ -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() {
index e20311787b576afd031644c0194788cd428120f6..8c0e18502a0fd6608be25c3da400774f4e190e7e 100644 (file)
@@ -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);
index f950e2ca3b890784dda8d3b2d71e4387ca18af70..b6c56949d4ce6b1b0744a369880f4109a6cb9336 100644 (file)
@@ -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;
index b6bc97c18553ca21aae0fc86f5beb3756132639e..b4ac8bd7c68c2a63742e86809cdd0118c0142697 100644 (file)
@@ -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");