반응형

Zip파일 내부에 존재하는 파일리스트 확인하기

pom.xml

apache.commons 추가

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-compress</artifactId>
	<version>1.8</version>
</dependency>

ZipArchiveInputStream, ZipArchiveEntry 사용을 위해 추가합니다.

 

 

apache.tika 추가

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.14</version>
</dependency>

파일의 MIME Type을 체크하기 위해 Tika 추가

 

 

ZipUtils.java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.tika.Tika;

public class ZipUtils {

	public static void zipChecker(String filePath) {
		InputStream in = null;
		ZipArchiveInputStream zais = null;
		ZipArchiveEntry entry = null;
		Tika tika = new Tika();
		try {
			File file = new File(filePath);
			if(file.exists()) {
				in = new FileInputStream(new File(filePath));
				zais = new ZipArchiveInputStream(in, "UTF-8", true);
				while((entry = zais.getNextZipEntry()) != null) {
					System.out.println(tika.detect(entry.getName()));
					System.out.print(entry.getTime() + "  ");
					System.out.print(entry.getName() + "  ");
					System.out.print(entry.getComment() + "  ");
					System.out.print(entry.getCompressedSize() + "  ");
					System.out.print(entry.getCrc() + "  ");
					System.out.print(entry.getPlatform() + "  ");
					System.out.print(entry.getUnparseableExtraFieldData());
					System.out.println();
				}
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(in != null) in.close();
				if(zais != null) zais.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

Main.java

public class App {
	public static void main(String[] args) {
		ZipUtils.zipChecker("E:/zip/test.zip");
	}
}

 

 

결과

 

압축 파일 리스트

반응형