Extract a file from a ZIP string

前端 未结 6 1766
野趣味
野趣味 2020-12-15 17:54

I have a BASE64 string of a zip file that contains one single XML file.

Any ideas on how I could get the contents of the XML file without having to deal with files o

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 18:45

    After some hours of research I think it's surprisingly not possible do handle a zip without a temporary file:

    1. The first try with php://memory will not work, beacuse it's a stream that cannot be read by functions like file_get_contents() or ZipArchive::open(). In the comments is a link to the php-bugtracker for the lack of documentation of this problem.
    2. There is a stream support ZipArchive with ::getStream() but as stated in the manual, it only supports reading operation on an opened file. So you cannot build a archive on-the-fly with that.
    3. The zip:// wrapper is also read-only: Create ZIP file with fopen() wrapper
    4. I also did some attempts with the other php wrappers/protocolls like

       file_get_contents("zip://data://text/plain;base64,{$base64_string}#test.txt")
       $zip->open("php://filter/read=convert.base64-decode/resource={$base64_string}")
       $zip->open("php://filter/read=/resource=php://memory")
      

      but for me they don't work at all, even if there are examples like that in the manual. So you have to swallow the pill and create a temporary file.


    Original Answer:

    This is just the way of temporary storing. I hope you manage the zip handling and parsing of xml on your own.

    Use the php php://memory (doc) wrapper. Be aware, that this is only usefull for small files, because its stored in the memory - obviously. Otherwise use php://temp instead.

    open('php://memory'));
    

提交回复
热议问题