/** * simple test that checks that the given filenames and contents can be grepped from the * uncompressed zip file * * No check for format correctness */ public function test_createfile() { $zip = new Zip(); $dir = dirname(__FILE__) . '/zip'; $tdir = ltrim($dir, '/'); $tmp = tempnam(sys_get_temp_dir(), 'dwziptest'); $zip->create($tmp); $zip->setCompression(0); $zip->AddFile("{$dir}/testdata1.txt", "{$dir}/testdata1.txt", 0); $zip->AddFile("{$dir}/foobar/testdata2.txt", 'noway/testdata2.txt', 0); $zip->addData('another/testdata3.txt', 'testcontent3', 0, 0); $zip->close(); $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number $data = file_get_contents($tmp); $this->assertTrue(strpos($data, 'testcontent1') !== false, 'Content in ZIP'); $this->assertTrue(strpos($data, 'testcontent2') !== false, 'Content in ZIP'); $this->assertTrue(strpos($data, 'testcontent3') !== false, 'Content in ZIP'); // fullpath might be too long to be stored as full path FS#2802 $this->assertTrue(strpos($data, "{$tdir}") !== false, "Path in ZIP '{$tdir}'"); $this->assertTrue(strpos($data, "testdata1.txt") !== false, 'File in ZIP'); $this->assertTrue(strpos($data, 'noway/testdata2.txt') !== false, 'Path in ZIP'); $this->assertTrue(strpos($data, 'another/testdata3.txt') !== false, 'Path in ZIP'); // fullpath might be too long to be stored as full path FS#2802 $this->assertTrue(strpos($data, "{$tdir}/foobar") === false, 'Path not in ZIP'); $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in ZIP'); $this->assertTrue(strpos($data, "foobar") === false, 'Path not in ZIP'); @unlink($tmp); }
/** * Create an archive and unpack it again */ public function test_dogfood() { $input = glob(dirname(__FILE__) . '/../src/*'); $archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip'; $extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1); $zip = new Zip(); $zip->create($archive); foreach ($input as $path) { $file = basename($path); $zip->addFile($path, $file); } $zip->close(); $this->assertFileExists($archive); $zip = new Zip(); $zip->open($archive); $zip->extract($extract, '', '/FileInfo\\.php/', '/.*\\.php/'); $this->assertFileExists("{$extract}/Tar.php"); $this->assertFileExists("{$extract}/Zip.php"); $this->assertFileNotExists("{$extract}/FileInfo.php"); self::rdelete($extract); unlink($archive); }
/** * @throws \Exception * @throws \splitbrain\PHPArchive\ArchiveIOException */ public function create() { $this->job->log(__('Creating archive', 'my-wp-backup')); $archive_name = $this->job->get_filename() . '.' . $this->extension; if ('zip' === $this->format) { $archive = new Zip(); $archive->create($archive_name); } else { $archive = new Tar(); if ($this->is_compressed()) { $this->job->log(sprintf(__('Compressing archive with %s', 'my-wp-backup'), Admin\Job::$compression_methods[$this->job['compression']])); } $archive->setCompression(9, $this->compression); $archive->create($archive_name); } $this->archives = array($archive_name); if ('1' === $this->job['backup_files']) { $files = $this->job->get_files(); /** * @var string $name filename * @var \DirectoryIterator $file */ foreach ($files['iterator'] as $name => $file) { // Skip directories (they would be added automatically) if ($file->isDir()) { continue; } // Get real and relative path for current file $filePath = $file->getRealPath(); $relativePath = substr($filePath, strlen(MyWPBackup::$info['root_dir'])); if (false === $relativePath) { $this->job->log(sprintf(__('Skipping "%s", not in root dir', 'my-wp-backup'), $filePath), 'error'); continue; } $this->job->log(sprintf(__('Adding file: %s....', 'my-wp-backup'), $relativePath), 'debug'); $archive->addFile($filePath, $relativePath); $this->job->log(__('Ok.', 'my-wp-backup'), 'debug'); $this->job->get_hashfile()->fwrite("ok {$relativePath}\n"); } } else { $this->job->log(__('Only backing up database file.', 'my-wp-backup')); } $archive->addFile($this->job->get_hashfile()->getPathname(), '.my-wp-backup'); if ('1' === $this->job['export_db']) { $this->job->log(__('Adding database export file...', 'my-wp-backup'), 'debug'); $archive->addFile($this->job->get_dbpath(), ExportFile::FILENAME); $this->job->log(__('Ok.', 'my-wp-backup'), 'debug'); } $this->job->log(__('Closing archive...', 'my-wp-backup'), 'debug'); $archive->close(); $this->job->log(__('Ok.', 'my-wp-backup'), 'debug'); $this->calc_size(); if ($this->split_size > 0 && $this->size > $this->split_size) { $this->job->log(__('Splitting archive', 'my-wp-backup')); $root_archive = fopen($this->archives[0], 'rb'); $this->archives = array(); $part = 0; if (!$root_archive) { throw new \Exception(__('Unable to split archive.', 'my-wp-backup')); } while (!feof($root_archive)) { $buffer = fread($root_archive, $this->split_size); $filename = sprintf('%s.vol%s.%s', $this->job->get_filename(), ++$part, $this->extension); $handle = fopen($filename, 'wb'); $this->job->log(sprintf(__('Creating volume %s...', 'my-wp-backup'), $part), 'debug'); if (!$handle) { throw new \Exception(sprintf(__('Unable to create volume %s.', 'my-wp-backup'), $part)); } fwrite($handle, $buffer); fclose($handle); array_push($this->archives, $filename); $this->job->log(__('Ok.', 'my-wp-backup'), 'debug'); $this->job->log(sprintf(__('Created volume %s', 'my-wp-backup'), $part)); } fclose($root_archive); unlink($archive_name); } $this->job->log(sprintf(__('Archive location: %s', 'my-wp-backup'), implode(',', $this->archives)), 'debug'); $this->job->log(__('Done create archive', 'my-wp-backup')); $this->job->set_archive($this); }