Example #1
0
<?php

$z = new ZipArchive();
$z->open('test_with_comment.zip');
// Add "Foo Comment" as comment for the foo entry
$z->setCommentName('foo', 'Too Comment ' . time());
$z->close();
Example #2
0
 function run($args, $options)
 {
     Bootstrap::connect();
     osTicket::start();
     switch ($args['action']) {
         case 'backends':
             // List configured backends
             foreach (FileStorageBackend::allRegistered() as $char => $bk) {
                 print "{$char} -- {$bk::$desc} ({$bk})\n";
             }
             break;
         case 'list':
             // List files matching criteria
             // ORM would be nice!
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             foreach ($files as $f) {
                 printf("% 5d %s % 8d %s % 16s %s\n", $f->id, $f->bk, $f->size, $f->created, $f->type, $f->name);
                 if ($f->attrs) {
                     printf("        %s\n", $f->attrs);
                 }
             }
             break;
         case 'dump':
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if ($files->count() != 1) {
                 $this->fail('Criteria must select exactly 1 file');
             }
             if (($f = AttachmentFile::lookup($files[0]->id)) && ($bk = $f->open())) {
                 $bk->passthru();
             }
             break;
         case 'load':
             // Load file content from STDIN
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if ($files->count() != 1) {
                 $this->fail('Criteria must select exactly 1 file');
             }
             $f = AttachmentFile::lookup($files[0]->id);
             try {
                 if ($bk = $f->open()) {
                     $bk->unlink();
                 }
             } catch (Exception $e) {
             }
             if ($options['to']) {
                 $bk = FileStorageBackend::lookup($options['to'], $f);
             } else {
                 // Use the system default
                 $bk = AttachmentFile::getBackendForFile($f);
             }
             $type = false;
             $signature = '';
             $finfo = new finfo(FILEINFO_MIME_TYPE);
             if ($options['file'] && $options['file'] != '-') {
                 if (!file_exists($options['file'])) {
                     $this->fail($options['file'] . ': Cannot open file');
                 }
                 if (!$bk->upload($options['file'])) {
                     $this->fail('Unable to upload file contents to backend');
                 }
                 $type = $finfo->file($options['file']);
                 list(, $signature) = AttachmentFile::_getKeyAndHash($options['file'], true);
             } else {
                 $stream = fopen('php://stdin', 'rb');
                 while ($block = fread($stream, $bk->getBlockSize())) {
                     if (!$bk->write($block)) {
                         $this->fail('Unable to send file contents to backend');
                     }
                     if (!$type) {
                         $type = $finfo->buffer($block);
                     }
                 }
                 if (!$bk->flush()) {
                     $this->fail('Unable to commit file contents to backend');
                 }
             }
             // TODO: Update file metadata
             $sql = 'UPDATE ' . FILE_TABLE . ' SET bk=' . db_input($bk->getBkChar()) . ', created=CURRENT_TIMESTAMP' . ', type=' . db_input($type) . ', signature=' . db_input($signature) . ' WHERE id=' . db_input($f->getId());
             if (!db_query($sql) || db_affected_rows() != 1) {
                 $this->fail('Unable to update file metadata');
             }
             $this->stdout->write("Successfully saved contents\n");
             break;
         case 'migrate':
             if (!$options['to']) {
                 $this->fail('Please specify a target backend for migration');
             }
             if (!FileStorageBackend::isRegistered($options['to'])) {
                 $this->fail('Target backend is not installed. See `backends` action');
             }
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             $count = 0;
             foreach ($files as $m) {
                 $f = AttachmentFile::lookup($m->id);
                 if ($f->getBackend() == $options['to']) {
                     continue;
                 }
                 if ($options['verbose']) {
                     $this->stdout->write('Migrating ' . $m->name . "\n");
                 }
                 try {
                     if (!$f->migrate($options['to'])) {
                         $this->stderr->write('Unable to migrate ' . $m->name . "\n");
                     } else {
                         $count++;
                     }
                 } catch (IOException $e) {
                     $this->stderr->write('IOError: ' . $e->getMessage());
                 }
             }
             $this->stdout->write("Migrated {$count} files\n");
             break;
         case 'export':
             // Create a temporary ZIP file
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if (!$options['file']) {
                 $this->fail('Please specify zip file with `-f`');
             }
             $zip = new ZipArchive();
             if (true !== ($reason = $zip->open($options['file'], ZipArchive::CREATE))) {
                 $this->fail($reason . ': Unable to create zip file');
             }
             $manifest = array();
             foreach ($files as $m) {
                 $f = AttachmentFile::lookup($m->id);
                 $zip->addFromString($f->getId(), $f->getData());
                 $zip->setCommentName($f->getId(), $f->getName());
                 // TODO: Log %attachment and %ticket_attachment entries
                 $info = array('file' => $f->getInfo());
                 foreach ($m->tickets as $t) {
                     $info['tickets'][] = $t->ht;
                 }
                 $manifest[$f->getId()] = $info;
             }
             $zip->addFromString('MANIFEST', serialize($manifest));
             $zip->close();
             break;
         case 'expunge':
             // Create a temporary ZIP file
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             foreach ($files as $f) {
                 $f->tickets->expunge();
                 $f->unlink() && $f->delete();
             }
     }
 }
