¿Cómo extraigo un archivo tar (o tar.gz, o tar.bz2) en Java?¿Cómo extraigo un archivo tar en Java?
Respuesta
Nota: Esta funcionalidad se publicó posteriormente a través de un proyecto independiente, Apache Commons Compress, como described in another answer. Esta respuesta no está actualizada.
No he usado una API tar directamente, pero tar y bzip2 están implementados en Ant; podría tomar prestada su implementación, o posiblemente usar Ant para hacer lo que necesita.
Gzip is part of Java SE (y supongo que la implementación de Ant sigue el mismo modelo).
GZIPInputStream
es solo un decorador InputStream
. Usted puede envolver, por ejemplo, un FileInputStream
en un GZIPInputStream
y utilizarlo de la misma manera que tendría que utilizar cualquier InputStream
:
InputStream is = new GZIPInputStream(new FileInputStream(file));
(Tenga en cuenta que la GZIPInputStream tiene su propio buffer, interno, por lo que envuelve el FileInputStream
en un BufferedInputStream
probablemente disminuirá el rendimiento.)
estaba a punto de contarle sobre GZIPInputStream. Pero no lo ayudará, ya que todavía necesita leer el archivo .tar contenido :) –
La verdad es que ya sé sobre GZIPInputStream, gracias a otra pregunta que hice aquí. Pero no sé nada acerca de las API tar, y esperaba que pudiera haber algo que manejara gzip de manera integrada, así que no quería limitar las respuestas diciendo todo lo que ya sabía. – skiphoppy
Las clases de Apache incluidas en 'ant' funcionan bien. Lo uso todos los días: org.apache.tools.tar.TarEntry y org.apache.tools.tar.TarInputStream; el código es muy similar al que usarías para descomprimir archivos zip. Si quieres hacer Bzip2, usa jaxlib. – tucuxi
¿Qué hay de usar este API para archivos tar, este other one incluido dentro de Ant para BZIP2 y el standard one para GZIP?
Además de gzip y bzip2, Apache Commons Compress API tiene también el apoyo de alquitrán, originalmente basado en ICE Engineering Java Tar Package, que es a la vez activos y de herramienta independiente.
Apache Commons Compress API tiene soporte de alquitrán y se basa originalmente en el paquete ICE tar anterior Creo: http://commons.apache.org/compress/ –
Mi prueba muestra ICE tar para ser el más rápido entre los cinco contendientes (hielo, comprimir, hormiga , xeus + vfs), mientras que Commons Compress ocupa el segundo lugar ... sin embargo, ICE tar parece un poco menos completo de WRT al desempaquetar todas las entradas y WRT mantiene las entradas de archivo originales. –
Apache Commons VFS apoya alquitrán como un sistema de archivos virtual , que soporta URLs como éste tar:gz: http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt
TrueZip o su sucesor TrueVFS hace lo mismo ... también está disponible a partir de Maven Central.
Acabo de probar algunas de las librerías sugeridas (TrueZip, Apache Compress), pero no tuve suerte.
Aquí se muestra un ejemplo 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();
}
}
Y la dependencia Maven:
<dependency>
<groupId>commons-vfs</groupId>
<artifactId>commons-vfs</artifactId>
<version>1.0</version>
</dependency>
Usted puede hacer esto con la biblioteca Apache Commons Comprimir. Puede descargar la versión 1.2 desde http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2.
Aquí hay dos métodos: uno que descomprime un archivo y otro que lo recupera. Por lo tanto, para un archivo <fileName> tar.gz, primero debe descomprimirlo y luego desmarcarlo. Tenga en cuenta que el archivo tar también puede contener carpetas, caso en que deben crearse en el sistema de archivos local.
Disfrútalo.
/** 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;
}
Su ejemplo es un gran comienzo, pero parece que tengo un problema con: while ((entry = (TarArchiveEntry) debInputStream.getNextEntry())! = Null). el problema es cuando proceso el primer archivo a través de framewokr externo (por ej. SAXBuilder), la corriente de entrada debInputStream se cierra y la segunda llamada de depInputStream.getNextEntry() arroja una excepción "buffer de entrada cerrado" – adranale
Relacionado, con una implementación similar : [Cómo desbloquear un archivo TAR utilizando Apache Commons] (http://stackoverflow.com/a/14211580/320399) – blong
Gracias por compartir. Hubiera sido bueno si pusieran un método unTar en la biblioteca de compresas apache. Parece una operación fundamental. – Andrew
Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
archiver.extract(archiveFile, destDir);
Dependencia:
<dependency>
<groupId>org.rauschig</groupId>
<artifactId>jarchivelib</artifactId>
<version>0.5.0</version>
</dependency>
- 1. Cómo analizar un archivo tar en C++
- 2. ¿Cómo extraigo el código fuente de un archivo Jar?
- 3. Autotools - tar Esto no se ve como un archivo tar
- 4. Cómo crear archivo tar plana
- 5. Actualización de un único archivo en un archivo comprimido tar
- 6. ¿Cómo extraigo un solo fragmento de bytes de un archivo?
- 7. ¿Cómo extraigo los archivos adjuntos de un archivo pdf?
- 8. ¿Cómo enumerar todas las entradas de un archivo tar en Java?
- 9. ¿Cómo extraer el archivo tar desde stdin?
- 10. Descomprime el archivo tar en el directorio
- 11. ¿Cómo crear un archivo tar completamente comprimido usando Python?
- 12. Cómo descomprimir un archivo TAR utilizando Apache Commons
- 13. ¿Cómo puedo construir un archivo tar desde stdin?
- 14. Crear archivo tar sin estructura de carpeta
- 15. ¿Cómo extraigo datos ASCII de un archivo binario con formato desconocido en Windows?
- 16. Instalar una gema de un archivo tar o zip descargado
- 17. Tar: Crear un archivo excluir directorios excepto uno
- 18. HP-UX - ¿Cómo puedo leer un archivo de texto del archivo tar sin extraerlo?
- 19. ¿Cómo extraigo un campo de marca de tiempo Postgres usando java?
- 20. ¿Cómo creo un archivo de requisitos de pip para un archivo tar en mi sistema de archivos local?
- 21. ¿Cómo extraer un solo archivo de tar a un directorio diferente?
- 22. ¿Cómo puedo procesar un archivo tar con un grupo de multiprocesamiento de Python?
- 23. ¿Cómo extraigo datos de una DataTable?
- 24. ¿Cómo extraigo el dominio de una URL?
- 25. ¿Cómo extraigo un bit de forma más óptima?
- 26. ¿Qué es un NSConcreteValue y cómo extraigo su valor?
- 27. Cómo importar un archivo .class en un archivo .java?
- 28. Extraer solo un directorio de tar
- 29. ¿Cómo incluir .htaccess en los comandos tar?
- 30. ¿Cómo extraigo elementos uniformes de una matriz?
skiphoppy, después de 2008, cuando originalmente respondí, el proyecto Apache Commons Comprimir fue puesto en libertad. Probablemente deberías aceptar [esta respuesta] (http://stackoverflow.com/a/7556307/3474) para que se destaque más. – erickson