Search in file descriptor pointing to file in zip?

I want to print a specific line (say 200) of a file in a zip archive. I am trying to do the following:

$file = new SplFileObject("zip://archive.zip#file.txt"); $file->seek(200); echo $file->key() . "\n"; echo $file->current(); 

But I get

 PHP Warning: SplFileObject::seek(): stream does not support seeking in script.php on line 2 PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Cannot rewind file zip://archive.zip#file.txt' in script.php:2 

If I unzip the file and create a SplFileObject with the parameter "file.txt" , it works fine. Is it documented somewhere that the search does not work for zip files? I could not find him. Or am I doing something wrong? Thanks.

+6
source share
1 answer
Good question. The behavior you want is clearly expressed by the syntax you used. I think this is a mistake. You can probably report this at http://bugs.php.net . The stream must be processed internally without rewinding and caching.

Be careful! The solution with SplFileObject is more than wild.

NoRewind is required to prohibit rewinding. Then Cache is needed to store the contents of the stream inside and makes it viewable. Then LimitIterator is needed to search for line 200.

Here we go:

 $obj = new SplFileObject("zip://archive.zip#file.txt"); $norewind = new NoRewindIterator($obj); $caching = new CachingIterator($norewind); $limit = new LimitIterator($caching, 200, 1); foreach ($limit as $i => $line) { printf("%03d: %s", $i, $line); } 
+2
source

Source: https://habr.com/ru/post/972898/


All Articles