0

I want to read a the .txt file content from a .zip file that contains many other .txt files. But hope do not need to download the .zip file to the local machine..

How can I use functions like zip_open/zip_read to do it?

$path = 'Data/2021';

ftp_login($conn,$user,$pass);
     
ftp_chdir($conn,$path);
 
ftp_pasv($conn,TRUE);
 
foreach (ftp_nlist($conn,".") as $Lot_Zip) {
    $Lot_Zip = Trim($Lot_Zip);
    
    $Lot_Zip_Name = substr($Lot_Zip,0,10);
    
    if (preg_match("/$Lot_Zip_Name/i",$ParsedLot_String)){
           
        echo "processing ", $Lot_Zip, " ...\n";    

        // $Lot_Zip = $path.'/'.$Lot_Zip also can't be zip_open
        $zip = zip_open($Lot_Zip);
       
        echo "ZIP: ", $zip ,"\n";  //return value is 11
       
        if ($zip)
        {
            while ($zip_entry = zip_read($zip))
            {
                echo "Name: " . zip_entry_name($zip_entry) . "\n";
            }
        }
    }
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jie Lu
  • 1
  • 1

1 Answers1

0

Opening a connection to an FTP server won't make functions magically use files on the FTP server. The $Lot_Zip in zip_open($Lot_Zip) refers to the local file on the machine that runs your PHP script (webserver?), not files on the FTP server. With some PHP functions you can use ftp:// URL wrapper to refer to the files on the FTP server. But I believe that neither the deprecated zip_open, nor the up-to-date FtpArchive support the URL wrappers. They can work with local files only.

So the easiest solution is to download the archive from the FTP server to the local machine/webserver.

In general, it's rather complicated to work with ZIP files on an FTP server. Working with ZIP files requires a random access to the file contents, what is not easy to achieve with the FTP protocol.

See this related (Python) question that discusses the problem into more details:
Get files names inside a zip file on FTP server without downloading whole archive

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • So my question is how to process ZIP files on the FTP server without downloading to the local, even to access to the .txt file in the Zip. I know it is complicated but if it's achievable – Jie Lu Jul 08 '21 at 08:26
  • It is soo complicated, you will hardly get an answer here. Unless there's a ready-made library that can do it. – Martin Prikryl Jul 09 '21 at 14:47