Oihana PHP

tarCompressionOfFile.php

Table of Contents

Functions

tarCompressionOfFile()  : string|null
Reports how an archive is compressed, read from its own first bytes.

Functions

tarCompressionOfFile()

Reports how an archive is compressed, read from its own first bytes.

tarCompressionOfFile(string $tarFile) : string|null

Not from its name. tarIsCompressed() answers the same question from the extension, which is all that is needed to pick a code path for an archive this library wrote — but an archive handed to untar() may be named anything, and the system tar has to be told nothing it cannot verify. Three bytes settle it.

Scope: gzip, bzip2, xz, zstd, or none — the compressions this library writes (TarExtension::getExtensionForCompression()) and the ones hasTarExtension lets through. xz and zstd joined in 1.5.0, and this function is one of the six places that had to follow for an archive to be readable as well as writable: a format written and not read back is the defect that lot was fixing.

Parameters
$tarFile : string

Path to the archive.

Tags
example
use function oihana\files\archive\tar\tarCompressionOfFile;

tarCompressionOfFile('/tmp/site.tar.gz') ;  // 'gzip'
tarCompressionOfFile('/tmp/site.tar.zst') ; // 'zstd'
tarCompressionOfFile('/tmp/site.tar') ;     // null
author

Marc Alcaraz (ekameleon)

since
1.5.0
Return values
string|null —

CompressionType::GZIP, CompressionType::BZIP2, or null for an uncompressed archive — and for one that cannot be read, since an unreadable file is not a compressed one.

On this page

Search results