-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathdescomprimir.php
More file actions
53 lines (42 loc) · 1.79 KB
/
descomprimir.php
File metadata and controls
53 lines (42 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require 'vendor/autoload.php';
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
$connectionString = getenv("AZURE_STORAGE_CONNECTION_STRING");
$sourceContainer = "comprimidos";
$targetContainer = "descomprimidos";
$blobClient = BlobRestProxy::createBlobService($connectionString);
// Opcional: Obtener el primer archivo ZIP del contenedor
$options = new ListBlobsOptions();
$options->setPrefix(""); // Sin prefijo específico
$blobs = $blobClient->listBlobs($sourceContainer, $options);
foreach ($blobs->getBlobs() as $blob) {
if (strtolower(pathinfo($blob->getName(), PATHINFO_EXTENSION)) !== "zip") {
continue;
}
echo "Procesando ZIP: " . $blob->getName() . PHP_EOL;
// Descargar el ZIP
$zipContent = $blobClient->getBlob($sourceContainer, $blob->getName())->getContentStream();
$tempZip = tempnam(sys_get_temp_dir(), 'zip');
file_put_contents($tempZip, stream_get_contents($zipContent));
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
$fileContent = $zip->getFromIndex($i);
if ($fileContent !== false) {
$uploadOptions = new CreateBlockBlobOptions();
$blobClient->createBlockBlob($targetContainer, $entry, $fileContent, $uploadOptions);
echo "Extraído y subido: $entry" . PHP_EOL;
}
}
$zip->close();
} else {
echo "Error abriendo el ZIP: " . $blob->getName() . PHP_EOL;
}
unlink($tempZip);
break; // Solo procesamos el primer ZIP para este ejemplo
}
?>