[Resolvido] Abrir arquivo e capturar conteudo

4 respostas
paulofernandesjr

Opa!

galera não sei o que devo fazer. Vou explicar o que eu quero e vocês tentam entender e me ajudar, qualquer ajuda é valida.

Quero abrir um arquivo "aplicacao.jar", dentro desse arquivo tem uma pasta chamada "lib", dentro da pasta tem alguns arquivos.

O que eu quero fazer é abrir o arquivo, verificar se a pasta "lib" existe, e depois pegar o arquivo que tem dentro dela e copiar para um diretório local do meu computador que vou escolher atraves do FileChooser

comecei com este codigo aqui, mas não sei para onde vou!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.channels.FileChannel;


public class Teste {
	
	public static void main(String[] args){
		
		try {
			FileInputStream fis = new FileInputStream(new File("aplicacao.jar"));
			FileChannel fc = fis.getChannel();
			if ( fc.isOpen() ) {
				System.out.println("Arquivo aberto");
			} else {
				System.out.println("nao abrir arquivo");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}

aguardo ajuda!
abraço

edit:
adicionando a tag resolvido ao titulo

4 Respostas

rsakurai

Para vc ler o arquivo .jar, vc pode usar:

// Abrir arquivo .jar JarFile jarFile = new JarFile("aplicacao.jar"); Enumeration e = jarFile.entries(); while (e.hasMoreElements()) { JarEntry jarEntry = (JarEntry) e.nextElement(); // Verifica se é o diretorio lib dentro do jar. if (jarEntry.getName().equals("lib")) { // pega o arquivo que vc precisa e grava em outro diretorio break; } } // Fecha o XML jarFile.close();

paulofernandesjr

resolvido

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Classe responsavel por abrir um arquivo jar, selecionar a biblioteca do arquivo 
 * e copiar para uma pasta de destino
 * @author paulofernandesjr
 * @author rsakurai 
 *
 */
public class Teste {

	public static void main(String[] args){
		JarFile jarFile = null;
		try {
			jarFile = new JarFile("aplicacao.jar");
			Enumeration<JarEntry> e = jarFile.entries();  
			while (e.hasMoreElements()) {  
				JarEntry jarEntry = (JarEntry) e.nextElement();  
			    // Verifica se é o diretorio lib dentro do jar.
			    String arquivo = jarEntry.getName(); 
			    if (arquivo.equals("lib/biblioteca.jar")) {  
			    	System.out.println(arquivo);
			    	File f = new File(arquivo);
			    	FileInputStream fis = new FileInputStream(f);
			        FileChannel fc = fis.getChannel();
			        FileOutputStream fos = new FileOutputStream(new File("D:\biblioteca.jar"));
			        FileChannel target = fos.getChannel();
			        fc.transferTo(0, f.length(), target);
			    	// pega o arquivo que vc precisa e grava em outro diretorio  
			    	break;  
			    }  
			}  
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if ( jarFile != null ){
				try {
					// Fecha o arquivo JAR
					jarFile.close();
				} catch (IOException e) {
					// DO NOTHING
				}
			}
		}
	}
}

Esse exemplo esta bem simples e fora de qualquer norma, ele poderia ser melhor escrito.

mas prefiro evitar a fadiga

rsakurai

Oi Paulo,

so altera o comentario:
// Fecha o XML

para:

//Fechar o arquivo .jar

Isso foi um Ctrl+C / Ctrl+V mal sucedido… rs

paulofernandesjr

o meu foi falta de atenção!

mas esta editado!

Criado 6 de novembro de 2008
Ultima resposta 6 de nov. de 2008
Respostas 4
Participantes 2