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