get_file_contents of zipped file - php

I've got a ZIP file sitting on my server. I want to unzip it and then save the completely file contents into just one variable.
I do NOT want to save the unzipped file on my server or on a visitor's computer. I just need all of the contents of that zipped file stored in a variable that I can play around with and eventually show on the screen. Every other solution I've found for this problem includes resaving the file in unzipped form.
How can I do this with get_file_contents, or any other function?

You can simply find out the file names within the ZIP Archive with PHPs ZipArchive for example. try this (have not tested it but you should be able to get it to work) :
$za = new ZipArchive();
$za->open('archive.zip');
$fileContents = array();
for( $i = 0; $i < $za->numFiles; $i++ ) {
$stat = $za->statIndex( $i );
$fp = $z->getStream($stat['name']);
if(!$fp) exit("failed\n");
$contents = '';
while (!feof($fp)) {
$contents .= fread($fp, 1000);
}
fclose($fp);
$fileContents[$stat['name']] = $contents;
}
Once you know the names of the files within the Zip you can also use something like this:
$path = sprintf('zip://%s#%s', $zipArchive, $fileNameInZipArchive);
$fileData = file_get_contents($path);

Related

Cannot do raw printing from online website

I have a function to do raw printing like this:
function execPrint($pPrinterShareName, $pInit, $pData)
{
$tmpdir = sys_get_temp_dir(); # temporary directory to save temporary file
$x = 0;
$file = tempnam($tmpdir, 'cetak_struk__'.$pInit.'_'.$x); # temporary file name that will be printed
$handle = fopen($file, 'w');
fwrite($handle, $pData);
fclose($handle);
sleep(1);
chmod($file, 0777);
$v='';
$v = copy($file, "//localhost/".$pPrinterShareName);
sleep(1);
return $v;
}
this function will create a temporary folder and put a temporary file there. Then the file will be printed by copying that file to my localhost printer. $pPrinter
I can do this if my application is in my localhost, but how can I do that if my application is in an online hosting? Please help...

Extract a file from a zip archive then rename it

I have this file that gets downloaded at:
DownloadFile($reportDownloadUrl, $DownloadPath);
But it's a zip file. Inside of it, a CSV file gets created with a random name i.e random_name.csv
How do I extract this folder abc.zip in php and rename this file with random name to new_name.csv
Problem is that I can't use
$zip->renameName('currentname.csv','newname.csv');
since I don't have currentname.
This code inspects the file in zipLocation then iterates over them to check if there are csv files. If it finds something it copies inside the directory with its original name, then copies another copy with a new name.
$zipLocation = "path/to/file.zip";
$zip = new ZipArchive;
if ($zip->open($zipLocation) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
if (pathinfo($filename, PATHINFO_EXTENSION)=="csv"){
$fileinfo = pathinfo($filename);
copy("zip://".$zipLocation."#".$filename, "./newname.csv");
copy("zip://".$zipLocation."#".$filename, "./".$fileinfo['basename']);
}
}
$zip->close();
}

Count Lines at a ftp .txt php

I have a quesion: How to count lines at a file over ftp?
Like: I have a file generated by C# shared by ftp. How to count the lines at php at my webserver?
Thanks, Jonathan
if you can access the file then can do this:
$file="yourfile.txt";
$count = 0;
$fp = fopen($file, "r");
while(!feof($fp)){
$line = fgets($fp);
$count++;
}
fclose($fp);
echo $count;
you can use count() also :
$count = count( file( $file));
but if the file is big you should avoid using this.

Split tar.gz files using php

I am importing data feed into mywebsite.My datafeed files are came from tar.gz format. The file size is is very large upto 10 gb. i need to split tar.gz file and then upload to database. How can i split tar.gz single file to multiple file. Please Guide me.
You can create several parts like this:
$fileName = 'something_big.tar.gz';
$fileSize = filesize($fileName);
$parts = 10;
$partSize = ceil($fileSize / 10);
$f_in = fopen($fileName, 'rb');
for ($i = 0; $i < $parts; ++$i) {
$f_out = fopen("$fileName.$i", 'wb');
stream_copy_to_stream($f_in, $f_out, $partSize);
fclose($f_out);
}
fclose($f_in);

In memory download and extract zip archive

I would like to download a zip archive and unzip it in memory using PHP.
This is what I have today (and it's just too much file-handling for me :) ):
// 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...
Warning: This cannot be done in memory — ZipArchive cannot work with "memory mapped files".
You can obtain the data of a file inside a zip-file into a variable (memory) with file_get_contentsDocs as it supports the 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);
You can only access local files with zip:// or via ZipArchive. For that you can first copy the contents to a temporary file and work with it:
$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);
As easy as:
$zipFile = "test.zip";
$fileInsideZip = "somefile.txt";
$content = file_get_contents("zip://$zipFile#$fileInsideZip");
Old subject but still relevant since I asked myself the same question, without finding an answer.
I ended up writing this function which returns an array containing the name of each file contained in the archive, as well as the decompressed contents of that file:
function GetZipContent(String $body_containing_zip_file) {
$sectors = explode("\x50\x4b\x01\x02", $data);
array_pop($sectors);
$files = explode("\x50\x4b\x03\x04", implode("\x50\x4b\x01\x02", $sectors));
array_shift($files);
$result = array();
foreach($files as $file) {
$header = unpack("vversion/vflag/vmethod/vmodification_time/vmodification_date/Vcrc/Vcompressed_size/Vuncompressed_size/vfilename_length/vextrafield_length", $file);
array_push($result, [
'filename' => substr($file, 26, $header['filename_length']),
'content' => gzinflate(substr($file, 26 + $header['filename_length'], -12))
]);
}
return $result;
}
Hope this is useful ...
You can get a stream to a file inside the zip and extract it into a variable:
$fp = $zip->getStream('test.txt');
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 1024);
}
fclose($fp);
If you can use system calls, the simplest way should look like this (bzip2 case). You just use stdout.
$out=shell_exec('bzip2 -dkc '.$zip);

Categories