ArchiveExtractor
in package
Extracts dictionary archives (.zip, .tar.gz, .tgz, .tar.bz2, .tar.xz) to a temp directory so multi-file formats like StarDict can be imported via a single file upload.
Hardened against zip-bombs (file-count cap), path traversal, and stale temp files (caller-driven cleanup).
Tags
Table of Contents
Constants
- MAX_FILES = 500
- Maximum files allowed in an archive.
Methods
- cleanup() : void
- Remove temp files/directories created during a previous extract().
- extract() : string
- Extract an archive to a fresh temp directory.
- findByExtensions() : string|null
- Recursively scan a directory for the first file whose extension matches one of $extensions.
- isArchive() : bool
- Decide whether a filename looks like a supported archive.
- extractTar() : string
- Extract a tar archive (.tar.xz, .tar.gz, .tar.bz2, .tgz) using the system `tar` command.
- extractZip() : string
- Extract a ZIP archive into a fresh temp directory.
- makeTempDir() : string
- Allocate a fresh temp directory. Throws on failure.
- removeDir() : void
- Recursively remove a directory and all its contents.
Constants
MAX_FILES
Maximum files allowed in an archive.
public
mixed
MAX_FILES
= 500
Methods
cleanup()
Remove temp files/directories created during a previous extract().
public
cleanup(string ...$paths) : void
Parameters
- $paths : string
extract()
Extract an archive to a fresh temp directory.
public
extract(string $archivePath, string $originalName) : string
Parameters
- $archivePath : string
-
On-disk path to the archive (typically a PHP upload tmp_name).
- $originalName : string
-
Original filename, used to detect archive type.
Tags
Return values
string —Path to the extraction directory.
findByExtensions()
Recursively scan a directory for the first file whose extension matches one of $extensions.
public
findByExtensions(string $directory, array<int, string> $extensions) : string|null
Parameters
- $directory : string
-
Directory to scan.
- $extensions : array<int, string>
-
Lower-case extensions without the dot (e.g. ['ifo']).
Return values
string|null —Absolute path to the first match, or null if none found.
isArchive()
Decide whether a filename looks like a supported archive.
public
static isArchive(string $name) : bool
Parameters
- $name : string
Return values
boolextractTar()
Extract a tar archive (.tar.xz, .tar.gz, .tar.bz2, .tgz) using the system `tar` command.
private
extractTar(string $tarPath) : string
Parameters
- $tarPath : string
Tags
Return values
stringextractZip()
Extract a ZIP archive into a fresh temp directory.
private
extractZip(string $zipPath) : string
Parameters
- $zipPath : string
Tags
Return values
stringmakeTempDir()
Allocate a fresh temp directory. Throws on failure.
private
makeTempDir() : string
Return values
stringremoveDir()
Recursively remove a directory and all its contents.
private
removeDir(string $dir) : void
Parameters
- $dir : string