Frage

Ich habe ein Problem mit dem Komprimieren Sie die Apache-Bibliothek.Ich möchte ein Archiv entpacken, das Binärdateien enthält.Hier ist der Code:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

public class ArchiveManager {

    public static final int BUFFER_MAX = 2048;

    public static void untar(String fileName, String targetPath) throws IOException {
        File tarArchiveFile = new File(fileName);
        BufferedOutputStream dest = null;
        FileInputStream tarArchiveStream = new FileInputStream(tarArchiveFile);
        TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream));
        TarArchiveEntry entry = null;
        try {
            while ((entry = tis.getNextTarEntry()) != null) {
                int count;
                File outputFile = new File(targetPath, entry.getName());

                if (entry.isDirectory()) { // entry is a directory
                    if (!outputFile.exists()) {
                        outputFile.mkdirs();
                    }
                } else { // entry is a file
                    byte[] data = new byte[BUFFER_MAX];
                    FileOutputStream fos = new FileOutputStream(outputFile);
                    dest = new BufferedOutputStream(fos, BUFFER_MAX);
                    while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if (dest != null) {
                dest.flush();
                dest.close();
            }
            tis.close();
        }
    }
}

Beim Entpacken von Binärdateien getNextTarEntry() löst eine Ausnahme aus:

java.lang.IllegalArgumentException: Invalid byte 111 at offset 0 in 'o.txt{NUL}{NUL}{NUL}' len=8
    at org.apache.commons.compress.archivers.tar.TarUtils.parseOctal(TarUtils.java:99)
    at org.apache.commons.compress.archivers.tar.TarArchiveEntry.parseTarHeader(TarArchiveEntry.java:786)
    at org.apache.commons.compress.archivers.tar.TarArchiveEntry.<init>(TarArchiveEntry.java:308)
    at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:198)
    at com.airbus.pakito.download.ArchiveManager.untar(ArchiveManager.java:22)

Ich habe versucht, einfache Textdateien zu entpacken.Nachdem Sie die letzte Datei entpackt haben, getNextTarEntry() gibt nicht null zurück, sondern ein Objekt mit leeren Dateien.Also entry.getName() ist leer und offensichtlich new FileOutputStream(outputFile) Es kann keine Datei erstellt werden.

java.io.FileNotFoundException: C:\Temp (Accès refusé)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.airbus.pakito.util.Archive.untar(Archive.java:32)

Haben Sie eine Idee, woher das Problem kommt?

Danke.

War es hilfreich?

Lösung

Endlich habe ich herausgefunden, woher das Problem kommt.

Das ist ein bekannter Fehler Dies wurde in der Version 1.4 von Apache Compress behoben.Ich hoffe, dass es diese Veröffentlichung geben wird sehr bald verfügbar.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top