2026년 1월 13일 화요일

zstd 압출 해제

 import com.github.luben.zstd.Zstd;


import java.io.BufferedOutputStream;

import java.io.DataOutputStream;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Path;

import java.io.BufferedReader;


public class ZstdLineCompressor {


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


        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);


                // 1️⃣ 압축

                byte[] compressed = Zstd.compress(src);


                // 2️⃣ 압축 길이 + 데이터 기록

                out.writeInt(compressed.length);

                out.write(compressed);

            }

        }

    }

}

import com.github.luben.zstd.Zstd;

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 ZstdLineDecompressor {

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

        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();

                    byte[] compressed = new byte[compressedLength];
                    in.readFully(compressed);

                    // 2️⃣ 압축 해제
                    byte[] restored = Zstd.decompress(compressed, Zstd.getFrameContentSize(compressed));

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

                } catch (java.io.EOFException eof) {
                    break;
                }
            }
        }
    }
}


Path input = Path.of("/data/input.txt");
Path compressed = Path.of("/data/output.zst");
Path restored = Path.of("/data/restored.txt");

ZstdLineCompressor.compressFile(input, compressed);
ZstdLineDecompressor.decompressFile(compressed, restored);


항목LZ4Zstd
압축률낮음높음
속도매우 빠름빠름
원본 길이 저장❌ 직접 필요⭕ 내부 포함
Decompress 방식safe 필요자동 복원
코드 단순성보통더 단순

댓글 없음: