[java] 자바에서 압축 해제하기

우선, java.util.zip 패키지를 사용하여 압축 파일을 해제할 수 있습니다. 아래는 예시 코드입니다.

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

public class UnzipExample {
    public static void main(String[] args) {
        String zipFilePath = "path_to_your_zip_file.zip";
        String destDir = "output_directory_path";

        byte[] buffer = new byte[1024];

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry zipEntry = zis.getNextEntry();

            while (zipEntry != null) {
                String fileName = zipEntry.getName();
                File newFile = new File(destDir + File.separator + fileName);

                if (zipEntry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                zipEntry = zis.getNextEntry();
            }

            zis.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위의 코드는 ZipInputStream을 사용하여 압축 파일을 해제하는 예시입니다. 주어진 압축 파일의 경로와 압축 해제될 디렉토리의 경로를 설정하고, ZipEntry를 이용하여 개별 압축된 파일을 가져와서 처리합니다.

더 많은 압축 해제 방법은 Oracle 공식 문서를 참고하시기 바랍니다.