[java] ZIP 압축 방법을 사용하여 자바 데이터 압축 가용성 확인하기

이번 블로그에서는 자바에서 ZIP 파일을 생성 및 압축 해제하는 방법에 대해 알아보겠습니다. ZIP 파일 형식은 자바에서 편리하게 처리할 수 있는 압축 포맷 중 하나이며, 자바에서는 이를 다루기 위한 java.util.zip 라이브러리를 제공합니다.

ZIP 파일 생성하기

ZIP 파일을 생성하기 위해서는 ZipOutputStream 클래스를 사용합니다. 아래 예제는 지정된 파일과 폴더를 ZIP 파일로 압축하는 방법을 보여줍니다.

import java.io.*;
import java.util.zip.*;

public class ZipFileExample {
    public static void main(String[] args) {
        byte[] buffer = new byte[1024];

        try {
            FileOutputStream fos = new FileOutputStream("output.zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            File file = new File("input.txt");

            FileInputStream fis = new FileInputStream(file);
            zos.putNextEntry(new ZipEntry(file.getName()));

            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            zos.closeEntry();
            fis.close();

            File folder = new File("inputFolder");
            File[] files = folder.listFiles();

            for (File file : files) {
                FileInputStream fis = new FileInputStream(file);
                zos.putNextEntry(new ZipEntry(file.getName()));

                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }

                zos.closeEntry();
                fis.close();
            }

            zos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위 코드에서는 ZipOutputStream을 사용하여 ZIP 파일을 생성하고, FileInputStream을 사용하여 파일의 내용을 읽은 다음 ZipEntry를 생성하여 ZIP 파일에 쓰고 있습니다.

ZIP 파일 압축 해제하기

이어서 ZIP 파일을 해제하는 방법을 살펴보겠습니다. 아래 예제는 ZIP 파일을 압축 해제하여 파일과 폴더를 생성하는 방법을 보여줍니다.

import java.io.*;
import java.util.zip.*;

public class UnzipFileExample {
    public static void main(String[] args) {
        byte[] buffer = new byte[1024];

        try {
            File folder = new File("outputFolder");
            if (!folder.exists()) {
                folder.mkdir();
            }

            ZipInputStream zis = new ZipInputStream(new FileInputStream("input.zip"));
            ZipEntry ze;

            while ((ze = zis.getNextEntry()) != null) {
                String fileName = ze.getName();
                File newFile = new File(folder.getPath() + File.separator + fileName);

                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int length;
                while ((length = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, length);
                }

                fos.close();
                zis.closeEntry();
            }

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

위 코드에서는 ZipInputStream을 사용하여 ZIP 파일을 열고, ZipEntry가 있는 동안 파일을 생성하고 그 내용을 쓰고 있습니다.

결론

이제 자바에서 ZIP 파일을 생성하고 압축 해제하는 방법에 대해 알아보았습니다. ZIP 파일 형식은 파일 압축 및 압출 해제의 기능을 구현할 때 유용하게 활용될 수 있습니다. 해당 기능을 활용하여 자바 애플리케이션에서 데이터 압축을 구현하는 데 도움이 될 것입니다.

참고문헌: