Ejemplo n.º 1
0
  private void extract(final String inputFile, final String outputDir) throws IOException {
    FileInputStream fileInputStream = null;
    ZipArchiveInputStream zipArchiveInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {

      Log.d(this.getClass().getName(), "Will extract " + inputFile + " to " + outputDir);

      byte[] buffer = new byte[8192];
      fileInputStream = new FileInputStream(inputFile);

      // We use null as encoding.
      zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream, null, true);
      ArchiveEntry entry;
      while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
        Log.d(this.getClass().getName(), "Extracting entry " + entry.getName());
        File file = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
          file.mkdirs();
        } else {
          file.getParentFile().mkdirs();
          fileOutputStream = new FileOutputStream(file);
          int bytesRead;
          while ((bytesRead = zipArchiveInputStream.read(buffer, 0, buffer.length)) != -1)
            fileOutputStream.write(buffer, 0, bytesRead);
          fileOutputStream.close();
          fileOutputStream = null;
        }
      }
      // Delete the zip file
      File zipFile = new File(inputFile);
      zipFile.delete();
    } catch (Exception e) {
      Log.e("UnzipperTask", "Error unzipping file: " + inputFile + ", " + e);
    } finally {
      try {
        zipArchiveInputStream.close();
        fileInputStream.close();
        if (fileOutputStream != null) {
          fileOutputStream.close();
        }
      } catch (NullPointerException ex) {
        Log.e(this.getClass().getName(), "Error closing the file streams.", ex);
      } catch (IOException ex) {
        Log.e(this.getClass().getName(), "Error closing the file streams.", ex);
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public List<XarEntry> getEntries(File xarFile) throws IOException {
    List<XarEntry> documents = null;

    FileInputStream fis = new FileInputStream(xarFile);
    ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);

    try {
      for (ZipArchiveEntry zipEntry = zis.getNextZipEntry();
          zipEntry != null;
          zipEntry = zis.getNextZipEntry()) {
        if (!zipEntry.isDirectory()) {
          try {
            XarPageLimitedHandler documentHandler =
                new XarPageLimitedHandler(this.componentManager);

            parseDocument(zis, documentHandler);

            if (documents == null) {
              documents = new ArrayList<XarEntry>();
            }

            XarEntry xarEntry = documentHandler.getXarEntry();
            xarEntry.setEntryName(zipEntry.getName());

            documents.add(xarEntry);
          } catch (NotADocumentException e) {
            // Impossible to know that before parsing
          } catch (Exception e) {
            this.logger.error("Failed to parse document [" + zipEntry.getName() + "]", e);
          }
        }
      }
    } finally {
      zis.close();
    }

    return documents != null ? documents : Collections.<XarEntry>emptyList();
  }