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);
| 항목 | LZ4 | Zstd |
|---|
| 압축률 | 낮음 | 높음 |
| 속도 | 매우 빠름 | 빠름 |
| 원본 길이 저장 | ❌ 직접 필요 | ⭕ 내부 포함 |
| Decompress 방식 | safe 필요 | 자동 복원 |
| 코드 단순성 | 보통 | 더 단순 |
댓글 없음:
댓글 쓰기