php-zip
php-zip copied to clipboard
Get information about a file by a numeric key
Whether it is possible at a method call GetAlInfo to receive data where key instead of a file name there will be a number
sometimes the file name can be very long or, for example, in another language, for example Arabic, or the file name will be unreadable, then the contents of the file can not be seen
array (size=2680)
0 =>
object(PhpZip\Model\ZipInfo)[8074]
private 'key' => string '0' (length=1)
private 'path' => string '.env' (length=4)
private 'folder' => boolean false
private 'size' => string '388' (length=3)
private 'compressedSize' => string '248' (length=3)
1 => .......
and method getKey()
Hi.
Use the php function array_values()
.
$zipAllInfo = $zipFile->getAllInfo();
$zipAllInfo = array_values($zipAllInfo);
var_dump($zipAllInfo);
well, now I have for example the file public / index.php has key 155
how can I get it now?
$archive = new \PhpZip\ZipFile();
$archive->openFile('archive.zip');
var_dump($archive[155]); // will not work
you can do it like this, but it's not very nice
var_dump($archive[$archive->getListFiles()[$id]]);
Describe the task that you want to solve, and for which you need numeric indexes.
for example, I list all the files in the archive and I make links to the file view, for this, I need to transfer the file name in the path, and if the file name is in Arabic, in another encoding or some unreadable characters of course the link will be non-working
I did as you advised, but it's not very convenient https://github.com/visavi/rotor/blob/master/app/Controllers/Load/DownController.php#L323 https://github.com/visavi/rotor/blob/master/resources/views/load/zip.blade.php#L24
Раз комментарии в ссылках на русском, напишу на нём. Это будет добавлено в один из следующих релизов. На готове релиз 3.1.0, в нём скорее всего этого не будет, а будет отложено до релиза 3.2.0.
Посмотрите следующий пример:
<?php
require __DIR__ . '/vendor/autoload.php';
class ZipFileIndex extends \PhpZip\ZipFile
{
/**
* @param int $index
* @return string
*/
public function getNameByIndex($index)
{
return $this->getListFiles()[$index];
}
/**
* @param int $index
* @return string
*/
public function getEntryContentByIndex($index)
{
return $this[$this->getNameByIndex($index)];
}
/**
* @param int $index
* @return bool
*/
public function hasEntryByIndex($index)
{
return isset($this[$this->getNameByIndex($index)]);
}
/**
* @param int $index
* @return \PhpZip\Model\ZipInfo
*/
public function getEntryInfoByIndex($index)
{
return parent::getEntryInfo($this->getNameByIndex($index));
}
}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ۋ', 'ی'
];
$randomExtList = ['txt', 'jpg', 'gif', 'png', 'dat', 'json'];
function generateRandomName($length)
{
global $arabicAlphabet, $randomExtList;
$name = '';
for ($i = 0; $i < $length; $i++) {
$name .= $arabicAlphabet[array_rand($arabicAlphabet)];
}
$name .= '.' . $randomExtList[array_rand($randomExtList)];
return $name;
}
$outputFile = sys_get_temp_dir() . '/output_test.zip';
$zipFile = new ZipFileIndex();
for ($i = 0; $i < 1000; $i++) {
$entryName = generateRandomName(mt_rand(2, 50));
$zipFile[$entryName] = \PhpZip\Util\CryptoUtil::randomBytes(mt_rand(100, 10000));
}
$zipFile->saveAsFile($outputFile);
$zipFile->close();
$zipFile->openFile($outputFile);
$listFiles = $zipFile->getListFiles();
foreach ($listFiles as $index => $name) {
// link to index
echo 'index ' . $index . ' -> ' . $name . PHP_EOL;
}
echo '-------------' . PHP_EOL;
// index work
$index = mt_rand(1, sizeof($zipFile));
$name = $zipFile->getNameByIndex($index);
echo 'index ' . $index . ':' . PHP_EOL;
echo 'has ' . $index . ' index: ' . var_export($zipFile->hasEntryByIndex($index), true) . PHP_EOL;
echo 'name: ' . $name . PHP_EOL;
echo 'info: ' . $zipFile->getEntryInfoByIndex($index) . PHP_EOL;
echo 'content: ' . $zipFile->getEntryContentByIndex($index) . PHP_EOL;
$zipFile->close();