/wp-content/plugins/anthologize/vendor/pear/file_archive/File/Archive/Writer/Bzip2.php

https://github.com/livinglab/openlab · PHP · 147 lines · 60 code · 17 blank · 70 comment · 5 complexity · 320d4f2c90c09da45f6290c14cc22f2f MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Compress a single file to Bzip2 format
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
  21. *
  22. * @category File Formats
  23. * @package File_Archive
  24. * @author Vincent Lascaux <vincentlascaux@php.net>
  25. * @copyright 1997-2005 The PHP Group
  26. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  27. * @version CVS: $Id$
  28. * @link http://pear.php.net/package/File_Archive
  29. */
  30. require_once "File/Archive/Writer.php";
  31. /**
  32. * Compress a single file to Bzip2 format
  33. */
  34. class File_Archive_Writer_Bzip2 extends File_Archive_Writer
  35. {
  36. /**
  37. * compressionLevel
  38. *
  39. * @var integer
  40. * @access public
  41. * @deprecated
  42. */
  43. var $compressionLevel=9;
  44. var $bzfile;
  45. var $tmpName;
  46. var $nbFiles = 0;
  47. var $innerWriter;
  48. var $autoClose;
  49. var $filename;
  50. var $stat;
  51. /**
  52. * @param string $filename Name to give to the archive
  53. * @param File_Archive_Writer $innerWriter The inner writer to which the
  54. * compressed data will be written
  55. * @param array $stat The stat of the archive (see the PHP stat() function).
  56. * No element are required in this array
  57. * @param bool $autoClose Indicate if the inner writer must be closed when
  58. * closing this
  59. */
  60. function File_Archive_Writer_Bzip2($filename, &$innerWriter,
  61. $stat = array(), $autoClose = true)
  62. {
  63. $this->innerWriter =& $innerWriter;
  64. $this->autoClose = $autoClose;
  65. $this->filename = $filename;
  66. $this->stat = $stat;
  67. if ($this->filename === null) {
  68. $this->newFile(null);
  69. }
  70. }
  71. /**
  72. * Set the compression level. Do nothing because PHP bz2 ext doesn't
  73. * support this.
  74. *
  75. * @param int $compressionLevel From 0 (no compression) to 9 (best
  76. * compression)
  77. * @deprecated
  78. */
  79. function setCompressionLevel($compressionLevel)
  80. {
  81. $this->compressionLevel = $compressionLevel;
  82. }
  83. /**
  84. * @see File_Archive_Writer::newFile()
  85. *
  86. * Check that one single file is written in the BZip2 archive
  87. */
  88. function newFile($filename, $stat = array(),
  89. $mime = "application/octet-stream")
  90. {
  91. if ($this->nbFiles > 1) {
  92. return PEAR::raiseError("A Bzip2 archive can only contain one single file.".
  93. "Use Tbz archive to be able to write several files");
  94. }
  95. $this->nbFiles++;
  96. $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far');
  97. $this->bzfile = bzopen($this->tmpName, 'w');
  98. return true;
  99. }
  100. /**
  101. * Actually write the tmp file to the inner writer
  102. * Close and delete temporary file
  103. *
  104. * @see File_Archive_Writer::close()
  105. */
  106. function close()
  107. {
  108. bzclose($this->bzfile);
  109. if ($this->filename === null) {
  110. //Assume innerWriter is already opened on a file...
  111. $this->innerWriter->writeFile($this->tmpName);
  112. unlink($this->tmpName);
  113. } else {
  114. $this->innerWriter->newFromTempFile(
  115. $this->tmpName, $this->filename, $this->stat, 'application/x-compressed'
  116. );
  117. }
  118. if ($this->autoClose) {
  119. return $this->innerWriter->close();
  120. }
  121. }
  122. /**
  123. * @see File_Archive_Writer::writeData()
  124. */
  125. function writeData($data)
  126. {
  127. bzwrite($this->bzfile, $data);
  128. }
  129. }
  130. ?>