-
ํ์ผ ํ์ File Type ํ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋น๊ต :: Apache Tika, JMimeMagic, SimpleMagicPROGRAMMING/๊ธฐํ 2021. 2. 1. 22:46
ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค๋ณด๋ฉด file ์ ๊ด๋ จ๋ ์์ ์ ํ ๋๊ฐ ์๋ค.
์ด ๋ ํ์ผ์ด ์ด๋ค ํ์ ์ธ์ง ํ์ธํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ๋ช ๊ฐ์ง ์กด์ฌํ๋๋ฐ, ํ ๋ฒ ์ง์ ๋น๊ตํด๋ณด์๋ค.
1. ์คํ ํ๊ฒฝ:: Intellij, gradle, Kotlin, project SDK 15.0.2
์ข ๋ฅ์ ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ๋ค.
1) Apache Tika (tika.apache.org/)
๋ฐฉ์:: FIle MetaData ์ ํ์ผ ๋ด์ฉ์ ํ์ฑํด์ ํ์ธ
ํน์ง:: ์ด์ ์๋ ์์กด์ฑ์ด ๋ง์์ ๋ถํธํ์ง๋ง, ๊พธ์คํ ์ฑ๋ฅ ๊ฐ์ ์ผ๋ก ํ๋์ dependency ๋ง ์ถ๊ฐํด์ ์ฌ์ฉํ ์ ์๋ค.
์ฌ์ฉํ dependency : org.apache.tika:tika-parsers:1.18
์ ์ ์ฌํญ:: ๊ทธ๋ฅ dependency ์ ์ถ๊ฐํ๋ค๊ฐ๋ ๊ธฐ์กด์ dependency ์ ์ถฉ๋์ ์ผ์ผํฌ ํ๋ฅ ์ด ๋์ผ๋, ๋ฒ์ ์ ๊ผญ ์ ํ์ธํ ๊ฒ !!
(๋์ ๊ฒฝ์ฐ lombck ์ด 1.18.16 ์ด์๋๋ฐ, Apache Tika ๋ฅผ ์ด๊ฑฐ๋ ๋ง์ถฐ์ฃผ๋๊น Conflict ๊ฐ ํด๊ฒฐ๋๋ค.. ์ฐธ๊ณ )
2) JMimeMagic (github.com/arimus/jmimemagic)
๋ฐฉ์:: ํ์ผ ํ์ฅ์ & ํค๋ ์ ๋ณด๋ก ํ์ธ
์ฌ์ฉํ dependency : net.sf.jmimemagic:jmimemagic:0.1.5
3) SimpleMagic (github.com/j256/simplemagic)
๋ฐฉ์:: ํ์ผ ํ์ฅ์ & ํค๋ ์ ๋ณด๋ก ํ์ธ
์ฌ์ฉํ dependency : com.j256.simplemagic:simplemagic:1.16
2. ์คํ ์ค๋น
1) gradle ์ dependencies ์ถ๊ฐ
(์์ ์ build ์ ๋ง๊ฒ ์์ฑํด์ฃผ๋ฉด ๋๋ค.)
implementation("org.apache.tika:tika-parsers:1.18") implementation("net.sf.jmimemagic:jmimemagic:0.1.5") implementation("com.j256.simplemagic:simplemagic:1.16")
2) ํ ์คํธ ์ฝ๋ ์์ฑ
(ํ ์คํธํ ํ์ผ์ ๋ชจ์๋ ํด๋ ๊ฒฝ๋ก : E:\\tmp)
2-1) Apache Tike ํ ์คํธ ์ฝ๋
fun fileType1(){ //Apache Tika val rootPath = "E:\\tmp" val file = File(rootPath) if(!file.exists()) throw IllegalArgumentException("no ${file.absolutePath}") var tika = Tika() file.walk().forEach { println(it.absolutePath) println(tika.detect(it)+"\n") } }
2-2 JMimeMagic ํ ์คํธ ์ฝ๋
fun fileType2 () { //JMimeMagic val rootPath = "E:\\tmp" val file = File(rootPath) if(!file.exists()) throw IllegalArgumentException("no ${file.absolutePath}") file.walk().forEach { println(it.absolutePath) val match : MagicMatch? = Magic.getMagicMatch(it, true, false) if(match == null) { println("file match x\n") }else { println("extension: ${match.extension} / mimeType: ${match.mimeType}\n") } } }
2-3 SimpleMagic ํ ์คํธ ์ฝ๋
fun fileType3 () { //SimpleMagic var util : ContentInfoUtil = ContentInfoUtil() val rootPath = "E:\\tmp" val file = File(rootPath) if(!file.exists()) throw IllegalArgumentException("no ${file.absolutePath}") file.walk().forEach { println(it.absolutePath) val info : ContentInfo? = util.findMatch(it) if(info == null) println("file match x \n") else { println("contentType: ${info.contentType} / mimeType: ${info.mimeType} \n") } } }
3. ๊ฒฐ๊ณผ
3-1 Apache Tika
3-2 JMimeMagic
3-3 SimpleMagic
4. ๊ฒฐ๊ณผ ๋ถ์
(์ด๋๊น์ง๋ ์์ ์์ฑํ ํ ์คํธ ์ฝ๋์ ์์ ํ์ผ, ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฒ์ ์ ๋ํ ๊ฒฐ๊ณผ์ด๋ฏ๋ก, ๋ค๋ฅธ ํ์ผ์ด๋ ์ฝ๋, ๋ฒ์ ์ ๋ฐ๊พธ๋ฉด ๋ฌ๋ผ์ง ์ ์์์ ์๋ ค๋๋ฆฝ๋๋ค.)
(ํ๋์ Bold : [๋ด๊ธฐ์ค] ๋ง๊ฒ ํ๋ณํ ๊ฒ)
FileType / ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ข ๋ฅ Apache Tika JMimeMagic SimpleMagic .sin text/plain text/plain x .html text/html text/html text/html .png image/png image/png image/png .cpp text/x-csrc text/plain x .cpp
(๋ด์ฉ์ empty ์ํ, ํ์ฅ์๋ง ๋ณ๊ฒฝ)text/x-c++src text/plain x .pdf application/pdf application/pdf application/pdf .css text/css text/plain x .js application/javascript text/plain x .pptx [warning] application/vnd.openxmlformates-officedocument.presentationml.presentation application/vnd.openxmlformats-officedocument.presentationml.presentation application/vnd.openxmlformats-officedocument.presentationml.presentation .txt text/plain text/plain x .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.openxmlformats-officedocument.wordprocessingml.document .hwp application/x-hwp-v5 application/msword null ๋๋ถ๋ถ์ ํ์ผ์์ Apache Tika ๊ฐ ์ ํ๋๊ฐ ๋์ ๊ฒ์ ๋ณผ ์ ์๋ค.
๊ทธ๋ฐ๋ฐ png, ppt, docx ์ ๋์ ํ์ผ๋ง ์ฌ์ฉํ๋ค ์ถ์ผ๋ฉด ๊ตณ์ด Apache Tika ๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋ฌด๋ฐฉํ ๊ฒ์ด๋ค.
์ฆ ์์ ์ ํ์์ ๋ฐ๋ผ ์ทจ์ฌ์ ํํ๊ธฐ~
'PROGRAMMING > ๊ธฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