❤️01_Java/03_Library

#Tika 라이브러리

roomname-dev 2023. 3. 14.
728x90

Tika 라이브러리란? 

Apache Tika 는 Apache Software Foundation 에서 관리하는 Java 로 작성된 컨텐츠 감지 및 분석 프레임워크입니다 .  수천 가지가 넘는 파일 유형 에서 메타데이터와 텍스트를 감지하고 추출하며 Java 라이브러리를 제공할 뿐만 아니라 다른 프로그래밍 언어에서 사용하기에 적합한 서버 및 명령줄 버전이 있습니다.

 

* Maven

<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>2.7.0</version>
</dependency>

* gradle

// https://mvnrepository.com/artifact/org.apache.tika/tika-core
implementation group: 'org.apache.tika', name: 'tika-core', version: '2.7.0'

Maven dependency : https://mvnrepository.com/artifact/org.apache.tika/tika-core/2.7.0

 

 

* metadata 체크

public static void getMetadata() {
		
		Tika tika = new Tika();
		Metadata metadata = new Metadata();

		try (TikaInputStream reader = TikaInputStream.get(Paths.get(DEFAULTPATH))){
			
			// 파일 본문
			String contents = tika.parseToString(reader, metadata);

			/*
			 * 파일 메타데이터 X-Parsed-By: org.apache.tika.parser.DefaultParser
			 * Content-Encoding: UTF-8
			 * csv:delimiter: comma
			 * Content-Type: text/csv; charset=UTF-8; delimiter=comma
			 */
             
	        for(String name : metadata.names()) {
	            System.out.println(name + ": " + metadata.get(name));
	        }
			
		} catch (IOException | TikaException e) {
			e.printStackTrace();
		}
		
	}

* mime type 체크

public static void getMimeType(File file) {
		String mimeType;
		Tika tika = new Tika();
		
		try {
			mimeType = tika.detect(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

* charset  체크 

public static void getCharset() {
		try {
			byte[] arr = Files.readAllBytes(Paths.get(DEFAULTPATH));
			
			CharsetDetector charsetDetector = new CharsetDetector();
			charsetDetector.setText(arr);
			charsetDetector.enableInputFilter(true);
			CharsetMatch cm = charsetDetector.detect();
			
			System.out.println(cm.getName());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

 

 

참고 : https://logging-panda.tistory.com/72

참고 : https://www.tutorialspoint.com/tika/index.htm
참고 : https://tika.apache.org/
참고 : https://github.com/apache/tika

728x90

댓글