[java] 자바에서 파일 압축 시 압축 비율 설정하기
import java.io.*;
import java.util.zip.*;

public class FileCompressionDemo {
    public static void main(String[] args) {
        String sourceFile = "inputFile.txt";
        String compressedFile = "compressedFile.zip";
        
        try {
            byte[] buffer = new byte[1024];
            FileOutputStream fos = new FileOutputStream(compressedFile);
            ZipOutputStream zos = new ZipOutputStream(fos);
            FileInputStream fis = new FileInputStream(sourceFile);
            zos.putNextEntry(new ZipEntry(sourceFile));
            Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); // 압축 비율을 설정합니다.

            int length;
            while ((length = fis.read(buffer)) > 0) {
                deflater.setInput(buffer, 0, length);
                deflater.finish();
                while (!deflater.finished()) {
                    length = deflater.deflate(buffer);
                    zos.write(buffer, 0, length);
                }
                deflater.end();
            }

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

            System.out.println("File compressed successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위의 예제 코드에서 Deflater 클래스의 생성자에 Deflater.BEST_COMPRESSION을 인수로 전달하여 최상의 압축 비율을 설정하였습니다. 이렇게 설정된 압축 비율은 파일을 압축할 때 더욱 효율적으로 용량을 줄이는 데 도움을 줄 수 있습니다.

참고 자료:

위의 코드를 참고하여 원하는 압축 비율로 파일을 압축할 수 있습니다.