/**
   * Put an entry on the output stream. This writes the entry's header record and positions the
   * output stream for writing the contents of the entry. Once this method is called, the stream is
   * ready for calls to write() to write the entry's contents. Once the contents are written,
   * closeEntry() <B>MUST be called to ensure that all buffered data is completely written to the
   * output stream.
   *
   * @param entry The TarEntry to be written to the archive.
   */
  public void putNextEntry(TarEntry entry) throws IOException {
    StringBuffer name = entry.getHeader().name;

    // NOTE
    // This check is not adequate, because the maximum file length that
    // can be placed into a POSIX (ustar) header depends on the precise
    // locations of the path elements (slashes) within the file's full
    // pathname. For this reason, writeEntryHeader() can still throw an
    // InvalidHeaderException if the file's full pathname will not fit
    // in the header.

    if ((entry.isUnixTarFormat() && name.length() > TarHeader.NAMELEN)
        || (!entry.isUnixTarFormat()
            && name.length() > (TarHeader.NAMELEN + TarHeader.PREFIXLEN))) {
      throw new InvalidHeaderException(
          "file name '"
              + name
              + "' is too long ( "
              + name.length()
              + " > "
              + (entry.isUnixTarFormat()
                  ? TarHeader.NAMELEN
                  : (TarHeader.NAMELEN + TarHeader.PREFIXLEN))
              + " bytes )");
    }

    entry.writeEntryHeader(this.recordBuf);

    this.buffer.writeRecord(this.recordBuf);

    this.currBytes = 0;

    if (entry.isDirectory()) this.currSize = 0;
    else this.currSize = entry.getSize();
  }
예제 #2
0
  /** Sets the details for this file object. */
  protected void setTarEntry(final TarEntry entry) {
    if (this.entry != null) {
      return;
    }

    if ((entry == null) || (entry.isDirectory())) {
      type = FileType.FOLDER;
    } else {
      type = FileType.FILE;
    }

    this.entry = entry;
  }
  /**
   * Checks if the bytes being written exceed the current entry size.
   *
   * @see java.io.FilterOutputStream#write(byte[], int, int)
   */
  @Override
  public void write(byte[] b, int off, int len) throws IOException {
    if (currentEntry != null && !currentEntry.isDirectory()) {
      if (currentEntry.getSize() < currentFileSize + len) {
        throw new IOException(
            "The current entry["
                + currentEntry.getName()
                + "] size["
                + currentEntry.getSize()
                + "] is smaller than the bytes["
                + (currentFileSize + len)
                + "] being written.");
      }
    }

    out.write(b, off, len);

    bytesWritten += len;

    if (currentEntry != null) {
      currentFileSize += len;
    }
  }
  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;
  }