[java] Zip 파일을 자바로 압축 및 압축 해제하는 방법

이 문서에서는 자바를 사용하여 Zip 파일을 압축하고, 압축을 해제하는 방법을 알아보겠습니다.

Zip 파일 압축하기

Zip 파일을 압축하기 위해서는 java.util.zip 패키지에 있는 ZipOutputStream 클래스를 사용합니다. 다음은 ZipOutputStream을 사용하여 파일을 압축하는 예제 코드입니다.

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

public class ZipExample {

    public static void main(String[] args) {
        String sourceDirectory = "경로/파일들이 있는 디렉토리";
        String zipFile = "압축한 파일의 경로/파일명.zip";

        try {
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);
            zipDirectory(sourceDirectory, zos);
            zos.close();
            fos.close();
            System.out.println("파일 압축이 완료되었습니다.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void zipDirectory(String sourceDirectory, ZipOutputStream zos) throws IOException {
        File directory = new File(sourceDirectory);
        File[] files = directory.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                zipDirectory(file.getPath(), zos);
            } else {
                byte[] buffer = new byte[1024];
                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();
            }
        }
    }
}

압축할 파일들이 있는 디렉토리와 압축한 파일이 저장될 경로를 지정한 후에 위의 예제 코드를 실행하면, 지정한 디렉토리의 파일들이 압축되어 지정한 경로에 파일명.zip으로 저장됩니다.

Zip 파일 압축 해제하기

Zip 파일을 압축 해제하기 위해서는 java.util.zip 패키지에 있는 ZipInputStream 클래스를 사용합니다. 다음은 ZipInputStream을 사용하여 Zip 파일을 압축 해제하는 예제 코드입니다.

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

public class UnzipExample {

    public static void main(String[] args) {
        String zipFile = "압축 해제할 파일의 경로/파일명.zip";
        String destinationDirectory = "압축 해제될 디렉토리 경로";

        try {
            File directory = new File(destinationDirectory);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(fis);
            unzip(zis, destinationDirectory);
            zis.close();
            fis.close();
            System.out.println("파일 압축 해제가 완료되었습니다.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void unzip(ZipInputStream zis, String destinationDirectory) throws IOException {
        byte[] buffer = new byte[1024];
        ZipEntry zipEntry = zis.getNextEntry();

        while (zipEntry != null) {
            String entryFileName = zipEntry.getName();
            File entryFile = new File(destinationDirectory + File.separator + entryFileName);

            if (zipEntry.isDirectory()) {
                entryFile.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(entryFile);
                int length;
                while ((length = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, length);
                }
                fos.close();
            }

            zipEntry = zis.getNextEntry();
        }
    }
}

압축을 해제할 Zip 파일의 경로와 압축 해제된 파일이 저장될 디렉토리 경로를 지정한 후에 위의 예제 코드를 실행하면, Zip 파일이 압축 해제되어 지정한 디렉토리에 저장됩니다.

결론

위에서 소개한 예제 코드를 사용하여 자바에서 Zip 파일을 압축하고, 압축을 해제할 수 있습니다. 이를 응용하여 자신의 프로젝트에 적용해 보세요!