首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java解压缩LZO文件(使用库lzo-core)

如何使用java解压缩LZO文件(使用库lzo-core)
EN

Stack Overflow用户
提问于 2022-09-01 08:03:25
回答 1查看 41关注 0票数 0

我在尝试使用java解压缩LZO文件时遇到了这个问题。下面是我粘贴的代码和错误,有谁能帮我一下吗?

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

发布于 2022-09-01 13:17:36

我使用了LzopInputStream而不是LzoInputStream,该文件在Linux中被成功解压缩。我使用了以下代码:

代码语言:javascript
复制
public static void decompressOriginal() throws Exception
    {
        String basePath = "/home/saad/CompressionTest/";
        String compressedFileName = "temp1.lzo";
        String afterDecompressFilename = "temp1.txt";

        System.out.println("Decompressing:" + compressedFileName);

        InputStream in = new FileInputStream(new File(basePath + compressedFileName));

        LzoInputStream stream = new LzopInputStream(in);

        OutputStream outputStream = new FileOutputStream(new File(basePath + afterDecompressFilename));

        int len;
        byte[] bytes = new byte[256];

        while ((len = stream.read(bytes)) != -1)
        {
            outputStream.write(bytes, 0, len);
        }
        outputStream.close();
        stream.close();

        System.out.println("Successfully Decompressed:" + afterDecompressFilename);
    }

但是,我使用了以下代码来压缩文件:

代码语言:javascript
复制
public static void compressFile() throws IOException
    {
        String basePath = "/home/saad/CompressionTest/";
        String sourceFile = "source1.sql";
        String destinationFile = "temp1.lzo";

        File plainTextFile = new File(basePath + sourceFile);

        InputStream inputStream = new FileInputStream(plainTextFile);

        byte[] byteArray = org.apache.commons.io.IOUtils.toByteArray(inputStream);

        inputStream.close();

        System.out.println("File:" + sourceFile + " Array Size:" + byteArray.length);

        LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(LzoAlgorithm.LZO1X, null);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        LzopOutputStream lzopOutputStream = new LzopOutputStream(outputStream, compressor, 1024 * 100, LzopConstants.F_ADLER32_C);

        lzopOutputStream.write(byteArray);
        lzopOutputStream.close();

        System.out.println("Compression Complete:" + sourceFile);

        org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(basePath + destinationFile), outputStream.toByteArray());

        System.out.println("Written File:" + destinationFile);
    }

因为当我使用系统软件进行压缩时,它会产生以下错误:

线程"main“中的

异常:使用不兼容的lzo版本压缩: 0x2080 (预期为0x2050),因为LZO压缩是更新的,但是我无法获得已使用库的更新版本。

为此,我们需要在最新LZO版本的支持下使用其他库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73566051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档