使用Java的lzo-core库解压LZO文件时遇EOFException问题求助
解决Java使用lzo-core库解压LZO文件时的EOFException问题
我在尝试使用Java的lzo-core库解压LZO文件时遇到EOFException错误,以下是我的代码及报错信息:
我的代码
import org.anarres.lzo.*; import java.io.*; public class LZODecompression { public static void main(String args[]) throws IOException { InputStream in = new FileInputStream(new File("/desktop/mm_impressions_101349_20220723_2022072802.txt.lzo")); LzoAlgorithm algorithm = LzoAlgorithm.LZO1X; LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null); LzoInputStream stream = new LzoInputStream(in, decompressor); OutputStream outputStream = new FileOutputStream(new File("/Desktop/test.txt")); int len; byte[] bytes = new byte[1024]; while ((len = stream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } outputStream.close(); stream.close(); } }
报错信息
Exception in thread "main" java.io.EOFException at org.anarres.lzo.LzoInputStream.readBytes(LzoInputStream.java:183) at org.anarres.lzo.LzoInputStream.readBlock(LzoInputStream.java:132) at org.anarres.lzo.LzoInputStream.fill(LzoInputStream.java:119) at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:102) at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:97) at org.example.LZODecompression.main(LZODecompression.java:37)
可能的原因及解决办法
- 文件格式不匹配:
LzoInputStream仅支持带块头的LZO流格式,但很多场景下的LZO文件是无块结构的原始压缩数据(比如Hadoop生成的LZO文件),这会导致流读取时提前触发EOF。 - 文件损坏/不完整:源LZO文件可能下载不全或被篡改,导致读取到文件末尾时无法解析完整的压缩块。
- 算法不匹配:确认压缩该文件时使用的LZO算法是否为LZO1X,若算法不一致也会引发解析错误。
针对原始LZO数据的修改方案
如果你的文件是原始LZO压缩数据,直接使用LzoDecompressor处理,无需通过LzoInputStream:
import org.anarres.lzo.*; import java.io.*; public class LZODecompression { public static void main(String args[]) throws IOException, LzoException { File inputFile = new File("/desktop/mm_impressions_101349_20220723_2022072802.txt.lzo"); File outputFile = new File("/Desktop/test.txt"); // 使用try-with-resources自动关闭流 try (InputStream in = new BufferedInputStream(new FileInputStream(inputFile)); OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) { LzoAlgorithm algorithm = LzoAlgorithm.LZO1X; LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null); LzoByteBufferDecompressor byteBufferDecompressor = new LzoByteBufferDecompressor(decompressor); byte[] inputBuffer = new byte[8192]; // 分配足够大的输出缓冲区,避免溢出 byte[] outputBuffer = new byte[decompressor.getMaximumOutputLength(inputBuffer.length)]; int readLen; while ((readLen = in.read(inputBuffer)) != -1) { int decompressedLen = byteBufferDecompressor.decompress(inputBuffer, 0, readLen, outputBuffer, 0); out.write(outputBuffer, 0, decompressedLen); } } } }
其他排查步骤
- 用命令行工具
lzop验证文件完整性:
若命令行能正常解压,说明是代码流处理逻辑问题;若同样报错,需重新获取完整的LZO文件。lzop -d /desktop/mm_impressions_101349_20220723_2022072802.txt.lzo - 确认lzo-core依赖版本,建议使用最新稳定版(如1.0.6)。
内容的提问来源于stack exchange,提问作者Pritam007
相关产品推荐
相关产品推荐

