Read json file compressed in gz in PHP

2

Good morning everyone! how are you? I told them that I have a problem, I have a .gz file that contains a huge json, my question is if there is a possibility to access it via php, without the need to decompress it manually.

Greetings!

    
asked by Jonathan Pradi 18.02.2017 в 19:47
source

2 answers

0

You could try this:

$archivos = new PharData('miArchivo.tar.gz');
foreach($archivos as $archivo) { 
    $contenido = file_get_contents($archivo); // Obtengo el contenido del archivo
    $json = json_decode($contenido, true); // Parseo el JSON
    $jsonIterator = new RecursiveIteratorIterator( 
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST); // Genero un iterador de keys y valores

    // Imprimo los resultados
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "$key:\n";
        } else {
            echo "$key => $val\n";
        }
    }
}
    
answered by 18.02.2017 в 20:49
0

PHP has a feature module for working with .gz files: Zlib . It is available for PHP 4, 5 and 7, and will allow you to do among other things:

  • Compress and decompress chains with gzip
  • Open and read gz files
  • Write and modify filez gz

Then you should not manually unzip the gz file to work with it in PHP, because you can directly do it with PHP. Even, you would not need to unzip it to read the JSON it contains. A quick way to do that would be to use gzfile that reads the contents of a gz file and puts it in an array (each row will be an element of the array).

This can be done like this:

// lee el contenido del archivo gz a una variable (array)
$archivoGZ = gzfile("miArchivo.gz");

// convierte ese array a una cadena de texto
$cadenaGZ = implode("", $archivoGZ);

// conviértela a un objeto procesable por PHP
$jsonGZ = json_decode($cadenaGZ);

Or on a single line:

$jsonGZ = json_decode( implode("", gzfile("miArchivo.gz") ) );

For example, imagine that you have a JSON file with the following content:

[
    {
        "nombre": "Juan",
        "apellido": "Montero",
        "edad": 23
    },{
        "nombre": "Miguel",
        "apellido": "Pardo",
        "edad": 45
    },{
        "nombre": "Manuel",
        "apellido": "Castillo",
        "edad": 43
    },{
        "nombre": "Mariano",
        "apellido": "Blanco",
        "edad": 35
    }
]

The result of the previous code (if you make a var_dump ) would look like this:

array (size=4)
  0 => 
    object(stdClass)[1]
      public 'nombre' => string 'Juan' (length=4)
      public 'apellido' => string 'Montero' (length=7)
      public 'edad' => int 23
  1 => 
    object(stdClass)[2]
      public 'nombre' => string 'Miguel' (length=6)
      public 'apellido' => string 'Pardo' (length=5)
      public 'edad' => int 45
  2 => 
    object(stdClass)[3]
      public 'nombre' => string 'Manuel' (length=6)
      public 'apellido' => string 'Castillo' (length=8)
      public 'edad' => int 43
  3 => 
    object(stdClass)[4]
      public 'nombre' => string 'Mariano' (length=7)
      public 'apellido' => string 'Blanco' (length=6)
      public 'edad' => int 35
    
answered by 23.02.2017 в 18:15