Skip to content

Commit

Permalink
Merge branch 'w28_MDL-33710_m24_utfzip' of git://github.com/skodak/mo…
Browse files Browse the repository at this point in the history
…odle
  • Loading branch information
Sam Hemelryk committed Jul 10, 2012
2 parents 06dd433 + 79c966c commit ee2b82d
Show file tree
Hide file tree
Showing 17 changed files with 738 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/filestorage/file_packer.php
Expand Up @@ -84,7 +84,7 @@ public abstract function extract_to_storage($archivefile, $contextid, $component
/**
* Returns array of info about all files in archive
*
* @param file_archive $archivefile
* @param string|file_archive $archivefile
* @return array of file infos
*/
public abstract function list_files($archivefile);
Expand Down
1 change: 1 addition & 0 deletions lib/filestorage/tests/fixtures/test.txt
@@ -0,0 +1 @@
test
Binary file added lib/filestorage/tests/fixtures/test_7zip_927.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_infozip_3.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_moodle.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_moodle_22.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_osx_1074.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_tc_8.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_win8_cz.zip
Binary file not shown.
Binary file added lib/filestorage/tests/fixtures/test_win8_de.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
55 changes: 55 additions & 0 deletions lib/filestorage/tests/fixtures/zip_create_test_file.php
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This debug script is used during zip support development.
*
* @package core_files
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');

$help =
"Create sample zip file for testing
Example:
\$php zip_create_test_file.php test.zip
";

if (count($_SERVER['argv']) != 2 or file_exists($_SERVER['argv'][1])) {
echo $help;
exit(0);
}

$archive = $_SERVER['argv'][1];

$packer = get_file_packer('application/zip');

$file = __DIR__.'/test.txt';
$files = array(
'test.test' => $file,
'testíček.txt' => $file,
'Prüfung.txt' => $file,
'测试.txt' => $file,
'試験.txt' => $file,
'Žluťoučký/Koníček.txt' => $file,
);

$packer->archive_to_pathname($files, $archive);
302 changes: 302 additions & 0 deletions lib/filestorage/tests/fixtures/zip_info.php
@@ -0,0 +1,302 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This debug script is used during zip support development ONLY.
*
* @package core_files
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');

if (count($_SERVER['argv']) != 2 or !file_exists($_SERVER['argv'][1])) {
cli_error("This script expects zip file name as the only parameter");
}

$archive = $_SERVER['argv'][1];

// Note: the ZIP structure is described at http://www.pkware.com/documents/casestudies/APPNOTE.TXT
if (!$filesize = filesize($archive) or !$fp = fopen($archive, 'rb+')) {
cli_error("Can not open file file $archive");
}

fseek($fp, 0);
$info = unpack('Vsig', fread($fp, 4));
if ($info['sig'] !== 0x04034b50) {
fclose($fp);
cli_error("This is not a zip file: $archive");
}

// Find end of central directory record.
fseek($fp, $filesize - 22);
$info = unpack('Vsig', fread($fp, 4));
if ($info['sig'] === 0x06054b50) {
// There is no comment.
fseek($fp, $filesize - 22);
$data = fread($fp, 22);
} else {
// There is some comment with 0xFF max size - that is 65557.
fseek($fp, $filesize - 65557);
$data = fread($fp, 65557);
}

$pos = strpos($data, pack('V', 0x06054b50));
if ($pos === false) {
// Borked ZIP structure!
fclose($fp);
cli_error("Can not find end of central directory in $archive");
}
$centralend = unpack('Vsig/vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_length', substr($data, $pos, 22));

if ($centralend['disk'] !== 0 or $centralend['disk_start'] !== 0) {
cli_error("Multi-disk archives are not supported: $archive");
}

if ($centralend['offset'] === 0xFFFFFFFF) {
cli_error("ZIP64 archives are not supported: $archive");
}

if ($centralend['comment_length']) {
$centralend['comment'] = substr($data, 22, $centralend['comment_length']);
} else {
$centralend['comment'] = '';
}

fseek($fp, $centralend['offset']);
$data = fread($fp, $centralend['size']);
$pos = 0;
$files = array();
for($i=0; $i<$centralend['entries']; $i++) {
$file = unpack('Vsig/vversion/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length/vcomment_length/vdisk/vattr/Vattrext/Vlocal_offset', substr($data, $pos, 46));
$file['error'] = null;
$file['central_offset'] = $centralend['offset'] + $pos;
$pos = $pos + 46;
if ($file['sig'] !== 0x02014b50) {
$files[] = array('error'=>'Invalid central file signature');
continue;
}
$file['name'] = substr($data, $pos, $file['name_length']);
$pos = $pos + $file['name_length'];
$file['extra'] = array();
if ($file['extra_length']) {
$extradata = substr($data, $pos, $file['extra_length']);
while (strlen($extradata) > 4) {
$extra = unpack('vid/vsize', substr($extradata, 0, 4));
$extra['data'] = substr($extradata, 4, $extra['size']);
$extradata = substr($extradata, 4+$extra['size']);
$file['extra'][] = $extra;
}
$pos = $pos + $file['extra_length'];
}
if ($file['comment_length']) {
$file['comment'] = substr($data, $pos, $file['comment_length']);
$pos = $pos + $file['comment_length'];
} else {
$file['comment'] = '';
}

// Read local file header.
fseek($fp, $file['local_offset']);
$localfile = unpack('Vsig/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length', fread($fp, 30));
if ($localfile['sig'] !== 0x04034b50) {
// Borked file!
$file['error'] = 'Invalid local file signature';
$files[] = $file;
continue;
}
if ($localfile['name_length']) {
$localfile['name'] = fread($fp, $localfile['name_length']);
} else {
$localfile['name'] = '';
}
$localfile['extra'] = array();
if ($localfile['extra_length']) {
$extradata = fread($fp, $localfile['extra_length']);
while (strlen($extradata) > 4) {
$extra = unpack('vid/vsize', substr($extradata, 0, 4));
$extra['data'] = substr($extradata, 4, $extra['size']);
$extradata = substr($extradata, 4+$extra['size']);
$localfile['extra'][] = $extra;
}
}

$file['local'] = $localfile;
$files[] = $file;
}

echo "Archive: $archive\n";
echo "Number of files: {$centralend['entries']}\n";
echo "Archive comment: \"{$centralend['comment']}\" ({$centralend['comment_length']} bytes)\n";
foreach ($files as $i=>$file) {
echo "======== File ".($i+1)." ==============================================\n";
if (!empty($file['error'])) {
echo " ERROR: {$file['error']}\n";
}
echo " Name: ".zip_print_name($file['name'])."\n";
if ($file['comment'] !== '') {
echo " Comment: \"{$file['comment']}\" ({$file['comment_length']} bytes)\n";
}
echo " Version: 0x".str_pad(dechex($file['version']), 4, '0', STR_PAD_LEFT)."\n";
echo " Required: 0x".str_pad(dechex($file['version_req']), 4, '0', STR_PAD_LEFT)."\n";
echo " Method: ".zip_print_method($file['method'])."\n";
echo " General: ".zip_print_general($file['general'])."\n";
echo " Modified: ".userdate(zip_dos2unixtime($file['modified']))."\n";
echo " Size: ".zip_print_sizes($file['size'], $file['size_compressed'])."\n";
echo " CRC-32: {$file['crc']}\n";
if ($file['extra']) {
foreach($file['extra'] as $j=>$extra) {
echo " Extra ".($j+1).": ".zip_print_extra($extra)."\n";
}
}
if (!empty($file['local']['error'])) {
echo " Local ERROR: {$file['local']['error']}\n";
}
$localfile = $file['local'];
if ($localfile['name'] !== $file['name']) {
echo " Local name: ".zip_print_name($localfile['name'])."\n";
}
if ($localfile['version_req'] !== $file['version_req']) {
echo " Local required: 0x".str_pad(dechex($localfile['version_req']), 4, '0', STR_PAD_LEFT)."\n";
}
if ($localfile['method'] !== $file['method']) {
echo " Local method: ".zip_print_method($localfile['method'])."\n";
}
if ($localfile['general'] !== $file['general']) {
echo " Local general: ".zip_print_general($localfile['general'])."\n";
}
if ($localfile['modified'] !== $file['modified']) {
echo " Local modified: ".userdate(zip_dos2unixtime($localfile['modified']))."\n";
}
if ($localfile['size'] !== $file['size']) {
echo " Local size: ".zip_print_sizes($localfile['size'], $localfile['size_compressed'])."\n";
}
if ($localfile['crc'] !== $file['crc']) {
echo " Local CRC-32: {$localfile['crc']}\n";
}
if ($localfile['extra']) {
foreach($localfile['extra'] as $j=>$extra) {
echo " Local extra ".($j+1).": ".zip_print_extra($extra)."\n";
}
}
}

fclose($fp);
exit(0);

// === Some useful functions ======================================

function zip_print_name($name) {
$size = strlen($name);
$crc = crc32($name);
return "\"$name\" ($size bytes) - CRC $crc";
}

function zip_print_method($method) {
$desc = '';
switch($method) {
case 0: $desc = 'Stored'; break;
case 1: $desc = 'Shrunk'; break;
case 2: $desc = 'Reduced factor 1'; break;
case 3: $desc = 'Reduced factor 2'; break;
case 4: $desc = 'Reduced factor 3'; break;
case 5: $desc = 'Reduced factor 4'; break;
case 6: $desc = 'Imploded'; break;
case 8: $desc = 'Deflated'; break;
case 9: $desc = 'Deflate64'; break;
case 10: $desc = 'old IBM TERSE'; break;
case 12: $desc = 'BZIP2'; break;
case 14: $desc = 'LZMA'; break;
case 18: $desc = 'IBM TERSE'; break;
case 19: $desc = 'IBM LZ77'; break;
case 97: $desc = 'WavPack'; break;
case 98: $desc = 'PPMd v1'; break;
}
if ($desc) {
$desc = " ($desc)";
}
return "0x".str_pad(dechex($method), 4, '0', STR_PAD_LEFT).$desc;
}

function zip_print_general($general) {
$desc = array();
if ($general & pow(2, 0)) {
$desc[] = 'Encrypted';
}
if ($general & pow(2, 11)) {
$desc[] = 'Unicode name';
}
if ($desc) {
$desc = " (".implode(', ', $desc).")";
} else {
$desc = '';
}
return str_pad(decbin($general), 16, '0', STR_PAD_LEFT).$desc;
}

/**
* Convert MS date+time format to unix timestamp:
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms724274(v=vs.85).aspx
*
* Copied from: http://plugins.svn.wordpress.org/wp2epub/trunk/zipcreate/functions.lib.php
* author: redmonkey
* license: GPL
*/
function zip_dos2unixtime($dostime) {
$sec = 2 * ($dostime & 0x1f);
$min = ($dostime >> 5) & 0x3f;
$hrs = ($dostime >> 11) & 0x1f;
$day = ($dostime >> 16) & 0x1f;
$mon = ($dostime >> 21) & 0x0f;
$year = (($dostime >> 25) & 0x7f) + 1980;

return mktime($hrs, $min, $sec, $mon, $day, $year);
}

function zip_print_sizes($size, $compressed) {
return "$size ==> $compressed bytes";
}

function zip_print_extra($extra) {
$desc = '';
$info = "- ".bin2hex($extra['data'])." ({$extra['size']} bytes)";
switch($extra['id']) {
case 0x0009: $desc = 'OS/2'; break;
case 0x000a: $desc = 'NTFS'; break;
case 0x000d: $desc = 'UNIX'; break;
case 0x5455: $desc = 'Extended timestamp'; break;
case 0x5855: $desc = 'Infor-ZIP (original)'; break;
case 0x7075:
$desc = 'Info-ZIP Unicode path';
$data = unpack('cversion/Vcrc', substr($extra['data'], 0, 5));
$name = substr($extra['data'], 5);
$size = strlen($name);
if ($data['version'] === 1) {
$info = "- \"$name\" ($size bytes) - CRC {$data['crc']}";
}
break;
case 0x7865: $desc = 'Info-ZIP UNIX (new)'; break;
case 0x7875: $desc = 'Info-ZIP UNIX (3rd generation)'; break;
}
if ($desc) {
$desc = " ($desc)";
}
return "0x".str_pad(dechex($extra['id']), 4, '0', STR_PAD_LEFT)."$desc $info";
}

0 comments on commit ee2b82d

Please sign in to comment.