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
This commit is contained in:
Nick Burch 2013-05-15 00:27:59 +00:00
parent f8ccd5d014
commit a5c69d394b
4 changed files with 47 additions and 0 deletions

View 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() {

View 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);

View 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;

View 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");