2026년 1월 13일 화요일

lz4 압축하기

 public void writeUsers(String dirPath, List<User> users) throws Exception {


    Path dir = Paths.get(dirPath);

    Files.createDirectories(dir);


    Path filePath = dir.resolve(

        "users_" +

        java.time.LocalDateTime.now()

            .format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))

        + ".tmp"

    );


    try (BufferedWriter writer = Files.newBufferedWriter(

            filePath,

            StandardOpenOption.CREATE,

            StandardOpenOption.TRUNCATE_EXISTING

    )) {

        for (User u : users) {

            writer.write(u.getId() + "," + u.getName() + "," + u.getAge());

            writer.newLine();

        }

    }

}

import net.jpountz.lz4.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Lz4LineCompressor {

    public static void compressFile(Path inputFile, Path outputFile) throws Exception {

        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4Compressor compressor = factory.fastCompressor();

        try (
            BufferedReader reader = Files.newBufferedReader(inputFile, StandardCharsets.UTF_8);
            DataOutputStream out = new DataOutputStream(
                    new BufferedOutputStream(Files.newOutputStream(outputFile))
            )
        ) {
            String line;
            while ((line = reader.readLine()) != null) {

                byte[] src = line.getBytes(StandardCharsets.UTF_8);
                int maxCompressedLength = compressor.maxCompressedLength(src.length);
                byte[] compressed = new byte[maxCompressedLength];

                int compressedLength = compressor.compress(
                        src, 0, src.length,
                        compressed, 0, maxCompressedLength
                );

                // 1️⃣ 압축된 길이 기록
                out.writeInt(compressedLength);

                // 2️⃣ 압축 데이터 기록
                out.write(compressed, 0, compressedLength);
            }
        }
    }
}



import net.jpountz.lz4.*;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Lz4LineDecompressor {

    public static void decompressFile(Path inputFile, Path outputFile) throws Exception {

        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4SafeDecompressor decompressor = factory.safeDecompressor();

        try (
            DataInputStream in = new DataInputStream(
                    new BufferedInputStream(Files.newInputStream(inputFile))
            );
            var writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8)
        ) {
            while (true) {
                try {
                    // 1️⃣ 압축된 길이 읽기
                    int compressedLength = in.readInt();

                    // 2️⃣ 압축 데이터 읽기
                    byte[] compressed = new byte[compressedLength];
                    in.readFully(compressed);

                    // 3️⃣ 압축 해제
                    byte[] restored = decompressor.decompress(compressed, compressedLength);

                    // 4️⃣ 한 라인으로 복원
                    writer.write(new String(restored, StandardCharsets.UTF_8));
                    writer.newLine();

                } catch (java.io.EOFException eof) {
                    // 파일 끝
                    break;
                }
            }
        }
    }
}


댓글 없음: