Ver Mensaje Individual
  #2 (permalink)  
Antiguo 26/11/2012, 11:13
Padin
 
Fecha de Ingreso: octubre-2012
Mensajes: 25
Antigüedad: 11 años, 6 meses
Puntos: 3
Respuesta: Descomprimir un archivo con Java

Cita:
Iniciado por pierxo Ver Mensaje
Tengo un problema al descomprimir un archivo .RAR con java. Estoy utilizando Junrar para poder hacerlo pero me sale este error.

Código Java:
Ver original
  1.         at de.innosystec.unrar.Archive.readHeaders(Archive.java:234)
  2.         at de.innosystec.unrar.Archive.setFile(Archive.java:124)
  3.         at de.innosystec.unrar.Archive.<init>(Archive.java:108)
  4.         at de.innosystec.unrar.Archive.<init>(Archive.java:96)
  5.         at Main.extractArchive(Main.java:71)
  6.         at Main.extractArchive(Main.java:39)
  7.         at Main.main(Main.java:58)
  8.  
  9. Exception in thread "main" java.lang.NullPointerException: mainheader is null
  10.         at de.innosystec.unrar.Archive.isEncrypted(Archive.java:194)
  11.         at Main.extractArchive(Main.java:78)
  12.         at Main.extractArchive(Main.java:39)
  13.         at Main.main(Main.java:58)
  14.  
  15.  
  16. Este es mi codigo:
  17.  
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.zip.ZipEntry;
  24.  
  25. import javax.swing.JPopupMenu.Separator;
  26.  
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29.  
  30. import de.innosystec.unrar.Archive;
  31. import de.innosystec.unrar.exception.RarException;
  32. import de.innosystec.unrar.rarfile.FileHeader;
  33.  
  34.  
  35. public class Main {
  36.      public boolean accept(File dir, String extension) {
  37.             return dir.getName().endsWith(extension);
  38.         }
  39.      
  40.     private static Log logger = LogFactory.getLog(Main.class
  41.             .getName());
  42.  
  43.  public static void extractArchive(String archive, String destination) {
  44.         if (archive == null || destination == null) {
  45.             throw new RuntimeException("archivo y destino deben setearse");
  46.         }
  47.         File arch = new File(archive);
  48.         if (!arch.exists()) {
  49.             throw new RuntimeException("el archivo no existe: " + archive);
  50.         }
  51.         File dest = new File(destination);
  52.         if (!dest.exists() || !dest.isDirectory()) {
  53.             throw new RuntimeException(
  54.                 "El destino debe existir y apuntar a un directorio: "
  55.                     + destination);
  56.         }
  57.         extractArchive(arch, dest);
  58.         }
  59.  
  60.  public static void main(String[] args) {
  61.         if (args.length == 2) {
  62.             String path = "D:/dir";
  63.             File fi = new File(path);
  64.            
  65. //          File[] listaObjetos = fi.listFiles();
  66. //          for (int i = 0; i < listaObjetos.length; i++) {
  67. //              if (new Main().accept(listaObjetos[i], ".pdf.cxsig")) {
  68. //                  String res = listaObjetos[i].getPath();
  69. //                  String file = res.substring(res.lastIndexOf('/') + 1, res.lastIndexOf('.')-4);
  70. //                  File re = new File(file.concat(".rar"));
  71. //                  listaObjetos[i].renameTo(re);
  72. //                 
  73. //                 System.out.println("Se encontro archivos rar: " + listaObjetos[i].getPath() + file);
  74. //              }
  75. //       }
  76.             extractArchive(args[0], args[1]);
  77.         } else {
  78.             System.out
  79.                 .println("use: java -jar NombreJAR.jar <Origen> <Destino>");
  80.         }
  81.          
  82.        
  83. }
  84.      
  85.      public static void extractArchive(File archive, File destination) {
  86.  
  87.             Archive arch = null;
  88.             try {
  89.                 arch = new Archive(archive);
  90.             } catch (RarException e) {
  91.                 logger.error(e);
  92.             } catch (IOException e1) {
  93.                 logger.error(e1);
  94.             }
  95.             if (arch != null) {
  96.                 if (arch.isEncrypted()) {
  97.                 logger.warn("archive is encrypted cannot extreact");
  98.                 return;
  99.                 }
  100.                 FileHeader fh = null;
  101.                 while (true) {
  102.                 fh = arch.nextFileHeader();
  103.                 if (fh == null) {
  104.                     break;
  105.                 }
  106. //              if (fh.isEncrypted()) {
  107. //                  logger.warn("file is encrypted cannot extract: "
  108. //                      + fh.getFileNameString());
  109. //                  continue;
  110. //              }
  111.                 logger.info("extracting: " + fh.getFileNameString());
  112.                 try {
  113.                     if (fh.isDirectory()) {
  114.                     createDirectory(fh, destination);
  115.                     } else {
  116.                     File f = createFile(fh, destination);
  117.                     OutputStream stream = new FileOutputStream(f);
  118.                     arch.extractFile(fh, stream);
  119.                     stream.close();
  120.                     }
  121.                 } catch (IOException e) {
  122.                     logger.error("error extracting the file", e);
  123.                 } catch (RarException e) {
  124.                     logger.error("error extraction the file", e);
  125.                 }
  126.                 }
  127.             }
  128.             }
  129.      
  130.        private static File createFile(FileHeader fh, File destination) {
  131.             File f = null;
  132.             String name = null;
  133.             if (fh.isFileHeader() && fh.isUnicode()) {
  134.                 name = fh.getFileNameW();
  135.             } else {
  136.                 name = fh.getFileNameString();
  137.             }
  138.             f = new File(destination, name);
  139.             if (!f.exists()) {
  140.                 try {
  141.                 f = makeFile(destination, name);
  142.                 } catch (IOException e) {
  143.                 logger.error("error creating the new file: " + f.getName(), e);
  144.                 }
  145.             }
  146.             return f;
  147.             }
  148.        
  149.      private static File makeFile(File destination, String name)
  150.         throws IOException {
  151.     String[] dirs = name.split("\\\\");
  152.     if (dirs == null) {
  153.         return null;
  154.     }
  155.     String path = "";
  156.     int size = dirs.length;
  157.     if (size == 1) {
  158.         return new File(destination, name);
  159.     } else if (size > 1) {
  160.         for (int i = 0; i < dirs.length - 1; i++) {
  161.         path = path + File.separator + dirs[i];
  162.         new File(destination, path).mkdir();
  163.         }
  164.         path = path + File.separator + dirs[dirs.length - 1];
  165.         File f = new File(destination, path);
  166.         f.createNewFile();
  167.         return f;
  168.     } else {
  169.         return null;
  170.     }
  171.     }
  172.  
  173.      
  174.      private static void createDirectory(FileHeader fh, File destination) {
  175.             File f = null;
  176.             if (fh.isDirectory() && fh.isUnicode()) {
  177.                 f = new File(destination, fh.getFileNameW());
  178.                 if (!f.exists()) {
  179.                 makeDirectory(destination, fh.getFileNameW());
  180.                 }
  181.             } else if (fh.isDirectory() && !fh.isUnicode()) {
  182.                 f = new File(destination, fh.getFileNameString());
  183.                 if (!f.exists()) {
  184.                 makeDirectory(destination, fh.getFileNameString());
  185.                 }
  186.             }
  187.             }
  188.      
  189.      private static void makeDirectory(File destination, String fileName) {
  190.             String[] dirs = fileName.split("\\\\");
  191.             if (dirs == null) {
  192.                 return;
  193.             }
  194.             String path = "";
  195.             for (String dir : dirs) {
  196.                 path = path + File.separator + dir;
  197.                 new File(destination, path).mkdir();
  198.             }
  199.  
  200.     }
  201.      
  202.      public static boolean esPDF(ZipEntry arch){
  203.           if(arch.getName().endsWith("pdf"))
  204.              return true;
  205.      return false;
  206.      }
  207. }

Si alguien podria ayudarme.

El nombre de mi .rar es sample95.pdf.rar. Orginial mente era sample95.pdf.cxsig

le cambio la ultima extension por un .rar he intente descomprimirlo

Gracias
No puedo ayudarte, pero te pongo el código más legible