Galera,
Preciso acessar o conteúdo de um jar. Só que esse jar, é o jar que está em execução. Dentro dele, tenho uma pasta (ramaisgrupo) que preciso acessá-la, e executar um de seus arquivos(Ramais.jar).
A estrutura do jar está + ou - assim:
Principal.jar
-------- lib
-------- images
-------- src
-------- ramaisgrupo
-------------------------- Ramais.jar
-------------------------- lib
Pensei 2 soluções para isso:
-
Descompactar esse jar e acessar a pasta ramaisgrupo (Achei uma classe, mas ela não está conseguindo descompactar o jar todo, apenas começa e depois para);
-
Acessar esse jar em execução, abrir a pasta ramaisgrupo e executar (Runtime.getRuntime(…)) o aquivo Ramais.jar (Porém não tenho nem ideia de como se fazer).
Por enquanto não consegui solução alguma, mas qual das duas seria melhor?
Segue o código da classe que estou usando para descompactar. Essa classe chega a criar a pasta descompactada e alguns arquivos, mas não cria o resto. Alguém poderia me ajudar :?:
[code]
public class Arquivador {
private static final String BARRA = "/";
private static final String BARRA_INVERTIDA = "\\";
private static final int TAMANHO_BUFFER = 512000;
public static void main(String files[]) {
try {
final Arquivador arquivador = new Arquivador();
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("opcao [c = compactar] [d = descompactar] [s = sair]: ");
String operacao = br.readLine();
String origem = null;
String destino = null;
if ("c".equalsIgnoreCase(operacao)) {
System.out.println("COMPACTAR");
System.out.print("arquivo(s) a ser(em) compactado(s): ");
origem = br.readLine();
System.out.print("arquivo de destino: ");
destino = br.readLine();
String[] origens = arquivador.separaArquivos(origem);
arquivador.compacta(origens, destino);
} else if ("d".equalsIgnoreCase(operacao)) {
System.out.println("DESCOMPACTAR");
System.out.print("arquivo a ser descompactado: ");
origem = br.readLine();
System.out.print("diretorio de destino: ");
destino = br.readLine();
arquivador.descompacta(origem, destino);
} else if (!"s".equalsIgnoreCase(operacao)) {
System.out.println("opcao invalida");
main(files);
}
System.out.println("concluido");
} catch (Throwable t) {
t.printStackTrace();
}
}
public void compacta(String[] origens, String destino) throws IOException {
int length = origens == null ? 0 : origens.length;
if (length == 0 || destino == null) {
return;
}
FileOutputStream fos = new FileOutputStream(destino);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setMethod(ZipOutputStream.DEFLATED);
File file = new File(origens[0]).getParentFile();
String root = file == null ? null : file.getCanonicalPath();
for (int i = 0; i < length; i++) {
file = new File(origens[i]);
if (file.isDirectory()) {
varreDiretorio(root, origens[i], zos);
} else {
adicionaArquivo(root, origens[i], zos);
}
}
zos.close();
}
public void descompacta(String origem, String destino) throws IOException {
if (origem == null || destino == null) {
return;
}
FileInputStream fis = new FileInputStream(origem);
ZipInputStream zis = new ZipInputStream(fis);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ZipEntry ze = null;
String name = null;
while ((ze = zis.getNextEntry()) != null) {
name = destino + BARRA_INVERTIDA + ze.getName();
try {
fos = new FileOutputStream(name);
} catch (FileNotFoundException exc) {
montaDiretorio(name);
fos = new FileOutputStream(name);
}
bos = new BufferedOutputStream(fos, TAMANHO_BUFFER);
System.out.println("descompactando: '" + ze.getName() + "'");
int bytes;
byte buffer[] = new byte[TAMANHO_BUFFER];
while ((bytes = zis.read(buffer, 0, TAMANHO_BUFFER)) != -1) {
bos.write(buffer, 0, bytes);
}
bos.flush();
bos.close();
}
zis.close();
}
private void adicionaArquivo(final String raiz, final String arquivo,
final ZipOutputStream saida) throws IOException {
if (arquivo.length() > 0) {
int length = raiz == null ? 0 : raiz.length();
String name = length > 0 ? arquivo.substring(length) : arquivo;
name = name.replaceAll(BARRA, BARRA_INVERTIDA);
while (name.startsWith(BARRA_INVERTIDA)) {
name = name.substring(1);
}
System.out.println("compactando '" + name + "'");
ZipEntry entry = new ZipEntry(name);
saida.putNextEntry(entry);
FileInputStream fis = new FileInputStream(arquivo);
int offset = 0;
byte[] buffer = new byte[TAMANHO_BUFFER];
while ((offset = fis.read(buffer, 0, TAMANHO_BUFFER)) != -1) {
saida.write(buffer, 0, offset);
}
fis.close();
}
}
private String[] listaConteudo(File diretorio) throws IOException {
File[] files = diretorio.listFiles();
int length = files == null ? 0 : files.length;
String[] children = new String[length];
File f = null;
for (int i = 0; i < length; i++) {
children[i] = files[i].getCanonicalPath();
}
return children;
}
private void montaDiretorio(String nome) throws IOException {
File f;
StringBuffer sb = new StringBuffer();
StringTokenizer st = new StringTokenizer(nome, BARRA_INVERTIDA);
int tokens = st.countTokens() - 1;
for (int i = 0; i < tokens; i++) {
sb.append(st.nextToken() + BARRA_INVERTIDA);
}
f = new File(sb.toString());
f.mkdirs();
}
private String[] separaArquivos(String texto) {
StringTokenizer st = new StringTokenizer(texto, ";");
int tokens = st.countTokens();
String[] array = new String[tokens];
for (int i = 0; i < tokens; i++) {
array[i] = st.nextToken();
}
return array;
}
private void varreDiretorio(final String raiz, final String diretorio,
final ZipOutputStream saida) throws IOException {
File file = new File(diretorio);
String[] files = file.list();
int length = files == null ? 0 : files.length;
String caminho = null;
for (int i = 0; i < length; i++) {
file = new File(diretorio, files[i]);
caminho = file.getCanonicalPath();
if (file.isDirectory()) {
varreDiretorio(raiz, caminho, saida);
} else {
adicionaArquivo(raiz, caminho, saida);
}
}
saida.flush();
}
}[/code]
Galera,
Consegui através do excelente artigo que achei aqui no GUJ (Zip com Java). Muito bom mesmo esse artigo. Segue o link para os interessados:
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
http://www.guj.com.br/java.tutorial.artigo.181.1.guj
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
BrunoLeo, estou tendo problemas ao extrair os arquivos com a aplicação já compilada (em tempo de desenvolvimento esta OK). pode postar seu código p/ eu ver como ficou?