From 40591cf09af65a2f7b404bf1fdf2ec5c842e83d3 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 20 Jul 2016 11:07:29 +0000 Subject: #57919 Start on support for writing to a new File (faster than OutputStream) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753486 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/POIFSFileSystem.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/java/org/apache/poi/poifs/filesystem') diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index 48d75ab940..e78f18f9f1 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -20,6 +20,7 @@ package org.apache.poi.poifs.filesystem; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -121,6 +122,27 @@ public class POIFSFileSystem public static boolean hasPOIFSHeader(byte[] header8Bytes) { return NPOIFSFileSystem.hasPOIFSHeader(header8Bytes); } + + /** + * Creates a new {@link POIFSFileSystem} in a new {@link File}. + * Use {@link #POIFSFileSystem(File)} to open an existing File, + * this should only be used to create a new empty filesystem. + * + * @param file The file to create and open + * @return The created and opened {@link POIFSFileSystem} + */ + public static POIFSFileSystem create(File file) throws IOException { + // TODO Make this nicer! + // Create a new empty POIFS in the file + POIFSFileSystem tmp = new POIFSFileSystem(); + FileOutputStream fout = new FileOutputStream(file); + tmp.writeFilesystem(fout); + fout.close(); + tmp.close(); + + // Open it up again backed by the file + return new POIFSFileSystem(file); + } /** * read in a file and write it back out again -- cgit v1.2.3