Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preview .7z content and subfolders without extracting

I would like to preview the content of a .7z without extracting it with Java, so i tried with Apache Commons Compress :

public static void main(String[] args) {
    try {
        SevenZFile sevenZFile = new SevenZFile(new File("C://test.7z"));
        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null) {
            System.out.println("Name: " + entry.getName());
            System.out.println("Size : " + entry.getSize());
            System.out.println("--------------------------------");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

It works but i would like to also preview files into an other 7z or zip from the original 7z :

test.7z
--- file1.txt
--- otherInner.7z
    ---- innerFile1.txt
    ---- innerFile2.txt

Is there a mean to do that without extracting the 7z ? Thanks for your help.

like image 799
Denis Cucchietti Avatar asked Nov 09 '22 03:11

Denis Cucchietti


1 Answers

Kind of. Do you mean without extracting the outer 7z? Yes. without extracting the inner 7z? No.

It looks like Apache Commons SevenZ only works on File objects so you will have to at least extract the inner 7z file to then decode that.

Once you have the entry you want you can read the bytes from the byte array for that entry. You can then write those bytes to a (temp) file, and then make a new SevenZFile object to parse that..

This code is mostly taken from the Apache Common Compress Examples page :

SevenZFile sevenZFile =...
SevenZArchiveEntry entry...

...found our entry we want...
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    sevenZFile.read(content, offset, content.length - offset);
}

Now that we have our compressed bytes stored in byte[] we have to write it out to a file and then we can uncompress it using another SeventZFile instance.

You can probably skip the intermediate byte[] and instead read and write directly to the temp file but either way, you have to make a temp file somewhere.

like image 85
dkatzel Avatar answered Nov 15 '22 05:11

dkatzel