/**
   * Create a new TarFile for the given file.
   *
   * @param file
   * @throws TarException
   * @throws IOException
   */
  public TarFile(File file) throws TarException, IOException {
    this.file = file;

    InputStream in = new FileInputStream(file);
    // First, check if it's a GZIPInputStream.
    try {
      in = new GZIPInputStream(in);
    } catch (IOException e) {
      // If it is not compressed we close
      // the old one and recreate
      in.close();
      in = new FileInputStream(file);
    }
    entryEnumerationStream = new TarInputStream(in);
    curEntry = entryEnumerationStream.getNextEntry();
  }
 /**
  * Returns a new InputStream for the given file in the tar archive.
  *
  * @param entry
  * @return an input stream for the given file
  * @throws TarException
  * @throws IOException
  */
 public InputStream getInputStream(TarEntry entry) throws TarException, IOException {
   if (entryStream == null || !entryStream.skipToEntry(entry)) {
     InputStream in = new FileInputStream(file);
     // First, check if it's a GZIPInputStream.
     try {
       in = new GZIPInputStream(in);
     } catch (IOException e) {
       in = new FileInputStream(file);
     }
     entryStream =
         new TarInputStream(in, entry) {
           public void close() {
             // Ignore close() since we want to reuse the stream.
           }
         };
   }
   if (entryStream == null) {
     System.out.println("huh?"); // $NON-NLS-1$
   }
   return entryStream;
 }
 /**
  * Close the tar file input stream.
  *
  * @throws IOException if the file cannot be successfully closed
  */
 public void close() throws IOException {
   entryEnumerationStream.close();
 }
  public boolean extractTar(String asset, String target) {

    byte buf[] = new byte[1024 * 1024];

    InputStream assetStream = null;
    TarInputStream tis = null;

    try {
      assetStream = mAssetManager.open(asset, AssetManager.ACCESS_STREAMING);
      tis =
          new TarInputStream(
              new BufferedInputStream(
                  new GZIPInputStream(new BufferedInputStream(assetStream, 8192)), 8192));
    } catch (IOException e) {
      Log.e("python", "opening up extract tar", e);
      return false;
    }

    while (true) {
      TarEntry entry = null;

      try {
        entry = tis.getNextEntry();
      } catch (java.io.IOException e) {
        Log.e("python", "extracting tar", e);
        return false;
      }

      if (entry == null) {
        break;
      }

      Log.i("python", "extracting " + entry.getName());

      if (entry.isDirectory()) {

        try {
          new File(target + "/" + entry.getName()).mkdirs();
        } catch (SecurityException e) {
        }
        ;

        continue;
      }

      OutputStream out = null;
      String path = target + "/" + entry.getName();

      try {
        out = new BufferedOutputStream(new FileOutputStream(path), 8192);
      } catch (FileNotFoundException e) {
      } catch (SecurityException e) {
      }
      ;

      if (out == null) {
        Log.e("python", "could not open " + path);
        return false;
      }

      try {
        while (true) {
          int len = tis.read(buf);

          if (len == -1) {
            break;
          }

          out.write(buf, 0, len);
        }

        out.flush();
        out.close();
      } catch (java.io.IOException e) {
        Log.e("python", "extracting zip", e);
        return false;
      }
    }

    try {
      tis.close();
      assetStream.close();
    } catch (IOException e) {
      // pass
    }

    return true;
  }