Ejemplo n.º 1
0
 /**
  * Creates and return an {@link ArchiveEntry()} whose attributes are fetched from the given {@link
  * FileHeader}
  *
  * @param header the object that serves to initialize the attributes of the returned ArchiveEntry
  * @return an ArchiveEntry whose attributes are fetched from the given FileHeader
  */
 private ArchiveEntry createArchiveEntry(FileHeader header) {
   return new ArchiveEntry(
       header.getFileNameString().replace('\\', '/'),
       header.isDirectory(),
       header.getMTime().getTime(),
       header.getFullUnpackSize(),
       true);
 }
Ejemplo n.º 2
0
  private void process(Mode mode, Set<String> xmlRootElements, Map<String, Class<?>> entityClasses)
      throws Exception {
    LOGGER.info("Start {}: {}", mode, fiasRarFile);
    try (final Archive archive = new Archive(fiasRarFile)) {
      if (!archive.isEncrypted()) {
        final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        Collection<Object> batch = new ArrayList<>(batchSize);
        FileHeader fileHeader;

        while ((fileHeader = archive.nextFileHeader()) != null) {
          final String fileName = fileHeader.getFileNameString();

          if (!fileHeader.isEncrypted()) {
            if (fileHeader.isFileHeader()) {
              if ((!fileName.startsWith("AS_DEL_") && mode == Mode.SAVE)
                  || (fileName.startsWith("AS_DEL_") && mode == Mode.DELETE)) {
                LOGGER.info("Start Reading: {}", fileName);

                try (final InputStream inputStream = archive.getInputStream(fileHeader)) {
                  final XMLStreamReader xmlStreamReader =
                      xmlInputFactory.createXMLStreamReader(inputStream);

                  try {
                    batch =
                        processFile(mode, xmlRootElements, entityClasses, xmlStreamReader, batch);
                  } finally {
                    xmlStreamReader.close();
                  }
                } catch (Throwable e) {
                  LOGGER.error("{} Error: {}", mode, fileName, e);
                } finally {
                  LOGGER.info("Finish Reading: {}", fileName);
                }
              }
            } else {
              LOGGER.error("Not File: {}", fileName);
            }
          } else {
            LOGGER.error("File is encrypted: {}", fileName);
          }
        }

        if (batch.size() > 0) {
          processBatch(mode, batch);
        }
      } else {
        LOGGER.error("Archive is encrypted: {}", fiasRarFile);
      }
    } finally {
      LOGGER.info("Finish {}: {}", mode, fiasRarFile);
    }
  }
Ejemplo n.º 3
0
 /**
  * 根据原始rar路径,解压到指定文件夹下.
  *
  * @param srcRarPath 原始rar路径
  * @param dstDirectoryPath 解压到的文件夹
  */
 public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
   if (!srcRarPath.toLowerCase().endsWith(".rar")) {
     System.out.println("非rar文件!");
     return;
   }
   File dstDiretory = new File(dstDirectoryPath);
   if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
     dstDiretory.mkdirs();
   }
   Archive a = null;
   try {
     a = new Archive(new File(srcRarPath));
     if (a != null) {
       a.getMainHeader().print(); // 打印文件信息.
       FileHeader fh = a.nextFileHeader();
       while (fh != null) {
         if (fh.isDirectory()) { // 文件夹
           File fol = new File(dstDirectoryPath + File.separator + fh.getFileNameString());
           fol.mkdirs();
         } else { // 文件
           File out = new File(dstDirectoryPath + File.separator + fh.getFileNameString().trim());
           // System.out.println(out.getAbsolutePath());
           try { // 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
             if (!out.exists()) {
               if (!out.getParentFile().exists()) { // 相对路径可能多级,可能需要创建父目录.
                 out.getParentFile().mkdirs();
               }
               out.createNewFile();
             }
             FileOutputStream os = new FileOutputStream(out);
             a.extractFile(fh, os);
             os.close();
           } catch (Exception ex) {
             ex.printStackTrace();
           }
         }
         fh = a.nextFileHeader();
       }
       a.close();
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }