private void writeHeader(final CpioArchiveEntry e) throws IOException {
   switch (e.getFormat()) {
     case FORMAT_NEW:
       out.write(ArchiveUtils.toAsciiBytes(MAGIC_NEW));
       count(6);
       writeNewEntry(e);
       break;
     case FORMAT_NEW_CRC:
       out.write(ArchiveUtils.toAsciiBytes(MAGIC_NEW_CRC));
       count(6);
       writeNewEntry(e);
       break;
     case FORMAT_OLD_ASCII:
       out.write(ArchiveUtils.toAsciiBytes(MAGIC_OLD_ASCII));
       count(6);
       writeOldAsciiEntry(e);
       break;
     case FORMAT_OLD_BINARY:
       final boolean swapHalfWord = true;
       writeBinaryLong(MAGIC_OLD_BINARY, 2, swapHalfWord);
       writeOldBinaryEntry(e, swapHalfWord);
       break;
     default:
       throw new IOException("unknown format " + e.getFormat());
   }
 }
  private void writeAsciiLong(final long number, final int length, final int radix)
      throws IOException {
    final StringBuilder tmp = new StringBuilder();
    String tmpStr;
    if (radix == 16) {
      tmp.append(Long.toHexString(number));
    } else if (radix == 8) {
      tmp.append(Long.toOctalString(number));
    } else {
      tmp.append(Long.toString(number));
    }

    if (tmp.length() <= length) {
      final int insertLength = length - tmp.length();
      for (int pos = 0; pos < insertLength; pos++) {
        tmp.insert(0, "0");
      }
      tmpStr = tmp.toString();
    } else {
      tmpStr = tmp.substring(tmp.length() - length);
    }
    final byte[] b = ArchiveUtils.toAsciiBytes(tmpStr);
    out.write(b);
    count(b.length);
  }
Esempio n. 3
0
  /**
   * Checks if the signature matches what is expected for a tar file.
   *
   * @param signature the bytes to check
   * @param length the number of bytes to check
   * @return true, if this stream is a tar archive stream, false otherwise
   */
  public static boolean matches(byte[] signature, int length) {
    if (length < TarConstants.VERSION_OFFSET + TarConstants.VERSIONLEN) {
      return false;
    }

    if (ArchiveUtils.matchAsciiBuffer(
            TarConstants.MAGIC_POSIX, signature, TarConstants.MAGIC_OFFSET, TarConstants.MAGICLEN)
        && ArchiveUtils.matchAsciiBuffer(
            TarConstants.VERSION_POSIX,
            signature,
            TarConstants.VERSION_OFFSET,
            TarConstants.VERSIONLEN)) {
      return true;
    }
    if (ArchiveUtils.matchAsciiBuffer(
            TarConstants.MAGIC_GNU, signature, TarConstants.MAGIC_OFFSET, TarConstants.MAGICLEN)
        && (ArchiveUtils.matchAsciiBuffer(
                TarConstants.VERSION_GNU_SPACE,
                signature,
                TarConstants.VERSION_OFFSET,
                TarConstants.VERSIONLEN)
            || ArchiveUtils.matchAsciiBuffer(
                TarConstants.VERSION_GNU_ZERO,
                signature,
                TarConstants.VERSION_OFFSET,
                TarConstants.VERSIONLEN))) {
      return true;
    }
    // COMPRESS-107 - recognise Ant tar files
    if (ArchiveUtils.matchAsciiBuffer(
            TarConstants.MAGIC_ANT, signature, TarConstants.MAGIC_OFFSET, TarConstants.MAGICLEN)
        && ArchiveUtils.matchAsciiBuffer(
            TarConstants.VERSION_ANT,
            signature,
            TarConstants.VERSION_OFFSET,
            TarConstants.VERSIONLEN)) {
      return true;
    }
    return false;
  }