I think str_replace () is pretty obvious. But this memory intensity is for large files.
I will not show all the code, but php uses a rarely used function, where you can apply filters to streams, and filters can be specified as part of the file URL. This makes it easy to connect to certain file system operations that would otherwise prevent you from easily customizing the behavior.
Once you define a filter with stream_filter_register (), you should be able to pass the file URL, for example php://filter/string.to_unix_line_endings/resource=/path/to/the/file.txt
, to ZipArchive::addFile()
ultimately, you could just create a temporary file and copy the contents of the file into it in pieces, str_replacing as you go through, and then pass the name tempfile to the ZipArchive::addFile()
method. And that would be much easier to code.
source share