abstract class Archive {
/**
- * @param $source
+ * @param string $source
*/
abstract public function __construct($source);
/**
/**
* get the content of a file
* @param string $path
- * @return string
+ * @return string|false
*/
abstract public function getFile($path);
/**
* extract a single file from the archive
* @param string $path
* @param string $dest
- * @return bool
+ * @return bool success
*/
abstract public function extractFile($path, $dest);
/**
* add a folder and all its content
* @param string $path
* @param string $source
+ * @return void
*/
public function addRecursive($path, $source) {
$dh = opendir($source);
public const GZIP = 1;
public const BZIP = 2;
- private $fileList;
- private $cachedHeaders;
+ /**
+ * @var string[]|false
+ */
+ private $fileList = false;
+ /**
+ * @var array|false
+ */
+ private $cachedHeaders = false;
/**
- * @var \Archive_Tar tar
+ * @var \Archive_Tar
*/
private $tar = null;
+
+ /**
+ * @var string
+ */
private $path;
/**
/**
* @param string $file
+ * @return array|null
*/
private function getHeader($file) {
if (!$this->cachedHeaders) {
* get the content of a file
*
* @param string $path
- * @return string
+ * @return string|false
*/
public function getFile($path) {
- return $this->tar->extractInString($path);
+ $string = $this->tar->extractInString($path);
+ if (is_string($string)) {
+ return $string;
+ } else {
+ return false;
+ }
}
/**
/**
* write back temporary files
+ * @param string $tmpFile
+ * @param string $path
+ * @return void
*/
public function writeBack($tmpFile, $path) {
$this->addFile($path, $tmpFile);
/**
* reopen the archive to ensure everything is written
*/
- private function reopen() {
+ private function reopen(): void {
if ($this->tar) {
$this->tar->_close();
$this->tar = null;
use Icewind\Streams\CallbackWrapper;
use OCP\ILogger;
+use Psr\Log\LoggerInterface;
class ZIP extends Archive {
/**
* @var \ZipArchive zip
*/
- private $zip = null;
+ private $zip;
+
+ /**
+ * @var string
+ */
private $path;
/**
$this->zip = new \ZipArchive();
if ($this->zip->open($source, \ZipArchive::CREATE)) {
} else {
- \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
+ \OC::$server->get(LoggerInterface::class)->warning('Error while opening archive '.$source, ['app' => 'files_archive']);
}
}
/**
* rename a file or folder in the archive
* @param string $source
* @param string $dest
- * @return boolean|null
+ * @return bool
*/
public function rename($source, $dest) {
$source = $this->stripPath($source);
$dest = $this->stripPath($dest);
- $this->zip->renameName($source, $dest);
+ return $this->zip->renameName($source, $dest);
}
/**
* get the uncompressed size of a file in the archive
/**
* get the content of a file
* @param string $path
- * @return string
+ * @return string|false
*/
public function getFile($path) {
return $this->zip->getFromName($path);
* extract a single file from the archive
* @param string $path
* @param string $dest
- * @return boolean|null
+ * @return bool
*/
public function extractFile($path, $dest) {
$fp = $this->zip->getStream($path);
- file_put_contents($dest, $fp);
+ if ($fp === false) {
+ return false;
+ }
+ return file_put_contents($dest, $fp) !== false;
}
/**
* extract the archive
//since we can't directly get a writable stream,
//make a temp copy of the file and put it back
//in the archive when the stream is closed
- if (strrpos($path, '.') !== false) {
- $ext = substr($path, strrpos($path, '.'));
+ $lastPoint = strrpos($path, '.');
+ if ($lastPoint !== false) {
+ $ext = substr($path, $lastPoint);
} else {
$ext = '';
}
/**
* write back temporary files
+ * @param string $tmpFile
+ * @param string $path
+ * @return void
*/
public function writeBack($tmpFile, $path) {
$this->addFile($path, $tmpFile);