How to decompress LZO file using java (using library lzo-core)

169 Views Asked by At

I am getting the issue while trying to decompress the LZO file using java. Below is the code and error I have pasted, can someone please help me on this

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

There are 1 best solutions below

4
On

I have used LzopInputStream instead of LzoInputStream and the file got decompressed successfully in Linux. I used following code :

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

However, I used following code to compress file:

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

Because when I used system software for compression it gave following error:

Exception in thread "main" java.io.IOException: Compressed with incompatible lzo version: 0x2080 (expected 0x2050) Because LZO compression is updated, but I was not able to get updated version of used library.

We would need to use some other library with the support of latest LZO Version for that.