Example #3
0
   $zip = new ZipArchive();
   $zipfilename = $safe_name . ".cbz";
   if ($zip->open($zipfilename, ZIPARCHIVE::CREATE) !== TRUE) {
       exit("cannot open <{$zipfilename}>\n");
   }
   $manifest = '';
   foreach ($contents as $filepath => $file) {
       // Traditional CBZ have all files in the root,
       // but that explodes if unpacked. Retain the folder inside the package.
       $innerfilename = $file['filename'];
       if ($use_subfolder) {
           $innerfilename = $safe_name . '/' . $file['filename'];
       }
       $zip->addFile($filepath, $innerfilename);
       $manifest .= "{$innerfilename}, '{$file['url']}'\n";
       $zip->setCommentName($innerfilename, $file['url']);
   }
   $zipinfo = '{
 "appID":"cbzget/001",
 "lastModified":"' . date("Y-m-d h:i:s O") . '",
 "ComicBookInfo/1.0":{
   "title":"' . $page_title . '",
   "series":"' . $original_url->host . '",
   "publisher":"' . $original_url . '",
   "url":"' . $original_url . '",
   "publicationYear":' . date('Y') . ',
   "publicationMonth":' . date('n') . ',
   "tags":["Downloads"]
 }}';
   $zip->addFromString("ZipInfo.txt", $zipinfo);
   $zip->addFromString("manifest.txt", $manifest);
Example #4
0
<?php

$dirname = dirname(__FILE__) . '/';
include $dirname . 'utils.inc';
$file = $dirname . '__tmp_oo_set_comment.zip';
@unlink($file);
$zip = new ZipArchive();
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
    exit('failed');
}
$zip->addFromString('entry1.txt', 'entry #1');
$zip->addFromString('entry2.txt', 'entry #2');
$zip->addFromString('dir/entry2d.txt', 'entry #2');
$zip->addFromString('entry4.txt', 'entry #1');
$zip->addFromString('entry5.txt', 'entry #2');
var_dump($zip->setCommentName('entry1.txt', 'entry1.txt'));
var_dump($zip->setCommentName('entry2.txt', 'entry2.txt'));
var_dump($zip->setCommentName('dir/entry2d.txt', 'dir/entry2d.txt'));
var_dump($zip->setArchiveComment('archive'));
var_dump($zip->setCommentIndex(3, 'entry4.txt'));
var_dump($zip->setCommentIndex(4, 'entry5.txt'));
var_dump($zip->setArchiveComment('archive'));
if (!$zip->status == ZIPARCHIVE::ER_OK) {
    echo "failed to write zip\n";
}
$zip->close();
if (!$zip->open($file)) {
    @unlink($file);
    exit('failed');
}
var_dump($zip->getCommentIndex(0));