Kir Mazur
Kir Mazur

Reputation: 1037

BASE64 decode and ungzip data in PHP

I am working with OpenSubtitles API and need to BASE64 decode and ungzip API response ('$data' parameter) in PHP.

Full '$data' parameter you can find here: https://jsfiddle.net/noxjmz40/

I can easily BASE64 decode this parameter with base64_decode($data) but I am not sure how to ungzip and get a normal result.

Please, advice me how to solve this task in a short and efficient way. Thanks!

Upvotes: 0

Views: 2644

Answers (3)

WasteD
WasteD

Reputation: 778

Found an easy solution after some seconds of searching. The solution is gzdecode().

$data = ... ; // Your string
$decoded = base64_decode($data);
$plain_string = gzdecode($decoded);

Upvotes: 2

Shantanu
Shantanu

Reputation: 692

This way you can get your output

$output = gzdecode(base64_decode($data));

Upvotes: 1

user7098858
user7098858

Reputation:

<?php
$zipStr = 'UEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAAWAAAAb2JqZWN0cy9Db250YWN0Lm9iamVjdI2SX0vDMBTF3/cpSt9NOhERyTJCl+Gga6VNBZ9GlmWuI21mk0799ka7P50TXR4C9+R3L5ecg4bvpfK2sjaFrgZ+HwS+JyuhF0X1MvBzNr6684e4h8LGWF0m87UU1nMtlRn4K2s39xAazTfALHUtJBC6hNdBcAuDG1hKyxfcch/3PHeQKox9KuSbaetvbdkoFfNSYqEry4WdpXxdzBE86EdUaNWUlcHjPIpmMZlSBPfSOUTCMMljBv7jwiRmJGSATVh0Efj4kMS0fwlJp2QS/Q2mFOQZTTNAognJfmOXhbKyzoTeSEydSx925Yxx/9PRf9Kd/p0q1eKwVphSwuhoNnL315yvt1Pezay5dXHAShqT1PS14QrBo3yKb7lqJGbJiDwj2BbHjeDZSkjxuVR7v72d363Y5gR2goJgN3i49wlQSwcI3kTEMT8BAACvAgAAUEsDBBQACAAIABprdEEAAAAAAAAAAAAAAAALAAAAcGFja2FnZS54bWxNj00LwjAMhu/7FaN3lzpkiHTdQfDkQUS9StbFWbXtWIsf/96xDzSX5CF53ySieJtH/KTWa2dzNk84i8kqV2lb5+x42MyWrJCR2KG6Y01xN219zq4hNCsA77BJ/MW1ihLlDKScZ8AXYChghQGZjOIuRPg05Ie6Z0Om7FbKtbMBVei0fT7v8aZLAVP7J7BoSG61DydNLwE9Dtbw5y3GP2SaJVzARJGA8XwZfQFQSwcIaf0pNKsAAADwAAAAUEsBAhQAFAAIAAgAGmt0Qd5ExDE/AQAArwIAABYAAAAAAAAAAAAAAAAAAAAAAG9iamVjdHMvQ29udGFjdC5vYmplY3RQSwECFAAUAAgACAAaa3RBaf0pNKsAAADwAAAACwAAAAAAAAAAAAAAAACDAQAAcGFja2FnZS54bWxQSwUGAAAAAAIAAgB9AAAAZwIAAAAA';

// Prepare Tmp File for Zip archive
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

// Add contents
$zip->addFromString('your_file_name', base64_decode($zipStr));

// Close and send to users
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file);

Upvotes: 0

Related Questions