Olá a todos,
Estou desenvolvendo um projeto em que eu preciso descompactar arquivos zip com senhas.
Pesquisei um bocado mas ainda não encontrei a solução (Talvez não tenha procurado no lugar certo). Por isso peço ajuda.
Se alguém souber de exemplos, qualquer coisa que ajude, por favor me avise.
Olá,
O problema foi resolvido. Post encerrado.
Olá amigo. Estou com com um problema parecido, se vc puder postar a solução que encontrou, ajudaria muito.
Obrigado.
Oi Juno,
Vi um método que descompacta o arquivo utilizando a senha. No meu caso, não precisei quebrar a mesma como achei que teria que fazer. Um outro arquivo trazia informações para que eu gerasse o hash pelo md5 para ser usada neste método.
Peguei o mesmo neste site: http://java.sys-con.com/node/1258827
obs.: Estou tendo problemas com ele quando o arquivo zipado contém arquivos muito grandes. Você pode acompanhar pelo post http://www.guj.com.br/posts/list/217004.java que abri há pouco. Ainda estou tentando resolver.
Abaixo está ele separado.
@SuppressWarnings("CallToThreadDumpStack")
public static void extrairZipComSenha(final String arquivoZipadoComSenha, final String diretorioDestino, final String senha) {
RandomAccessFile randomAccessFile = null;
ISevenZipInArchive inArchive = null;
//Collins
final byte[] buffer = new byte[TAMANHO_BUFFER];
try {
randomAccessFile = new RandomAccessFile(arquivoZipadoComSenha, "r");
inArchive = SevenZip.openInArchive(null, // autodetect archive type
new RandomAccessFileInStream(randomAccessFile));
// Getting simple interface of the archive inArchive
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
final int[] hash = new int[]{0};
if (!item.isFolder()) {
ExtractOperationResult result;
result = item.extractSlow(new ISequentialOutStream() {
@Override
@SuppressWarnings("CallToThreadDumpStack")
public int write(final byte[] data) throws SevenZipException {
try {
if (item.getPath().indexOf(File.separator) > 0) {
String path = diretorioDestino + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
File folderExisting = new File(path);
if (!folderExisting.exists()) {
new File(path).mkdirs();
}
}
OutputStream out = new FileOutputStream(diretorioDestino + File.separator + item.getPath());
out.write(data);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
hash[0] |= Arrays.hashCode(data);
return data.length; // Return amount of proceed data
}
}, senha); /// password.
if (result == ExtractOperationResult.OK) {
System.out.println(String.format("%9X | %s",
hash[0], item.getPath()));
} else {
System.err.println("Error extracting item: " + result);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
System.err.println("Error closing archive: " + e);
e.printStackTrace();
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
e.printStackTrace();
}
}
}
}
Quem quiser descompactar/compactar arquivos com senha ou sem senha segue abaixo link para excelentes exemplos:
http://www.example-code.com/java/zip.asp