[java] ZIP 압축 방법을 사용하여 압축 파일 해제하기

이 블로그에서는 JAVA를 사용하여 ZIP 파일을 압축 해제하는 방법에 대해 설명합니다.

ZIP 파일 압축 해제하기

우선, JAVA에서 ZIP 파일을 압축 해제하는 방법은 매우 간단합니다. java.util.zip 패키지에 포함된 ZipInputStream 클래스를 사용하여 ZIP 파일을 압축 해제할 수 있습니다.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipExample {
    public static void main(String[] args) {
        String filePath = "path_to_zip_file.zip";
        String destinationPath = "path_to_destination_folder";

        try {
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(filePath));
            ZipEntry entry = zipInputStream.getNextEntry();

            while (entry != null) {
                String entryName = entry.getName();
                String outputPath = destinationPath + entryName;
                if (!entry.isDirectory()) {
                    FileOutputStream fileOutputStream = new FileOutputStream(outputPath);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = zipInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, length);
                    }
                    fileOutputStream.close();
                } else {
                    // Create directory
                    new File(outputPath).mkdirs();
                }
                zipInputStream.closeEntry();
                entry = zipInputStream.getNextEntry();
            }
            zipInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

위의 예제 코드는 ZIP 파일을 압축 해제하는 데 사용할 수 있는 간단한 JAVA 프로그램입니다. 프로그램은 ZipInputStream을 사용하여 ZIP 파일을 엽니다. 그런 다음, 각 파일을 반복하여 파일을 읽고 쓰는 방식으로 압축을 해제합니다.

이제 위의 코드를 사용하여 ZIP 파일을 압축 해제하는 방법을 간단히 알아보았습니다. ZIP 파일 압축 해제에 대한 추가적인 정보는 Java 8 API 문서에서 확인할 수 있습니다.