Archive_Tar.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. Documentation for class Archive_Tar
  2. ===================================
  3. Last update : 2001-08-15
  4. Overview :
  5. ----------
  6. The Archive_Tar class helps in creating and managing GNU TAR format
  7. files compressed by GNU ZIP or not.
  8. The class offers basic functions like creating an archive, adding
  9. files in the archive, extracting files from the archive and listing
  10. the archive content.
  11. It also provide advanced functions that allow the adding and
  12. extraction of files with path manipulation.
  13. Sample :
  14. --------
  15. // ----- Creating the object (uncompressed archive)
  16. $tar_object = new Archive_Tar("tarname.tar");
  17. $tar_object->setErrorHandling(PEAR_ERROR_PRINT);
  18. // ----- Creating the archive
  19. $v_list[0]="file.txt";
  20. $v_list[1]="data/";
  21. $v_list[2]="file.log";
  22. $tar_object->create($v_list);
  23. // ----- Adding files
  24. $v_list[0]="dev/file.txt";
  25. $v_list[1]="dev/data/";
  26. $v_list[2]="log/file.log";
  27. $tar_object->add($v_list);
  28. // ----- Adding more files
  29. $tar_object->add("release/newfile.log release/readme.txt");
  30. // ----- Listing the content
  31. if (($v_list = $tar_object->listContent()) != 0)
  32. for ($i=0; $i<sizeof($v_list); $i++)
  33. {
  34. echo "Filename :'".$v_list[$i][filename]."'<br>";
  35. echo " .size :'".$v_list[$i][size]."'<br>";
  36. echo " .mtime :'".$v_list[$i][mtime]."' (".date("l dS of F Y h:i:s A", $v_list[$i][mtime]).")<br>";
  37. echo " .mode :'".$v_list[$i][mode]."'<br>";
  38. echo " .uid :'".$v_list[$i][uid]."'<br>";
  39. echo " .gid :'".$v_list[$i][gid]."'<br>";
  40. echo " .typeflag :'".$v_list[$i][typeflag]."'<br>";
  41. }
  42. // ----- Extracting the archive in directory "install"
  43. $tar_object->extract("install");
  44. Public arguments :
  45. ------------------
  46. None
  47. Public Methods :
  48. ----------------
  49. Method : Archive_Tar($p_tarname, $compress = null)
  50. Description :
  51. Archive_Tar Class constructor. This flavour of the constructor only
  52. declare a new Archive_Tar object, identifying it by the name of the
  53. tar file.
  54. If the compress argument is set the tar will be read or created as a
  55. gzip or bz2 compressed TAR file.
  56. Arguments :
  57. $p_tarname : A valid filename for the tar archive file.
  58. $p_compress : can be null, 'gz' or 'bz2'. For
  59. compatibility reason it can also be true. This
  60. parameter indicates if gzip or bz2 compression
  61. is required.
  62. Return value :
  63. The Archive_Tar object.
  64. Sample :
  65. $tar_object = new Archive_Tar("tarname.tar");
  66. $tar_object_compressed = new Archive_Tar("tarname.tgz", true);
  67. How it works :
  68. Initialize the object.
  69. Method : create($p_filelist)
  70. Description :
  71. This method creates the archive file and add the files / directories
  72. that are listed in $p_filelist.
  73. If the file already exists and is writable, it is replaced by the
  74. new tar. It is a create and not an add. If the file exists and is
  75. read-only or is a directory it is not replaced. The method return
  76. false and a PEAR error text.
  77. The $p_filelist parameter can be an array of string, each string
  78. representing a filename or a directory name with their path if
  79. needed. It can also be a single string with names separated by a
  80. single blank.
  81. See also createModify() method for more details.
  82. Arguments :
  83. $p_filelist : An array of filenames and directory names, or a single
  84. string with names separated by a single blank space.
  85. Return value :
  86. true on success, false on error.
  87. Sample 1 :
  88. $tar_object = new Archive_Tar("tarname.tar");
  89. $tar_object->setErrorHandling(PEAR_ERROR_PRINT); // Optional error handling
  90. $v_list[0]="file.txt";
  91. $v_list[1]="data/"; (Optional '/' at the end)
  92. $v_list[2]="file.log";
  93. $tar_object->create($v_list);
  94. Sample 2 :
  95. $tar_object = new Archive_Tar("tarname.tar");
  96. $tar_object->setErrorHandling(PEAR_ERROR_PRINT); // Optional error handling
  97. $tar_object->create("file.txt data/ file.log");
  98. How it works :
  99. Just calling the createModify() method with the right parameters.
  100. Method : createModify($p_filelist, $p_add_dir, $p_remove_dir = "")
  101. Description :
  102. This method creates the archive file and add the files / directories
  103. that are listed in $p_filelist.
  104. If the file already exists and is writable, it is replaced by the
  105. new tar. It is a create and not an add. If the file exists and is
  106. read-only or is a directory it is not replaced. The method return
  107. false and a PEAR error text.
  108. The $p_filelist parameter can be an array of string, each string
  109. representing a filename or a directory name with their path if
  110. needed. It can also be a single string with names separated by a
  111. single blank.
  112. The path indicated in $p_remove_dir will be removed from the
  113. memorized path of each file / directory listed when this path
  114. exists. By default nothing is removed (empty path "")
  115. The path indicated in $p_add_dir will be added at the beginning of
  116. the memorized path of each file / directory listed. However it can
  117. be set to empty "". The adding of a path is done after the removing
  118. of path.
  119. The path add/remove ability enables the user to prepare an archive
  120. for extraction in a different path than the origin files are.
  121. See also addModify() method for file adding properties.
  122. Arguments :
  123. $p_filelist : An array of filenames and directory names, or a single
  124. string with names separated by a single blank space.
  125. $p_add_dir : A string which contains a path to be added to the
  126. memorized path of each element in the list.
  127. $p_remove_dir : A string which contains a path to be removed from
  128. the memorized path of each element in the list, when
  129. relevant.
  130. Return value :
  131. true on success, false on error.
  132. Sample 1 :
  133. $tar_object = new Archive_Tar("tarname.tar");
  134. $tar_object->setErrorHandling(PEAR_ERROR_PRINT); // Optional error handling
  135. $v_list[0]="file.txt";
  136. $v_list[1]="data/"; (Optional '/' at the end)
  137. $v_list[2]="file.log";
  138. $tar_object->createModify($v_list, "install");
  139. // files are stored in the archive as :
  140. // install/file.txt
  141. // install/data
  142. // install/data/file1.txt
  143. // install/data/... all the files and sub-dirs of data/
  144. // install/file.log
  145. Sample 2 :
  146. $tar_object = new Archive_Tar("tarname.tar");
  147. $tar_object->setErrorHandling(PEAR_ERROR_PRINT); // Optional error handling
  148. $v_list[0]="dev/file.txt";
  149. $v_list[1]="dev/data/"; (Optional '/' at the end)
  150. $v_list[2]="log/file.log";
  151. $tar_object->createModify($v_list, "install", "dev");
  152. // files are stored in the archive as :
  153. // install/file.txt
  154. // install/data
  155. // install/data/file1.txt
  156. // install/data/... all the files and sub-dirs of data/
  157. // install/log/file.log
  158. How it works :
  159. Open the file in write mode (erasing the existing one if one),
  160. call the _addList() method for adding the files in an empty archive,
  161. add the tar footer (512 bytes block), close the tar file.
  162. Method : addModify($p_filelist, $p_add_dir, $p_remove_dir="")
  163. Description :
  164. This method add the files / directories listed in $p_filelist at the
  165. end of the existing archive. If the archive does not yet exists it
  166. is created.
  167. The $p_filelist parameter can be an array of string, each string
  168. representing a filename or a directory name with their path if
  169. needed. It can also be a single string with names separated by a
  170. single blank.
  171. The path indicated in $p_remove_dir will be removed from the
  172. memorized path of each file / directory listed when this path
  173. exists. By default nothing is removed (empty path "")
  174. The path indicated in $p_add_dir will be added at the beginning of
  175. the memorized path of each file / directory listed. However it can
  176. be set to empty "". The adding of a path is done after the removing
  177. of path.
  178. The path add/remove ability enables the user to prepare an archive
  179. for extraction in a different path than the origin files are.
  180. If a file/dir is already in the archive it will only be added at the
  181. end of the archive. There is no update of the existing archived
  182. file/dir. However while extracting the archive, the last file will
  183. replace the first one. This results in a none optimization of the
  184. archive size.
  185. If a file/dir does not exist the file/dir is ignored. However an
  186. error text is send to PEAR error.
  187. If a file/dir is not readable the file/dir is ignored. However an
  188. error text is send to PEAR error.
  189. If the resulting filename/dirname (after the add/remove option or
  190. not) string is greater than 99 char, the file/dir is
  191. ignored. However an error text is send to PEAR error.
  192. Arguments :
  193. $p_filelist : An array of filenames and directory names, or a single
  194. string with names separated by a single blank space.
  195. $p_add_dir : A string which contains a path to be added to the
  196. memorized path of each element in the list.
  197. $p_remove_dir : A string which contains a path to be removed from
  198. the memorized path of each element in the list, when
  199. relevant.
  200. Return value :
  201. true on success, false on error.
  202. Sample 1 :
  203. $tar_object = new Archive_Tar("tarname.tar");
  204. [...]
  205. $v_list[0]="dev/file.txt";
  206. $v_list[1]="dev/data/"; (Optional '/' at the end)
  207. $v_list[2]="log/file.log";
  208. $tar_object->addModify($v_list, "install");
  209. // files are stored in the archive as :
  210. // install/file.txt
  211. // install/data
  212. // install/data/file1.txt
  213. // install/data/... all the files and sub-dirs of data/
  214. // install/file.log
  215. Sample 2 :
  216. $tar_object = new Archive_Tar("tarname.tar");
  217. [...]
  218. $v_list[0]="dev/file.txt";
  219. $v_list[1]="dev/data/"; (Optional '/' at the end)
  220. $v_list[2]="log/file.log";
  221. $tar_object->addModify($v_list, "install", "dev");
  222. // files are stored in the archive as :
  223. // install/file.txt
  224. // install/data
  225. // install/data/file1.txt
  226. // install/data/... all the files and sub-dirs of data/
  227. // install/log/file.log
  228. How it works :
  229. If the archive does not exists it create it and add the files.
  230. If the archive does exists and is not compressed, it open it, jump
  231. before the last empty 512 bytes block (tar footer) and add the files
  232. at this point.
  233. If the archive does exists and is compressed, a temporary copy file
  234. is created. This temporary file is then 'gzip' read block by block
  235. until the last empty block. The new files are then added in the
  236. compressed file.
  237. The adding of files is done by going through the file/dir list,
  238. adding files per files, in a recursive way through the
  239. directory. Each time a path need to be added/removed it is done
  240. before writing the file header in the archive.
  241. Method : add($p_filelist)
  242. Description :
  243. This method add the files / directories listed in $p_filelist at the
  244. end of the existing archive. If the archive does not yet exists it
  245. is created.
  246. The $p_filelist parameter can be an array of string, each string
  247. representing a filename or a directory name with their path if
  248. needed. It can also be a single string with names separated by a
  249. single blank.
  250. See addModify() method for details and limitations.
  251. Arguments :
  252. $p_filelist : An array of filenames and directory names, or a single
  253. string with names separated by a single blank space.
  254. Return value :
  255. true on success, false on error.
  256. Sample 1 :
  257. $tar_object = new Archive_Tar("tarname.tar");
  258. [...]
  259. $v_list[0]="dev/file.txt";
  260. $v_list[1]="dev/data/"; (Optional '/' at the end)
  261. $v_list[2]="log/file.log";
  262. $tar_object->add($v_list);
  263. Sample 2 :
  264. $tar_object = new Archive_Tar("tarname.tgz", true);
  265. [...]
  266. $v_list[0]="dev/file.txt";
  267. $v_list[1]="dev/data/"; (Optional '/' at the end)
  268. $v_list[2]="log/file.log";
  269. $tar_object->add($v_list);
  270. How it works :
  271. Simply call the addModify() method with the right parameters.
  272. Method : addString($p_filename, $p_string, $p_datetime, $p_params)
  273. Description :
  274. This method add a single string as a file at the
  275. end of the existing archive. If the archive does not yet exists it
  276. is created.
  277. Arguments :
  278. $p_filename : A string which contains the full filename path
  279. that will be associated with the string.
  280. $p_string : The content of the file added in the archive.
  281. $p_datetime : (Optional) Timestamp of the file (default = now)
  282. $p_params : (Optional) Various file metadata:
  283. stamp - As above, timestamp of the file
  284. mode - UNIX-style permissions (default 0600)
  285. type - Is this a regular file or link (see TAR
  286. format spec for how to create a hard/symlink)
  287. uid - UNIX-style user ID (default 0 = root)
  288. gid - UNIX-style group ID (default 0 = root)
  289. Return value :
  290. true on success, false on error.
  291. Sample 1 :
  292. $v_archive = & new Archive_Tar($p_filename);
  293. $v_archive->setErrorHandling(PEAR_ERROR_PRINT);
  294. $v_result = $v_archive->addString('data/test.txt', 'This is the text of the string');
  295. $v_result = $v_archive->addString(
  296. 'data/test.sh',
  297. "#!/bin/sh\necho 'Hello'",
  298. time(),
  299. array( "mode" => 0755, "uid" => 34 )
  300. );
  301. Method : extract($p_path = "")
  302. Description :
  303. This method extract all the content of the archive in the directory
  304. indicated by $p_path.If $p_path is optional, if not set the archive
  305. is extracted in the current directory.
  306. While extracting a file, if the directory path does not exists it is
  307. created.
  308. See extractModify() for details and limitations.
  309. Arguments :
  310. $p_path : Optional path where the files/dir need to by extracted.
  311. Return value :
  312. true on success, false on error.
  313. Sample :
  314. $tar_object = new Archive_Tar("tarname.tar");
  315. $tar_object->extract();
  316. How it works :
  317. Simply call the extractModify() method with appropriate parameters.
  318. Method : extractModify($p_path, $p_remove_path)
  319. Description :
  320. This method extract all the content of the archive in the directory
  321. indicated by $p_path. When relevant the memorized path of the
  322. files/dir can be modified by removing the $p_remove_path path at the
  323. beginning of the file/dir path.
  324. While extracting a file, if the directory path does not exists it is
  325. created.
  326. While extracting a file, if the file already exists it is replaced
  327. without looking for last modification date.
  328. While extracting a file, if the file already exists and is write
  329. protected, the extraction is aborted.
  330. While extracting a file, if a directory with the same name already
  331. exists, the extraction is aborted.
  332. While extracting a directory, if a file with the same name already
  333. exists, the extraction is aborted.
  334. While extracting a file/directory if the destination directory exist
  335. and is write protected, or does not exist but can not be created,
  336. the extraction is aborted.
  337. If after extraction an extracted file does not show the correct
  338. stored file size, the extraction is aborted.
  339. When the extraction is aborted, a PEAR error text is set and false
  340. is returned. However the result can be a partial extraction that may
  341. need to be manually cleaned.
  342. Arguments :
  343. $p_path : The path of the directory where the files/dir need to by
  344. extracted.
  345. $p_remove_path : Part of the memorized path that can be removed if
  346. present at the beginning of the file/dir path.
  347. Return value :
  348. true on success, false on error.
  349. Sample :
  350. // Imagine tarname.tar with files :
  351. // dev/data/file.txt
  352. // dev/data/log.txt
  353. // readme.txt
  354. $tar_object = new Archive_Tar("tarname.tar");
  355. $tar_object->extractModify("install", "dev");
  356. // Files will be extracted there :
  357. // install/data/file.txt
  358. // install/data/log.txt
  359. // install/readme.txt
  360. How it works :
  361. Open the archive and call a more generic function that can extract
  362. only a part of the archive or all the archive.
  363. See extractList() method for more details.
  364. Method : extractInString($p_filename)
  365. Description :
  366. This method extract from the archive one file identified by $p_filename.
  367. The return value is a string with the file content, or NULL on error.
  368. Arguments :
  369. $p_filename : The path of the file to extract in a string.
  370. Return value :
  371. a string with the file content or NULL.
  372. Sample :
  373. // Imagine tarname.tar with files :
  374. // dev/data/file.txt
  375. // dev/data/log.txt
  376. // dev/readme.txt
  377. $v_archive = & new Archive_Tar('tarname.tar');
  378. $v_archive->setErrorHandling(PEAR_ERROR_PRINT);
  379. $v_string = $v_archive->extractInString('dev/readme.txt');
  380. echo $v_string;
  381. Method : listContent()
  382. Description :
  383. This method returns an array of arrays that describe each
  384. file/directory present in the archive.
  385. The array is not sorted, so it show the position of the file in the
  386. archive.
  387. The file informations are :
  388. $file[filename] : Name and path of the file/dir.
  389. $file[mode] : File permissions (result of fileperms())
  390. $file[uid] : user id
  391. $file[gid] : group id
  392. $file[size] : filesize
  393. $file[mtime] : Last modification time (result of filemtime())
  394. $file[typeflag] : "" for file, "5" for directory
  395. Arguments :
  396. Return value :
  397. An array of arrays or 0 on error.
  398. Sample :
  399. $tar_object = new Archive_Tar("tarname.tar");
  400. if (($v_list = $tar_object->listContent()) != 0)
  401. for ($i=0; $i<sizeof($v_list); $i++)
  402. {
  403. echo "Filename :'".$v_list[$i][filename]."'<br>";
  404. echo " .size :'".$v_list[$i][size]."'<br>";
  405. echo " .mtime :'".$v_list[$i][mtime]."' (".
  406. date("l dS of F Y h:i:s A", $v_list[$i][mtime]).")<br>";
  407. echo " .mode :'".$v_list[$i][mode]."'<br>";
  408. echo " .uid :'".$v_list[$i][uid]."'<br>";
  409. echo " .gid :'".$v_list[$i][gid]."'<br>";
  410. echo " .typeflag :'".$v_list[$i][typeflag]."'<br>";
  411. }
  412. How it works :
  413. Call the same function as an extract however with a flag to only go
  414. through the archive without extracting the files.
  415. Method : extractList($p_filelist, $p_path = "", $p_remove_path = "")
  416. Description :
  417. This method extract from the archive only the files indicated in the
  418. $p_filelist. These files are extracted in the current directory or
  419. in the directory indicated by the optional $p_path parameter.
  420. If indicated the $p_remove_path can be used in the same way as it is
  421. used in extractModify() method.
  422. Arguments :
  423. $p_filelist : An array of filenames and directory names, or a single
  424. string with names separated by a single blank space.
  425. $p_path : The path of the directory where the files/dir need to by
  426. extracted.
  427. $p_remove_path : Part of the memorized path that can be removed if
  428. present at the beginning of the file/dir path.
  429. Return value :
  430. true on success, false on error.
  431. Sample :
  432. // Imagine tarname.tar with files :
  433. // dev/data/file.txt
  434. // dev/data/log.txt
  435. // readme.txt
  436. $tar_object = new Archive_Tar("tarname.tar");
  437. $tar_object->extractList("dev/data/file.txt readme.txt", "install",
  438. "dev");
  439. // Files will be extracted there :
  440. // install/data/file.txt
  441. // install/readme.txt
  442. How it works :
  443. Go through the archive and extract only the files present in the
  444. list.