Comprimere e decomprimere file 7zip in Java

Mattepuffo's logo
Comprimere e decomprimere file 7zip in Java

Comprimere e decomprimere file 7zip in Java

L'altro giorno avevo bisogno di decomprimere tutta una serie di file 7zip in Java.

A quel punto mi sono messo a capire anche come creare archivi 7zip.

Prima di tutto dobbiamo aggiungere delle dipendenze; se usate Maven:

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.20</version>
        </dependency>

        <dependency>
            <groupId>org.tukaani</groupId>
            <artifactId>xz</artifactId>
            <version>1.6</version>
        </dependency>
    </dependencies>

Qui sotto il codice Java:

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class Main {

    public static void main(String[] args) {
        crea7zip(new File("C:\XLS"));
        decomprimi7zip(new File("test.7z"));
    }

    public static void crea7zip(File cartellaDaZippare) {
        try (SevenZOutputFile sevenZOutputFile = new SevenZOutputFile(new File("test.7z"))) {
            Files.walk(cartellaDaZippare.toPath()).forEach(f -> {
                File file = f.toFile();
                if (file.isFile()) {
                    try {
                        SevenZArchiveEntry sevenZArchiveEntry = sevenZOutputFile.createArchiveEntry(file, file.getName());
                        sevenZOutputFile.putArchiveEntry(sevenZArchiveEntry);
                        sevenZOutputFile.write(Files.readAllBytes(file.toPath()));
                        sevenZOutputFile.closeArchiveEntry();
                    } catch (IOException e) {
                        System.out.println(e.getMessage());
                    }
                }
            });

            sevenZOutputFile.finish();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void decomprimi7zip(File file7zip) {
        try (SevenZFile sevenZFile = new SevenZFile(file7zip)) {
            SevenZArchiveEntry entry;
            while ((entry = sevenZFile.getNextEntry()) != null) {
                File file = new File(entry.getName());
                System.out.println("Un seven zipping - " + file);
                String dir = file.toPath().toString().substring(0, file.toPath().toString().lastIndexOf("\"));
                Files.createDirectories(new File(dir).toPath());
                byte[] content = new byte[(int) entry.getSize()];
                sevenZFile.read(content);
                Files.write(file.toPath(), content);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}

Enjoy!


Condividi su Facebook

Commentami!