Skip to contents

archive_format() names the container format the bytes start with, from the magic numbers the format specifications define. It reads at most the first 512 bytes of a file.

Usage

archive_format(x)

Arguments

x

A path to a file (a single string), a raw vector, or a handle from archive_open().

Value

"zip", "tar", "cfb", or NA when the bytes start with none of those magic numbers. For a handle, the format it reads the archive as.

Details

The magic numbers are, at the offsets the specifications give:

  • ZIP (APPNOTE.TXT 4.3.7 and 4.3.16): a local file header, PK\3\4, or, for an archive with no members, the end of central directory record, PK\5\6, at offset 0.

  • tar (POSIX ustar): ustar\0 at offset 257, or GNU tar's ustar \0.

  • Compound File (MS-CFB, section 2.2): D0 CF 11 E0 A1 B1 1A E1 at offset 0.

ZIP and Compound File are tried first, because a tar archive starts with a member name, which may be any bytes. A ZIP archive with data before its first member, such as a self-extracting one, and a tar archive older than POSIX ustar are not detected; archive_open() opens them when given format.

Bytes with none of those magic numbers that start with gzip's (1F 8B, RFC 1952), zstd's (28 B5 2F FD, RFC 8878) or an LZ4 frame's (04 22 4D 18) are decoded until they yield 512 bytes, and those are looked at for tar's magic number: a .tar.gz, .tar.zst or .tar.lz4 is "tar". Nothing else is read inside a compression, so a gzip of a ZIP archive is NA.

Conditions

zuarchive_error_path when x is none of the three, is NA or empty, or names a directory; zuarchive_error_not_found when the path does not exist or cannot be read. A handle that is not open raises zuarchive_error; one whose file has changed format since it was opened raises zuarchive_error_format.

See also

Examples

archive_format(as.raw(c(0x50, 0x4b, 0x03, 0x04)))   # ZIP local header
#> [1] "zip"
archive_format(as.raw(c(0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1)))
#> [1] "cfb"
archive_format(charToRaw("plain text"))
#> [1] NA