public function test_blockdata() { $tar = new Tar(); $tar->setCompression(0); $tar->create(); $tar->addData('block.txt', str_pad('', 512, 'x')); $file = $tar->getArchive(); $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks }
/** * @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); }