aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2018-05-02 18:33:23 +0000
committerAndreas Beeker <kiwiwings@apache.org>2018-05-02 18:33:23 +0000
commitba603ce0d79fb5214ec6aa61db0ac4852e1941d8 (patch)
tree787d3dc91da3c49571367b144788ac88846c6795 /src/java/org
parent1e5697909eee991ac0ddb272f153406aa68379c9 (diff)
downloadpoi-ba603ce0d79fb5214ec6aa61db0ac4852e1941d8.tar.gz
poi-ba603ce0d79fb5214ec6aa61db0ac4852e1941d8.zip
add convenience method for files to FileMagic
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1830782 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r--src/java/org/apache/poi/poifs/filesystem/FileMagic.java19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/java/org/apache/poi/poifs/filesystem/FileMagic.java b/src/java/org/apache/poi/poifs/filesystem/FileMagic.java
index 765cf6e323..4ac616082e 100644
--- a/src/java/org/apache/poi/poifs/filesystem/FileMagic.java
+++ b/src/java/org/apache/poi/poifs/filesystem/FileMagic.java
@@ -22,6 +22,8 @@ import static org.apache.poi.poifs.common.POIFSConstants.RAW_XML_FILE_HEADER;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -117,6 +119,23 @@ public enum FileMagic {
return UNKNOWN;
}
+
+ /**
+ * Get the file magic of the supplied {@link File}<p>
+ *
+ * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
+ * that the ZIP stream has leading junk bytes
+ *
+ * @param inp a file to be identified
+ */
+ public static FileMagic valueOf(final File inp) throws IOException {
+ try (FileInputStream fis = new FileInputStream(inp)) {
+ final byte[] data = IOUtils.toByteArray(fis, 8);
+ return FileMagic.valueOf(data);
+ }
+ }
+
+
/**
* Get the file magic of the supplied InputStream (which MUST
* support mark and reset).<p>