2011-09-12 12 views
5

मैं एक ज़िप संग्रह डाउनलोड करना चाहता हूं और इसे PHP का उपयोग करके स्मृति में अनजिप करना चाहता हूं।मेमोरी डाउनलोड और निकालें ज़िप संग्रह

यह है कि मैं क्या आज है (और यह सिर्फ बहुत ज्यादा फ़ाइल से निपटने मेरे लिए :) है):

// download the data file from the real page 
copy("http://www.curriculummagic.com/AdvancedBalloons.kmz", "./data/zip.kmz"); 

// unzip it 
$zip = new ZipArchive; 
$res = $zip->open('./data/zip.kmz'); 
if ($res === TRUE) { 
    $zip->extractTo('./data'); 
    $zip->close(); 
} 

// use the unzipped files... 

उत्तर

8

चेतावनी: यह स्मृति में नहीं किया जा सकता - ZipArchive के साथ "स्मृति मैप की गई फ़ाइलें" काम नहीं कर सकता।

आप file_get_contentsDocs साथ एक चर (स्मृति) में एक ज़िप-फाइल के अंदर एक फ़ाइल का डेटा प्राप्त कर सकते हैं के रूप में यह zip:// Stream wrapper Docs समर्थन करता है:

$zipFile = './data/zip.kmz';  # path of zip-file 
$fileInZip = 'test.txt';   # name the file to obtain 

# read the file's data: 
$path = sprintf('zip://%s#%s', $zipFile, $fileInZip); 
$fileData = file_get_contents($path); 

आप केवल zip:// साथ या ZipArchive के माध्यम से स्थानीय फ़ाइलों तक पहुंच सकते हैं। उसके लिए आपको पहले एक अस्थायी फ़ाइल के लिए सामग्री की प्रतिलिपि और इसके साथ काम कर सकते हैं:

$zip = 'http://www.curriculummagic.com/AdvancedBalloons.kmz'; 
$file = 'doc.kml'; 

$ext = pathinfo($zip, PATHINFO_EXTENSION); 
$temp = tempnam(sys_get_temp_dir(), $ext); 
copy($zip, $temp); 
$data = file_get_contents("zip://$temp#$file"); 
unlink($temp); 
+0

क्या मैं '$ zipFile = 'http: //..../bla.kmz' लिख सकता हूं;'? – dacwe

+0

नहीं, 'ज़िप: //' अब तक केवल स्थानीय फाइलों का समर्थन करता है (साथ ही ज़िपप्राइव)। आपको इसे अपने मानक फाइल-सिस्टम के माध्यम से एक्सेस करना होगा। यह आपके 'http' यूआरएल के लिए दृश्यमान बनाने के लिए उत्तर अपडेट करेगा। – hakre

+0

क्या यह 'php: // memory' के साथ काम करता है? – dacwe

0

आप ज़िप के अंदर एक फाइल करने के लिए एक धारा हो और एक चर में निकाल सकते हैं:

$fp = $zip->getStream('test.txt'); 
if(!$fp) exit("failed\n"); 

while (!feof($fp)) { 
    $contents .= fread($fp, 1024); 
} 

fclose($fp); 
+0

मैं कैसे फ़ाइल और '$ zip' को" पाइप "इसे डाउनलोड कर सकते हैं? – dacwe

0

आप सिस्टम कॉल का उपयोग कर सकते हैं, तो सबसे आसान तरीका यह (bzip2 मामले) की तरह दिखना चाहिए। आप बस stdout का उपयोग करें।

$out=shell_exec('bzip2 -dkc '.$zip); 
2

के रूप में आसान के रूप में:

$zipFile = "test.zip"; 
$fileInsideZip = "somefile.txt"; 
$content = file_get_contents("zip://$zipFile#$fileInsideZip"); 
संबंधित मुद्दे