0

I have been working on a task to decompress from different types of file format such as "zip,tar,tbz,tgz". I am able to do for all except tbz because apache common compress library provides BZIP2 compressors. But I need to decompress a old BZIP not BZIP2. Is there any way to do it java. I have added the code I have done so far for extracting different tar file archives using apache commons library below.

public List<ArchiveFile> processTarFiles(String compressedFilePath, String fileType) throws IOException {
    List<ArchiveFile> extractedFileList = null;
    TarArchiveInputStream is = null;
    FileOutputStream fos = null;
    BufferedOutputStream dest = null;
    try {
        if(fileType.equalsIgnoreCase("tar"))
        {
            is = new TarArchiveInputStream(new FileInputStream(new File(compressedFilePath)));
        }
        else if(fileType.equalsIgnoreCase("tbz")||fileType.equalsIgnoreCase("bz"))
        {
            is = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(new File(compressedFilePath))));
        }
        else if(fileType.equalsIgnoreCase("tgz")||fileType.equalsIgnoreCase("gz"))
        {
            is = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(new File(compressedFilePath))));
        }
        TarArchiveEntry entry = is.getNextTarEntry();
        extractedFileList = new ArrayList<>();
        while (entry != null) {
            // grab a zip file entry
            String currentEntry = entry.getName();

            if (!entry.isDirectory()) {
                File destFile = new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry);
                File destinationParent = destFile.getParentFile();
                // create the parent directory structure if needed
                destinationParent.mkdirs();
                ArchiveFile archiveFile = new ArchiveFile();
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[(int) entry.getSize()];
                // write the current file to disk
                fos = new FileOutputStream(destFile);
                dest = new BufferedOutputStream(fos, (int) entry.getSize());

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, (int) entry.getSize())) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                archiveFile.setExtractedFilePath(destFile.getAbsolutePath());
                archiveFile.setFormat(destFile.getName().split("\\.")[1]);
                extractedFileList.add(archiveFile);
                entry = is.getNextTarEntry();
            } else {
                new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry).mkdirs();
                entry = is.getNextTarEntry();
            }

        }
    } catch (IOException e) {
        System.out.println(("ERROR: " + e.getMessage()));
    } catch (Exception e) {
        System.out.println(("ERROR: " + e.getMessage()));
    } finally {
        is.close();
        dest.flush();
        dest.close();
    }

    return extractedFileList;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
venkat
  • 442
  • 5
  • 17
  • 1
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow (see [help]). – Zabuzard Jun 04 '19 at 09:10
  • Actually, this is not off topic as I also need a sample code and the library to use – venkat Jun 04 '19 at 09:15
  • 1
    You're asking for a library recommendation - that's off-topic. I don't see how your justification *isn't* "I'm asking for the library to use". Asking for sample code along with that doesn't make it on-topic. – Jon Skeet Jun 04 '19 at 09:19
  • Okay, suggest me a platform in which I have to ask – venkat Jun 04 '19 at 09:20
  • I also edited my question not asking for a library, but for a solution in java – venkat Jun 04 '19 at 09:22
  • I am pretty sure the answer to your question is "yes", that it is possible. But to find a solution that you don't have to build yourself is your task. – Smutje Jun 04 '19 at 09:23
  • Try asking on https://www.reddit.com/r/javahelp. It's better suited for such questions. – Behrang Jun 04 '19 at 09:27
  • You could ask specifically for a specific library, show some attempt on your own and ask how to solve the specific issue. But then also make sure to add the tag to that library, then the experts can find the question. – Zabuzard Jun 04 '19 at 09:32
  • @Zabuza - I have added the code, that I have done using apache commons library. The problem is the it used BZIP2compressor. So as for now I have to write the entire code for decompressing old BZIP formats. – venkat Jun 04 '19 at 09:35
  • Now, after all the edits, its way better. I retracted my close vote. – Zabuzard Jun 04 '19 at 09:43
  • Tldr: Apache does not support bzip, nor does any of the known libraries. Maybe you will find some implementation anywhere, but rather unlikely. But asking for that would be off topic here. Is there a specific reason you want to support a format almost nobody supports? Highly unlikely that you will ever encounter an old bzip. – Zabuzard Jun 04 '19 at 09:49
  • Thanks @ zabuza - The requirement suggests the list of archive files needed to be handled. In that tbz was mentioned. So i have to do that. A way to go may be executing the linux commands to extract the tbz file from java – venkat Jun 04 '19 at 10:07

1 Answers1

2

The original Bzip was supposedly using a patented algorithm so Bzip2 was born using algorithms and techniques that were not patented.

That might be the reason why it's no longer in widespread use and open source libraries ignore it.

There's some C code for decompressing Bzip files shown here (gist.github.com mirror).

You might want to read and rewrite that in Java.

Behrang
  • 46,888
  • 25
  • 118
  • 160
  • 1
    Also see [r/compression/bzip vs bzip2](https://www.reddit.com/r/compression/comments/4ipei4/bzip_vs_bzip2/) – Zabuzard Jun 04 '19 at 09:42