[java] 자바에서 압축 파일 내 특정 파일의 속성 가져오기
자바에서는 java.util.zip
패키지를 사용하여 압축 파일의 내용을 처리할 수 있습니다. 특히, 압축 파일 내에 포함된 파일의 속성을 가져오는 방법을 알아보겠습니다.
압축 파일의 속성 가져오기
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Enumeration;
import java.util.Iterator;
public class ZipPropertiesReader {
public static void main(String[] args) {
String zipFileName = "example.zip";
String entryName = "file.txt";
try (ZipFile zipFile = new ZipFile(zipFileName)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equals(entryName)) {
System.out.println("File Name: " + entry.getName());
System.out.println("Compressed Size: " + entry.getCompressedSize());
System.out.println("Uncompressed Size: " + entry.getSize());
System.out.println("Last Modified: " + entry.getLastModifiedTime());
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
위의 예제 코드는 ZipPropertiesReader
클래스를 정의하고, 주어진 압축 파일에서 지정된 파일의 속성을 읽는 방법을 보여줍니다. ZipFile
을 사용하여 압축 파일을 열고, entries()
메서드를 통해 압축 파일에 포함된 모든 엔트리(파일 및 폴더)를 얻어옵니다. 그런 다음 각 엔트리의 속성(파일 이름, 압축된 크기, 압축 해제 크기, 최종 수정 시간)을 확인하여 원하는 파일의 속성을 얻을 수 있습니다.
결론
위의 예제 코드를 참조하여 자바에서 압축 파일 내 특정 파일의 속성을 가져오는 방법을 익히고 활용할 수 있습니다.