Skip to content

Commit

Permalink
Added splitter to split way large files
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunjangra committed Dec 18, 2014
1 parent e1eaff2 commit 0ac4849
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Importer/Splitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: tarunjangra
* Date: 18/12/14
* Time: 18:38
*/

namespace Importer;


class Splitter {
private $config = null;

public function __construct(&$config){
$this->config = $config;
}

public function split($size = 90000000){
$done = false;
$part = 0;
$filename = basename($this->config->source_file,'.csv');
if (($handle = fopen($this->config->source_file, "r")) !== FALSE) {
$header = fgets($handle);
while ($done == false) {
$locA = ftell($handle); // gets the current location. START
fseek($handle, $size, SEEK_CUR); // jump the length of $size from current position
$tmp = fgets($handle); // read to the end of line. We want full lines
$locB = ftell($handle); // gets the current location. END
$span = ($locB - $locA);
fseek($handle, $locA, SEEK_SET); // jump to the START of this chunk
$chunk = fread($handle,$span); // read the chunk between START and END
$destination = $this->config->workarea_root.date('Y/m/d').DIRECTORY_SEPARATOR.$this->config->workarea.DIRECTORY_SEPARATOR.$filename;
file_put_contents($destination.'_'.$part.'.csv', $header.$chunk);
$part++;
if (strlen($chunk) < $size) $done = true;
}
fclose($handle);
}
}

}

0 comments on commit 0ac4849

Please sign in to comment.