php-zip icon indicating copy to clipboard operation
php-zip copied to clipboard

Get information about a file by a numeric key

Open visavi opened this issue 7 years ago • 7 comments

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()

visavi avatar Nov 10 '17 13:11 visavi

Hi. Use the php function array_values().

$zipAllInfo = $zipFile->getAllInfo();
$zipAllInfo = array_values($zipAllInfo);
var_dump($zipAllInfo);

Ne-Lexa avatar Nov 10 '17 13:11 Ne-Lexa

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

visavi avatar Nov 10 '17 13:11 visavi

you can do it like this, but it's not very nice

var_dump($archive[$archive->getListFiles()[$id]]);

visavi avatar Nov 10 '17 13:11 visavi

Describe the task that you want to solve, and for which you need numeric indexes.

Ne-Lexa avatar Nov 10 '17 21:11 Ne-Lexa

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

visavi avatar Nov 10 '17 21:11 visavi

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

visavi avatar Nov 10 '17 21:11 visavi

Раз комментарии в ссылках на русском, напишу на нём. Это будет добавлено в один из следующих релизов. На готове релиз 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));
    }
}

$arabicAlphabet = [
    'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س',
    'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ػ', 'ؼ', 'ؽ', 'ؾ', 'ؿ', 'ـ',
    'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ',
    'ُ', 'ِ', 'ّ', 'ْ', 'ٓ', 'ٔ', 'ٕ', 'ٖ', 'ٗ', '٘', 'ٙ', 'ٚ', 'ٛ', 'ٜ', 'ٝ', 'ٞ', 'ٟ', '٠',
    '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩', '٪', '٫', '٬', '٭',
    'ٮ', 'ٯ', 'ٰ', 'ٱ', 'ٲ', 'ٳ', 'ٴ', 'ٵ', 'ٶ', 'ٷ', 'ٸ', 'ٹ', 'ٺ',
    'ٻ', 'ټ', 'ٽ', 'پ', 'ٿ', 'ڀ', 'ځ', 'ڂ', 'ڃ', 'ڄ', 'څ', 'چ', 'ڇ',
    'ڈ', 'ډ', 'ڊ', 'ڋ', 'ڌ', 'ڍ', 'ڎ', 'ڏ', 'ڐ', 'ڑ', 'ڒ', 'ړ', 'ڔ', 'ڕ',
    'ږ', 'ڗ', 'ژ', 'ڙ', 'ښ', 'ڛ', 'ڜ', 'ڝ', 'ڞ', 'ڟ', 'ڠ', 'ڡ',
    'ڢ', 'ڣ', 'ڤ', 'ڥ', 'ڦ', 'ڧ', 'ڨ', 'ک', 'ڪ', 'ګ', 'ڬ', 'ڭ',
    'ڮ', 'گ', 'ڰ', 'ڱ', 'ڲ', 'ڳ', 'ڴ', 'ڵ', 'ڶ', 'ڷ', 'ڸ', 'ڹ',
    'ں', 'ڻ', 'ڼ', 'ڽ', 'ھ', 'ڿ', 'ۀ', 'ہ', 'ۂ', 'ۃ', 'ۄ', 'ۅ',
    'ۆ', 'ۇ', 'ۈ', 'ۉ', 'ۊ', 'ۋ', 'ی'
];

$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();

Ne-Lexa avatar Nov 10 '17 22:11 Ne-Lexa