0

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)
Pritam007
  • 31
  • 4
  • Have you verified with external tools that the input file is decompressable? Also: `/desktop` if a very weird absolute path that won't work on most systems, so I assume you've replaced the actual path (especially since at another place you specify `/Desktop`). – Generous Badger Sep 01 '22 at 08:15
  • @GenerousBadger Path is correct it is like user/user_name/desktop empty test.txt file is generating on desktop – Pritam007 Sep 01 '22 at 08:22
  • You seem to have missed my first question. – Generous Badger Sep 01 '22 at 09:48

1 Answers1

0

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.

saadeez
  • 111
  • 4
  • Can you please let me know which dependency you are using and version – Pritam007 Sep 02 '22 at 15:04
  • I have tried your code and getting below issues Decompressing:test.lzo Exception in thread "main" java.io.IOException: Invalid LZO header at org.anarres.lzo.LzopInputStream.readHeader(LzopInputStream.java:151) at org.anarres.lzo.LzopInputStream.(LzopInputStream.java:72) at org.example.LZODecompression.decompressOriginal(LZODecompression.java:30) at org.example.LZODecompression.main(LZODecompression.java:49) Process finished with exit code 1 – Pritam007 Sep 02 '22 at 15:07
  • I am using this dependency org.anarres.lzo lzo-core 1.0.6 – Pritam007 Sep 02 '22 at 15:08
  • I am using same file though I have downloaded this jar 'lzo-core-1.0.6.jar'. Commons jar is 'commons-io-2.4.jar' – saadeez Sep 02 '22 at 17:35