If doing an in-place write, update the properties too

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753623 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-07-20 23:31:57 +00:00
parent af36e63bff
commit 92a146e4b5
3 changed files with 18 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -34,6 +35,8 @@ import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.DocumentNode;
import org.apache.poi.poifs.filesystem.NPOIFSDocument;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -298,7 +301,16 @@ public abstract class POIDocument implements Closeable {
mSet.write(bOut); mSet.write(bOut);
byte[] data = bOut.toByteArray(); byte[] data = bOut.toByteArray();
ByteArrayInputStream bIn = new ByteArrayInputStream(data); ByteArrayInputStream bIn = new ByteArrayInputStream(data);
// New or Existing?
// TODO Use a createOrUpdate method for this to be cleaner!
try {
DocumentNode propSetNode = (DocumentNode)outFS.getRoot().getEntry(name);
NPOIFSDocument propSetDoc = new NPOIFSDocument(propSetNode);
propSetDoc.replaceContents(bIn);
} catch (FileNotFoundException e) {
outFS.createDocument(bIn,name); outFS.createDocument(bIn,name);
}
logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length); logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length);
} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) { } catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {

View File

@ -1311,6 +1311,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode); NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode);
workbookDoc.replaceContents(new ByteArrayInputStream(getBytes())); workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
// Update the properties streams in the file
writeProperties(directory.getFileSystem(), null);
// Sync with the File on disk // Sync with the File on disk
directory.getFileSystem().writeFilesystem(); directory.getFileSystem().writeFilesystem();
} }

View File

@ -689,6 +689,8 @@ public class NPOIFSFileSystem extends BlockStore
return getRoot().createDocument(name, stream); return getRoot().createDocument(name, stream);
} }
// TODO Add a createOrUpdateDocument method to simplify code
/** /**
* create a new DocumentEntry in the root entry; the data will be * create a new DocumentEntry in the root entry; the data will be
* provided later * provided later