Parsing a Zipped JSON file in PHP without Extracting
With help from the guys on Stackoverflow I can now Parse JSON code from a
file and save a 'Value' into a database
However the real file I intend to read from is actually a massive 2GB
file. My web server will not hold this file. However it will hold a ZIPPED
version of it - ie 80MB.
I believe there is a way to PARSE JSON from a ZIPPED file without actually
having to save the 2GB version of the file on my web server only having to
save the ZIPPED 80MB version instead............Can anybody help?
I have found the below function which I believe will do this (I think) but
I don't know how to link it to my code
private function uncompressFile($srcName, $dstName) {
$sfp = gzopen($srcName, "rb");
$fp = fopen($dstName, "w");
while ($string = gzread($sfp, 4096)) {
fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);
}
My current PHP code is below and works. It reads a basic small file, JSON
decodes it (The JSON is in a series of separate lines hence the need for
FILE_IGNORE_NEW_LINES) and then takes a value and saves to MySQL database.
However I believe I need to somehow combine these two bits of code so I
can read a ZIPPED file without exceeding my 100MB storage on my webserver
$file="CIF_ALL_UPDATE_DAILY_toc-update-sun";
$trains = file($json_filename, FILE_IGNORE_NEW_LINES |
FILE_SKIP_EMPTY_LINES);
foreach ($trains as $train) {
$json=json_decode($train,true);
foreach ($json as $key => $value) {
$input=$value['main_train_uid'];
$q="INSERT INTO railstptest (main_train_uid) VALUES ('$input')";
$r=mysqli_query($mysql_link,$q);
}
}
}
if (is_null($json)) {
die("Json decoding failed with error: ". json_last_error());
}
mysqli_close($mysql_link);
Many Thanks
No comments:
Post a Comment