xref: /dokuwiki/vendor/splitbrain/php-archive/src/Archive.php (revision 605f8e8d0e501057749c50581087ce05089c1af3)
1*605f8e8dSAndreas Gohr<?php
2*605f8e8dSAndreas Gohr
3*605f8e8dSAndreas Gohrnamespace splitbrain\PHPArchive;
4*605f8e8dSAndreas Gohr
5*605f8e8dSAndreas Gohrabstract class Archive
6*605f8e8dSAndreas Gohr{
7*605f8e8dSAndreas Gohr
8*605f8e8dSAndreas Gohr    const COMPRESS_AUTO = -1;
9*605f8e8dSAndreas Gohr    const COMPRESS_NONE = 0;
10*605f8e8dSAndreas Gohr    const COMPRESS_GZIP = 1;
11*605f8e8dSAndreas Gohr    const COMPRESS_BZIP = 2;
12*605f8e8dSAndreas Gohr
13*605f8e8dSAndreas Gohr    /**
14*605f8e8dSAndreas Gohr     * Set the compression level and type
15*605f8e8dSAndreas Gohr     *
16*605f8e8dSAndreas Gohr     * @param int $level Compression level (0 to 9)
17*605f8e8dSAndreas Gohr     * @param int $type  Type of compression to use (use COMPRESS_* constants)
18*605f8e8dSAndreas Gohr     * @return mixed
19*605f8e8dSAndreas Gohr     */
20*605f8e8dSAndreas Gohr    abstract public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO);
21*605f8e8dSAndreas Gohr
22*605f8e8dSAndreas Gohr    /**
23*605f8e8dSAndreas Gohr     * Open an existing archive file for reading
24*605f8e8dSAndreas Gohr     *
25*605f8e8dSAndreas Gohr     * @param string $file
26*605f8e8dSAndreas Gohr     * @throws ArchiveIOException
27*605f8e8dSAndreas Gohr     */
28*605f8e8dSAndreas Gohr    abstract public function open($file);
29*605f8e8dSAndreas Gohr
30*605f8e8dSAndreas Gohr    /**
31*605f8e8dSAndreas Gohr     * Read the contents of an archive
32*605f8e8dSAndreas Gohr     *
33*605f8e8dSAndreas Gohr     * This function lists the files stored in the archive, and returns an indexed array of FileInfo objects
34*605f8e8dSAndreas Gohr     *
35*605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
36*605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
37*605f8e8dSAndreas Gohr     *
38*605f8e8dSAndreas Gohr     * @return FileInfo[]
39*605f8e8dSAndreas Gohr     */
40*605f8e8dSAndreas Gohr    abstract public function contents();
41*605f8e8dSAndreas Gohr
42*605f8e8dSAndreas Gohr    /**
43*605f8e8dSAndreas Gohr     * Extract an existing archive
44*605f8e8dSAndreas Gohr     *
45*605f8e8dSAndreas Gohr     * The $strip parameter allows you to strip a certain number of path components from the filenames
46*605f8e8dSAndreas Gohr     * found in the archive file, similar to the --strip-components feature of GNU tar. This is triggered when
47*605f8e8dSAndreas Gohr     * an integer is passed as $strip.
48*605f8e8dSAndreas Gohr     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
49*605f8e8dSAndreas Gohr     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
50*605f8e8dSAndreas Gohr     *
51*605f8e8dSAndreas Gohr     * By default this will extract all files found in the archive. You can restrict the output using the $include
52*605f8e8dSAndreas Gohr     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
53*605f8e8dSAndreas Gohr     * $include is set, only files that match this expression will be extracted. Files that match the $exclude
54*605f8e8dSAndreas Gohr     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
55*605f8e8dSAndreas Gohr     * stripped filenames as described above.
56*605f8e8dSAndreas Gohr     *
57*605f8e8dSAndreas Gohr     * The archive is closed afterwards. Reopen the file with open() again if you want to do additional operations
58*605f8e8dSAndreas Gohr     *
59*605f8e8dSAndreas Gohr     * @param string     $outdir  the target directory for extracting
60*605f8e8dSAndreas Gohr     * @param int|string $strip   either the number of path components or a fixed prefix to strip
61*605f8e8dSAndreas Gohr     * @param string     $exclude a regular expression of files to exclude
62*605f8e8dSAndreas Gohr     * @param string     $include a regular expression of files to include
63*605f8e8dSAndreas Gohr     * @throws ArchiveIOException
64*605f8e8dSAndreas Gohr     * @return array
65*605f8e8dSAndreas Gohr     */
66*605f8e8dSAndreas Gohr    abstract public function extract($outdir, $strip = '', $exclude = '', $include = '');
67*605f8e8dSAndreas Gohr
68*605f8e8dSAndreas Gohr    /**
69*605f8e8dSAndreas Gohr     * Create a new archive file
70*605f8e8dSAndreas Gohr     *
71*605f8e8dSAndreas Gohr     * If $file is empty, the archive file will be created in memory
72*605f8e8dSAndreas Gohr     *
73*605f8e8dSAndreas Gohr     * @param string $file
74*605f8e8dSAndreas Gohr     */
75*605f8e8dSAndreas Gohr    abstract public function create($file = '');
76*605f8e8dSAndreas Gohr
77*605f8e8dSAndreas Gohr    /**
78*605f8e8dSAndreas Gohr     * Add a file to the current archive using an existing file in the filesystem
79*605f8e8dSAndreas Gohr     *
80*605f8e8dSAndreas Gohr     * @param string          $file     path to the original file
81*605f8e8dSAndreas 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
82*605f8e8dSAndreas Gohr     * @throws ArchiveIOException
83*605f8e8dSAndreas Gohr     */
84*605f8e8dSAndreas Gohr    abstract public function addFile($file, $fileinfo = '');
85*605f8e8dSAndreas Gohr
86*605f8e8dSAndreas Gohr    /**
87*605f8e8dSAndreas Gohr     * Add a file to the current archive using the given $data as content
88*605f8e8dSAndreas Gohr     *
89*605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
90*605f8e8dSAndreas Gohr     * @param string          $data     binary content of the file to add
91*605f8e8dSAndreas Gohr     * @throws ArchiveIOException
92*605f8e8dSAndreas Gohr     */
93*605f8e8dSAndreas Gohr    abstract public function addData($fileinfo, $data);
94*605f8e8dSAndreas Gohr
95*605f8e8dSAndreas Gohr    /**
96*605f8e8dSAndreas Gohr     * Close the archive, close all file handles
97*605f8e8dSAndreas Gohr     *
98*605f8e8dSAndreas Gohr     * After a call to this function no more data can be added to the archive, for
99*605f8e8dSAndreas Gohr     * read access no reading is allowed anymore
100*605f8e8dSAndreas Gohr     */
101*605f8e8dSAndreas Gohr    abstract public function close();
102*605f8e8dSAndreas Gohr
103*605f8e8dSAndreas Gohr    /**
104*605f8e8dSAndreas Gohr     * Returns the created in-memory archive data
105*605f8e8dSAndreas Gohr     *
106*605f8e8dSAndreas Gohr     * This implicitly calls close() on the Archive
107*605f8e8dSAndreas Gohr     */
108*605f8e8dSAndreas Gohr    abstract public function getArchive();
109*605f8e8dSAndreas Gohr
110*605f8e8dSAndreas Gohr    /**
111*605f8e8dSAndreas Gohr     * Save the created in-memory archive data
112*605f8e8dSAndreas Gohr     *
113*605f8e8dSAndreas Gohr     * Note: It is more memory effective to specify the filename in the create() function and
114*605f8e8dSAndreas Gohr     * let the library work on the new file directly.
115*605f8e8dSAndreas Gohr     *
116*605f8e8dSAndreas Gohr     * @param string $file
117*605f8e8dSAndreas Gohr     */
118*605f8e8dSAndreas Gohr    abstract public function save($file);
119*605f8e8dSAndreas Gohr
120*605f8e8dSAndreas Gohr}
121*605f8e8dSAndreas Gohr
122*605f8e8dSAndreas Gohrclass ArchiveIOException extends \Exception
123*605f8e8dSAndreas Gohr{
124*605f8e8dSAndreas Gohr}
125*605f8e8dSAndreas Gohr
126*605f8e8dSAndreas Gohrclass ArchiveIllegalCompressionException extends \Exception
127*605f8e8dSAndreas Gohr{
128*605f8e8dSAndreas Gohr}
129