Domanda

Come estraggo un file tar (o tar.gz, o tar.bz2) in Java?

È stato utile?

Soluzione

Nota: Questa funzionalità è stata successivamente pubblicata attraverso un progetto separato, Apache Commons Compress, come descritta in un altro risposta. Questa risposta non è aggiornata.


Non ho usato direttamente un'API tar, ma tar e bzip2 sono implementati in Ant; potresti prendere in prestito la loro implementazione o eventualmente usare Ant per fare ciò di cui hai bisogno.

Gzip è parte di Java SE (e immagino che l'implementazione di Ant segua lo stesso modello).

GZIPInputStream è solo un InputStream decoratore. Puoi avvolgere, ad esempio, un FileInputStream in un BufferedInputStream e utilizzarlo nello stesso modo in cui utilizzeresti qualsiasi <=>:

InputStream is = new GZIPInputStream(new FileInputStream(file));

(Notare che GZIPInputStream ha un proprio buffer interno, quindi il wrapping di <=> in un <=> probabilmente ridurrebbe le prestazioni.)

Altri suggerimenti

Puoi farlo con la libreria Compress di Apache Commons. Puoi scaricare la versione 1.2 da http://mvnrepository.com/artifact/ org.apache.commons / commons-comprimere / 1.2 .

Ecco due metodi: uno che decomprime un file e un altro che lo decomprime. Quindi, per un file < nomefile > tar.gz, è necessario prima decomprimerlo e successivamente decomprimerlo. Si noti che anche l'archivio tar può contenere cartelle, nel caso in cui debbano essere create sul filesystem locale.

Enjoy.

/** Untar an input file into an output file.

 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.tar' extension. 
 * 
 * @param inputFile     the input .tar file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException 
 */
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {

    LOG.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile); 
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null; 
    while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                LOG.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            LOG.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
            final OutputStream outputFileStream = new FileOutputStream(outputFile); 
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close(); 

    return untaredFiles;
}

/**
 * Ungzip an input file into an output file.
 * <p>
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.gz' extension. 
 * 
 * @param inputFile     the input .gz file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
private static File unGzip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {

    LOG.info(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3));

    final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    IOUtils.copy(in, out);

    in.close();
    out.close();

    return outputFile;
}

Apache Commons VFS supporta tar come file system virtuale , che supporta URL come questo tar:gz:http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt

TrueZip o il suo successore TrueVFS fa lo stesso ... è disponibile anche da Maven Central.

Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
archiver.extract(archiveFile, destDir);

Dipendenza:

 <dependency>
        <groupId>org.rauschig</groupId>
        <artifactId>jarchivelib</artifactId>
        <version>0.5.0</version>
</dependency>

Ho appena provato alcune delle librerie suggerite (TrueZip, Apache Compress), ma senza fortuna.

Ecco un esempio con Apache Commons VFS:

FileSystemManager fsManager = VFS.getManager();
FileObject archive = fsManager.resolveFile("tgz:file://" + fileName);

// List the children of the archive file
FileObject[] children = archive.getChildren();
System.out.println("Children of " + archive.getName().getURI()+" are ");
for (int i = 0; i < children.length; i++) {
    FileObject fo = children[i];
    System.out.println(fo.getName().getBaseName());
    if (fo.isReadable() && fo.getType() == FileType.FILE
        && fo.getName().getExtension().equals("nxml")) {
        FileContent fc = fo.getContent();
        InputStream is = fc.getInputStream();
    }
}

E la dipendenza maven:

    <dependency>
      <groupId>commons-vfs</groupId>
      <artifactId>commons-vfs</artifactId>
      <version>1.0</version>
    </dependency>

Oltre a gzip e bzip2, Apache Commons Compress API ha anche il supporto tar, originariamente basato su Pacchetto di tar Java ICE Engineering , che è sia API che strumento autonomo.

Che dire dell'utilizzo di questa API per i file tar, questo altro incluso in Ant per BZIP2 e uno standard per GZIP?

Ecco una versione basata su questa precedente risposta di Dan Borza che utilizza Apache Commons Compress e Java NIO (ovvero Path anziché File). Fa anche la decompressione e la decompressione in un flusso in modo da non creare file intermedi.

public static void unTarGz( Path pathInput, Path pathOutput ) throws IOException {
    TarArchiveInputStream tararchiveinputstream =
        new TarArchiveInputStream(
            new GzipCompressorInputStream(
                new BufferedInputStream( Files.newInputStream( pathInput ) ) ) );

    ArchiveEntry archiveentry = null;
    while( (archiveentry = tararchiveinputstream.getNextEntry()) != null ) {
        Path pathEntryOutput = pathOutput.resolve( archiveentry.getName() );
        if( archiveentry.isDirectory() ) {
            if( !Files.exists( pathEntryOutput ) )
                Files.createDirectory( pathEntryOutput );
        }
        else
            Files.copy( tararchiveinputstream, pathEntryOutput );
    }

    tararchiveinputstream.close();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top