at de.innosystec.unrar.Archive.readHeaders(Archive.java:234)
at de.innosystec.unrar.Archive.setFile(Archive.java:124)
at de.innosystec.unrar.Archive.<init>(Archive.java:108)
at de.innosystec.unrar.Archive.<init>(Archive.java:96)
at Main.extractArchive(Main.java:71)
at Main.extractArchive(Main.java:39)
at Main.main(Main.java:58)
at de.innosystec.unrar.Archive.isEncrypted(Archive.java:194)
at Main.extractArchive(Main.java:78)
at Main.extractArchive(Main.java:39)
at Main.main(Main.java:58)
Este es mi codigo:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import javax.swing.JPopupMenu.Separator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;
public class Main {
public boolean accept
(File dir,
String extension
) { return dir.getName().endsWith(extension);
}
private static Log logger = LogFactory.getLog(Main.class
.getName());
public static void extractArchive
(String archive,
String destination
) { if (archive == null || destination == null) {
}
if (!arch.exists()) {
}
if (!dest.exists() || !dest.isDirectory()) {
"El destino debe existir y apuntar a un directorio: "
+ destination);
}
extractArchive(arch, dest);
}
public static void main
(String[] args
) { if (args.length == 2) {
// File[] listaObjetos = fi.listFiles();
// for (int i = 0; i < listaObjetos.length; i++) {
// if (new Main().accept(listaObjetos[i], ".pdf.cxsig")) {
// String res = listaObjetos[i].getPath();
// String file = res.substring(res.lastIndexOf('/') + 1, res.lastIndexOf('.')-4);
// File re = new File(file.concat(".rar"));
// listaObjetos[i].renameTo(re);
//
// System.out.println("Se encontro archivos rar: " + listaObjetos[i].getPath() + file);
// }
// }
extractArchive(args[0], args[1]);
} else {
.println("use: java -jar NombreJAR.jar <Origen> <Destino>");
}
}
public static void extractArchive
(File archive,
File destination
) {
Archive arch = null;
try {
arch = new Archive(archive);
} catch (RarException e) {
logger.error(e);
logger.error(e1);
}
if (arch != null) {
if (arch.isEncrypted()) {
logger.warn("archive is encrypted cannot extreact");
return;
}
FileHeader fh = null;
while (true) {
fh = arch.nextFileHeader();
if (fh == null) {
break;
}
// if (fh.isEncrypted()) {
// logger.warn("file is encrypted cannot extract: "
// + fh.getFileNameString());
// continue;
// }
logger.info("extracting: " + fh.getFileNameString());
try {
if (fh.isDirectory()) {
createDirectory(fh, destination);
} else {
File f
= createFile
(fh, destination
); arch.extractFile(fh, stream);
stream.close();
}
logger.error("error extracting the file", e);
} catch (RarException e) {
logger.error("error extraction the file", e);
}
}
}
}
private static File createFile
(FileHeader fh,
File destination
) { if (fh.isFileHeader() && fh.isUnicode()) {
name = fh.getFileNameW();
} else {
name = fh.getFileNameString();
}
f
= new File(destination, name
); if (!f.exists()) {
try {
f = makeFile(destination, name);
logger.error("error creating the new file: " + f.getName(), e);
}
}
return f;
}
String[] dirs
= name.
split("\\\\"); if (dirs == null) {
return null;
}
int size = dirs.length;
if (size == 1) {
return new File(destination, name
); } else if (size > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
path
= path
+ File.
separator + dirs
[i
]; new File(destination, path
).
mkdir(); }
path
= path
+ File.
separator + dirs
[dirs.
length - 1]; f.createNewFile();
return f;
} else {
return null;
}
}
private static void createDirectory
(FileHeader fh,
File destination
) { if (fh.isDirectory() && fh.isUnicode()) {
f
= new File(destination, fh.
getFileNameW()); if (!f.exists()) {
makeDirectory(destination, fh.getFileNameW());
}
} else if (fh.isDirectory() && !fh.isUnicode()) {
f
= new File(destination, fh.
getFileNameString()); if (!f.exists()) {
makeDirectory(destination, fh.getFileNameString());
}
}
}
private static void makeDirectory
(File destination,
String fileName
) { String[] dirs
= fileName.
split("\\\\"); if (dirs == null) {
return;
}
path
= path
+ File.
separator + dir
; new File(destination, path
).
mkdir(); }
}
public static boolean esPDF
(ZipEntry arch
){ if(arch.getName().endsWith("pdf"))
return true;
return false;
}
}