/** * Extract an archive file to a directory. * * @param string $archivename The name of the archive file * @param string $extractdir Directory to unpack into * * @return boolean True for success * * @since 11.1 * @throws InvalidArgumentException */ public static function extract($archivename, $extractdir) { $untar = false; $result = false; $ext = File::getExt(strtolower($archivename)); // Check if a tar is embedded...gzip/bzip2 can just be plain files! if (File::getExt(File::stripExt(strtolower($archivename))) == 'tar') { $untar = true; } switch ($ext) { case 'zip': $adapter = self::getAdapter('zip'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tar': $adapter = self::getAdapter('tar'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tgz': // This format is a tarball gzip'd $untar = true; case 'gz': case 'gzip': // This may just be an individual file (e.g. sql script) $adapter = self::getAdapter('gzip'); if ($adapter) { $config = Factory::getConfig(); $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip'); $gzresult = $adapter->extract($archivename, $tmpfname); if ($gzresult instanceof Exception) { @unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter = self::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = Path::clean($extractdir); Folder::create($path); $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(strtolower($archivename))), null, 1); } @unlink($tmpfname); } break; case 'tbz2': // This format is a tarball bzip2'd $untar = true; case 'bz2': case 'bzip2': // This may just be an individual file (e.g. sql script) $adapter = self::getAdapter('bzip2'); if ($adapter) { $config = Factory::getConfig(); $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2'); $bzresult = $adapter->extract($archivename, $tmpfname); if ($bzresult instanceof Exception) { @unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter = self::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = Path::clean($extractdir); Folder::create($path); $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(strtolower($archivename))), null, 1); } @unlink($tmpfname); } break; default: throw new InvalidArgumentException('Unknown Archive Type'); } if (!$result || $result instanceof Exception) { return false; } return true; }
/** * Extract an archive file to a directory. * * @param string $archivename The name of the archive file * @param string $extractdir Directory to unpack into * * @return boolean True for success * * @since 1.0 * @throws \InvalidArgumentException */ public function extract($archivename, $extractdir) { $ext = pathinfo($archivename, PATHINFO_EXTENSION); $path = pathinfo($archivename, PATHINFO_DIRNAME); $filename = pathinfo($archivename, PATHINFO_FILENAME); switch ($ext) { case 'zip': $result = $this->getAdapter('zip')->extract($archivename, $extractdir); break; case 'tar': $result = $this->getAdapter('tar')->extract($archivename, $extractdir); break; case 'tgz': case 'gz': case 'gzip': // This may just be an individual file (e.g. sql script) $tmpfname = $this->options['tmp_path'] . '/' . uniqid('gzip'); $gzresult = $this->getAdapter('gzip')->extract($archivename, $tmpfname); if ($gzresult instanceof \Exception) { @unlink($tmpfname); return false; } if ($ext === 'tgz' || stripos($filename, '.tar') !== false) { $result = $this->getAdapter('tar')->extract($tmpfname, $extractdir); } else { Folder::create($path); $result = File::copy($tmpfname, $extractdir, null, 0); } @unlink($tmpfname); break; case 'tbz2': case 'bz2': case 'bzip2': // This may just be an individual file (e.g. sql script) $tmpfname = $this->options['tmp_path'] . '/' . uniqid('bzip2'); $bzresult = $this->getAdapter('bzip2')->extract($archivename, $tmpfname); if ($bzresult instanceof \Exception) { @unlink($tmpfname); return false; } if ($ext === 'tbz2' || stripos($filename, '.tar') !== false) { $result = $this->getAdapter('tar')->extract($tmpfname, $extractdir); } else { Folder::create($path); $result = File::copy($tmpfname, $extractdir, null, 0); } @unlink($tmpfname); break; default: throw new \InvalidArgumentException(sprintf('Unknown archive type: %s', $ext)); } if (!$result || $result instanceof \Exception) { return false; } return true; }
/** * Test makeCopy method for an exception * * @return void * * @covers Joomla\Filesystem\File::copy * @expectedException \UnexpectedValueException */ public function testCopyException() { $name = 'tempFile'; $path = __DIR__; $copiedFileName = 'copiedTempFile'; File::copy($path . '/' . $name . 'foobar', $path . '/' . $copiedFileName); }
/** * Test makeCopy method * * @return void * * @covers Joomla\Filesystem\File::copy * @since 1.0 */ public function testCopy() { $name = 'tempFile'; $path = __DIR__; $copiedFileName = 'copiedTempFile'; $data = 'Lorem ipsum dolor sit amet'; // Create a temp file to test copy operation $this->object->write($path . '/' . $name, $data); // Trying to read non-existing file. $this->assertThat(File::copy($path . '/' . $name . 'foobar', $path . '/' . $copiedFileName), $this->isFalse(), 'Line:' . __LINE__ . ' File should not copy successfully.'); File::delete($path . '/' . $copiedFileName); $this->assertThat(File::copy($path . '/' . $name, $path . '/' . $copiedFileName), $this->isTrue(), 'Line:' . __LINE__ . ' File should copy successfully.'); File::delete($path . '/' . $copiedFileName); $this->assertThat(File::copy($name, $copiedFileName, $path), $this->isTrue(), 'Line:' . __LINE__ . ' File should copy successfully.'); File::delete($path . '/' . $copiedFileName); // Copy using streams. $this->assertThat(File::copy($name, $copiedFileName, $path, true), $this->isTrue(), 'Line:' . __LINE__ . ' File should copy successfully.'); File::delete($path . '/' . $copiedFileName); File::delete($path . '/' . $name); }