xref: /dokuwiki/vendor/splitbrain/php-archive/src/Archive.php (revision 530d672995040c320a9506a0b93258a49c4d0b29)
1605f8e8dSAndreas Gohr<?php
2605f8e8dSAndreas Gohr
3605f8e8dSAndreas Gohrnamespace splitbrain\PHPArchive;
4605f8e8dSAndreas Gohr
5605f8e8dSAndreas Gohrabstract class Archive
6605f8e8dSAndreas Gohr{
7605f8e8dSAndreas Gohr
8605f8e8dSAndreas Gohr    const COMPRESS_AUTO = -1;
9605f8e8dSAndreas Gohr    const COMPRESS_NONE = 0;
10605f8e8dSAndreas Gohr    const COMPRESS_GZIP = 1;
11605f8e8dSAndreas Gohr    const COMPRESS_BZIP = 2;
12605f8e8dSAndreas Gohr
13605f8e8dSAndreas Gohr    /**
14605f8e8dSAndreas Gohr     * Set the compression level and type
15605f8e8dSAndreas Gohr     *
16605f8e8dSAndreas Gohr     * @param int $level Compression level (0 to 9)
17605f8e8dSAndreas Gohr     * @param int $type  Type of compression to use (use COMPRESS_* constants)
18605f8e8dSAndreas Gohr     * @return mixed
19605f8e8dSAndreas Gohr     */
20605f8e8dSAndreas Gohr    abstract public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO);
21605f8e8dSAndreas Gohr
22605f8e8dSAndreas Gohr    /**
23605f8e8dSAndreas Gohr     * Open an existing archive file for reading
24605f8e8dSAndreas Gohr     *
25605f8e8dSAndreas Gohr     * @param string $file
26605f8e8dSAndreas Gohr     * @throws ArchiveIOException
27605f8e8dSAndreas Gohr     */
28605f8e8dSAndreas Gohr    abstract public function open($file);
29605f8e8dSAndreas Gohr
30605f8e8dSAndreas Gohr    /**
31605f8e8dSAndreas Gohr     * Read the contents of an archive
32605f8e8dSAndreas Gohr     *
33605f8e8dSAndreas Gohr     * This function lists the files stored in the archive, and returns an indexed array of FileInfo objects
34605f8e8dSAndreas Gohr     *
35605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
36605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
37605f8e8dSAndreas Gohr     *
38605f8e8dSAndreas Gohr     * @return FileInfo[]
39605f8e8dSAndreas Gohr     */
40605f8e8dSAndreas Gohr    abstract public function contents();
41605f8e8dSAndreas Gohr
42605f8e8dSAndreas Gohr    /**
43605f8e8dSAndreas Gohr     * Extract an existing archive
44605f8e8dSAndreas Gohr     *
45605f8e8dSAndreas Gohr     * The $strip parameter allows you to strip a certain number of path components from the filenames
46605f8e8dSAndreas Gohr     * found in the archive file, similar to the --strip-components feature of GNU tar. This is triggered when
47605f8e8dSAndreas Gohr     * an integer is passed as $strip.
48605f8e8dSAndreas Gohr     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
49605f8e8dSAndreas Gohr     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
50605f8e8dSAndreas Gohr     *
51605f8e8dSAndreas Gohr     * By default this will extract all files found in the archive. You can restrict the output using the $include
52605f8e8dSAndreas Gohr     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
53605f8e8dSAndreas Gohr     * $include is set, only files that match this expression will be extracted. Files that match the $exclude
54605f8e8dSAndreas Gohr     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
55605f8e8dSAndreas Gohr     * stripped filenames as described above.
56605f8e8dSAndreas Gohr     *
57605f8e8dSAndreas Gohr     * The archive is closed afterwards. Reopen the file with open() again if you want to do additional operations
58605f8e8dSAndreas Gohr     *
59605f8e8dSAndreas Gohr     * @param string     $outdir  the target directory for extracting
60605f8e8dSAndreas Gohr     * @param int|string $strip   either the number of path components or a fixed prefix to strip
61605f8e8dSAndreas Gohr     * @param string     $exclude a regular expression of files to exclude
62605f8e8dSAndreas Gohr     * @param string     $include a regular expression of files to include
63605f8e8dSAndreas Gohr     * @throws ArchiveIOException
64605f8e8dSAndreas Gohr     * @return array
65605f8e8dSAndreas Gohr     */
66605f8e8dSAndreas Gohr    abstract public function extract($outdir, $strip = '', $exclude = '', $include = '');
67605f8e8dSAndreas Gohr
68605f8e8dSAndreas Gohr    /**
69605f8e8dSAndreas Gohr     * Create a new archive file
70605f8e8dSAndreas Gohr     *
71605f8e8dSAndreas Gohr     * If $file is empty, the archive file will be created in memory
72605f8e8dSAndreas Gohr     *
73605f8e8dSAndreas Gohr     * @param string $file
74605f8e8dSAndreas Gohr     */
75605f8e8dSAndreas Gohr    abstract public function create($file = '');
76605f8e8dSAndreas Gohr
77605f8e8dSAndreas Gohr    /**
78605f8e8dSAndreas Gohr     * Add a file to the current archive using an existing file in the filesystem
79605f8e8dSAndreas Gohr     *
80605f8e8dSAndreas Gohr     * @param string          $file     path to the original file
81605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
82605f8e8dSAndreas Gohr     * @throws ArchiveIOException
83605f8e8dSAndreas Gohr     */
84605f8e8dSAndreas Gohr    abstract public function addFile($file, $fileinfo = '');
85605f8e8dSAndreas Gohr
86605f8e8dSAndreas Gohr    /**
87605f8e8dSAndreas Gohr     * Add a file to the current archive using the given $data as content
88605f8e8dSAndreas Gohr     *
89605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
90605f8e8dSAndreas Gohr     * @param string          $data     binary content of the file to add
91605f8e8dSAndreas Gohr     * @throws ArchiveIOException
92605f8e8dSAndreas Gohr     */
93605f8e8dSAndreas Gohr    abstract public function addData($fileinfo, $data);
94605f8e8dSAndreas Gohr
95605f8e8dSAndreas Gohr    /**
96605f8e8dSAndreas Gohr     * Close the archive, close all file handles
97605f8e8dSAndreas Gohr     *
98605f8e8dSAndreas Gohr     * After a call to this function no more data can be added to the archive, for
99605f8e8dSAndreas Gohr     * read access no reading is allowed anymore
100605f8e8dSAndreas Gohr     */
101605f8e8dSAndreas Gohr    abstract public function close();
102605f8e8dSAndreas Gohr
103605f8e8dSAndreas Gohr    /**
104605f8e8dSAndreas Gohr     * Returns the created in-memory archive data
105605f8e8dSAndreas Gohr     *
106605f8e8dSAndreas Gohr     * This implicitly calls close() on the Archive
107605f8e8dSAndreas Gohr     */
108605f8e8dSAndreas Gohr    abstract public function getArchive();
109605f8e8dSAndreas Gohr
110605f8e8dSAndreas Gohr    /**
111605f8e8dSAndreas Gohr     * Save the created in-memory archive data
112605f8e8dSAndreas Gohr     *
113605f8e8dSAndreas Gohr     * Note: It is more memory effective to specify the filename in the create() function and
114605f8e8dSAndreas Gohr     * let the library work on the new file directly.
115605f8e8dSAndreas Gohr     *
116605f8e8dSAndreas Gohr     * @param string $file
117605f8e8dSAndreas Gohr     */
118605f8e8dSAndreas Gohr    abstract public function save($file);
119605f8e8dSAndreas Gohr
120605f8e8dSAndreas Gohr}
121605f8e8dSAndreas Gohr
122605f8e8dSAndreas Gohrclass ArchiveIOException extends \Exception
123605f8e8dSAndreas Gohr{
124605f8e8dSAndreas Gohr}
125605f8e8dSAndreas Gohr
126605f8e8dSAndreas Gohrclass ArchiveIllegalCompressionException extends \Exception
127605f8e8dSAndreas Gohr{
128605f8e8dSAndreas Gohr}
129*530d6729SAndreas Gohr
130*530d6729SAndreas Gohrclass ArchiveCorruptedException extends \Exception
131*530d6729SAndreas Gohr{
132*530d6729SAndreas Gohr}
133