Jump to content

How do I write to a gz file?


beginnerForPHP

Recommended Posts

I would like to read from an existing .csv file and create and write the contents in a different format to a new test.csv.gz file.

private function createFile($id, $date, $file)
{
    if(file_exists($file)){
        $outputDirHandle = gzopen($file, 'r');
        $newfile = $id . DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR.'test.csv.gz';
        $handle = gzopen($newfile, 'w');
        $temp = [];
        while (! gzeof($outputDirHandle) && $values = fgetcsv($outputDirHandle)) {
            foreach($values as $key => $val) {
                $data = explode(';', $val);
            }
            $temp[$data[0]][] = $data[2];
        }
        gzclose($handle);
    }
}

The content of the csv file that i am reading is in the following format(date1;hours;price):

The content of the csv file that i am reading is in the following format:

2021-06-30;01:00:00;78
2021-06-30;02:00:00;75.9
2021-06-30;03:00:00;72
2021-06-30;04:00:00;70
2021-06-30;05:00:00;78
2021-06-30;06:00:00;84
2021-06-30;07:00:00;92
2021-06-30;08:00:00;99.7
2021-06-30;09:00:00;101.35
2021-06-30;10:00:00;99.2
2021-06-30;11:00:00;96.6
2021-06-30;12:00:00;95.4
2021-06-30;13:00:00;86
2021-06-30;14:00:00;79.5
2021-06-30;15:00:00;81
2021-06-30;16:00:00;90
2021-06-30;17:00:00;100.9
2021-06-30;18:00:00;102
2021-06-30;19:00:00;97.9
2021-06-30;20:00:00;87
2021-06-30;21:00:00;85
2021-06-30;22:00:00;80.1
2021-06-30;23:00:00;87.4 

I need to read the above file and write to test.csv.gz in the below format. (date1;price;price;price.....price)

2021-06-30;78;75.9;72;70;78;84;92;99.7;101.35;99.2;96.6;95.4;86;79.5;81;90;100.9;102;97.9;87;85;80.1;87.4

Any help is appreciated.

Link to comment
Share on other sites

You need to change this from reading a csv file since it really isn't one.  It is simply a text file that you wan't to break up at the semis.

    while (! gzeof($outputDirHandle) && $values = fgetcsv($outputDirHandle)) {
            foreach($values as $key => $val) {
      

to be:

while (!gzeof($outputDirHandle))
{
	$values = fread($outputDirHandle)
	//  this gets: 2021-06-30;01:00:00;78
	$data = explode(';', $val);
	//  this gets: [0]=>2021-06-30, [1]=>01:00:00;, [2]=>78
	$temp[$data[0]][] = $data[2];
	//  this gets: [2021-06-30]=>78
}

You aren't writing anything out so that may be why you don't see any output file.  Plus as you can see I don't think you are creating the right output element.

I hope that I have read this correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.