Extract a file from a ZIP string

前端 未结 6 1772
野趣味
野趣味 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:35

    I had a similar problem, I ended up doing it manually.
    https://www.pkware.com/documents/casestudies/APPNOTE.TXT

    This extracts a single file (just the first one), no error/crc checks, assumes deflate was used.

    // zip in a string
    $data = file_get_contents('test.zip');
    
    // magic
    $head = unpack("Vsig/vver/vflag/vmeth/vmodt/vmodd/Vcrc/Vcsize/Vsize/vnamelen/vexlen", substr($data,0,30));
    $filename = substr($data,30,$head['namelen']);
    $raw = gzinflate(substr($data,30+$head['namelen']+$head['exlen'],$head['csize']));
    
    // first file uncompressed and ready to use
    file_put_contents($filename,$raw);
    

提交回复
热议问题