ZipArchive — ZipArchive-klassen: Skillnad mellan sidversioner
Admin (diskussion | bidrag) Ingen redigeringssammanfattning |
Admin (diskussion | bidrag) Ingen redigeringssammanfattning |
||
| Rad 1: | Rad 1: | ||
== Klassen ZipArchive == | == Klassen [[ZipArchive]] == | ||
''(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0)'' | ''(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0)'' | ||
| Rad 7: | Rad 7: | ||
=== Klassöversikt === | === Klassöversikt === | ||
<pre> | <pre> | ||
class ZipArchive implements Countable { | class [[ZipArchive]] implements [[Countable]] { | ||
/* Konstanter */ | /* Konstanter */ | ||
public const int CREATE; | public const int CREATE; | ||
| Rad 72: | Rad 72: | ||
=== Egenskaper === | === Egenskaper === | ||
* '''lastId''' - Indexvärdet för senast tillagd post (fil eller katalog). | * '''lastId''' - Indexvärdet för senast tillagd post (fil eller katalog). | ||
* '''status''' - Status för | * '''status''' - Status för [[ZipArchive]]-arkivet. | ||
* '''statusSys''' - Systemstatus för | * '''statusSys''' - Systemstatus för [[ZipArchive]]-arkivet. | ||
* '''numFiles''' - Antal filer i arkivet. | * '''numFiles''' - Antal filer i arkivet. | ||
* '''filename''' - Filnamnet i filsystemet. | * '''filename''' - Filnamnet i filsystemet. | ||
| Rad 81: | Rad 81: | ||
==== Lägg till och hantera filer ==== | ==== Lägg till och hantera filer ==== | ||
* '''ZipArchive::addEmptyDir()''' | * '''[[ZipArchive]]::addEmptyDir()''' | ||
Lägg till en ny katalog i arkivet. | Lägg till en ny katalog i arkivet. | ||
* '''ZipArchive::addFile()''' | * '''[[ZipArchive]]::addFile()''' | ||
Lägg till en fil i ZIP-arkivet från angiven sökväg. | Lägg till en fil i ZIP-arkivet från angiven sökväg. | ||
* '''ZipArchive::addFromString()''' | * '''[[ZipArchive]]::addFromString()''' | ||
Lägg till en fil till ett ZIP-arkiv med dess innehåll. | Lägg till en fil till ett ZIP-arkiv med dess innehåll. | ||
==== Extrahera filer ==== | ==== Extrahera filer ==== | ||
* '''ZipArchive::extractTo()''' | * '''[[ZipArchive]]::extractTo()''' | ||
Extrahera innehållet från arkivet till en angiven katalog. | Extrahera innehållet från arkivet till en angiven katalog. | ||
==== Läs och hämta filer ==== | ==== Läs och hämta filer ==== | ||
* '''ZipArchive::getFromName()''' | * '''[[ZipArchive]]::getFromName()''' | ||
Hämta innehållet från en post med dess namn. | Hämta innehållet från en post med dess namn. | ||
* '''ZipArchive::statName()''' | * '''[[ZipArchive]]::statName()''' | ||
Få detaljer om en post med dess namn. | Få detaljer om en post med dess namn. | ||
==== Hantera lösenord och kryptering ==== | ==== Hantera lösenord och kryptering ==== | ||
* '''ZipArchive::setPassword()''' | * '''[[ZipArchive]]::setPassword()''' | ||
Ange lösenord för det aktiva arkivet. | Ange lösenord för det aktiva arkivet. | ||
==== Ta bort filer ==== | ==== Ta bort filer ==== | ||
* '''ZipArchive::deleteName()''' | * '''[[ZipArchive]]::deleteName()''' | ||
Ta bort en post i arkivet med dess namn. | Ta bort en post i arkivet med dess namn. | ||
| Rad 111: | Rad 111: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$zip = new ZipArchive(); | $zip = new [[ZipArchive]](); | ||
$filename = "./example.zip"; | $filename = "./example.zip"; | ||
if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) { | if ($zip->open($filename, [[ZipArchive::CREATE]]) !== TRUE) { | ||
exit("Kan inte skapa <$filename>\n"); | exit("Kan inte skapa <$filename>\n"); | ||
} | } | ||
| Rad 128: | Rad 128: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$zip = new ZipArchive(); | $zip = new [[ZipArchive]](); | ||
if ($zip->open('example.zip') === TRUE) { | if ($zip->open('example.zip') === TRUE) { | ||
$zip->extractTo('./extraherad/'); | $zip->extractTo('./extraherad/'); | ||
| Rad 142: | Rad 142: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$zip = new ZipArchive(); | $zip = new [[ZipArchive]](); | ||
if ($zip->open('example.zip') === TRUE) { | if ($zip->open('example.zip') === TRUE) { | ||
$content = $zip->getFromName('fil1.txt'); | $content = $zip->getFromName('fil1.txt'); | ||
| Rad 152: | Rad 152: | ||
?> | ?> | ||
</pre> | </pre> | ||
= Sidslut = | = Sidslut = | ||
Versionen från 17 november 2024 kl. 12.46
Klassen ZipArchive
(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0)
Introduktion
Ett filarkiv som komprimeras med Zip-format.
Klassöversikt
class [[ZipArchive]] implements [[Countable]] {
/* Konstanter */
public const int CREATE;
public const int EXCL;
public const int CHECKCONS;
public const int OVERWRITE;
public const int RDONLY;
public const int FL_NOCASE;
public const int FL_NODIR;
public const int FL_COMPRESSED;
public const int FL_UNCHANGED;
public const int FL_RECOMPRESS;
public const int FL_ENCRYPTED;
public const int FL_OVERWRITE;
public const int FL_LOCAL;
public const int FL_CENTRAL;
public const int FL_ENC_GUESS;
public const int FL_ENC_RAW;
public const int FL_ENC_STRICT;
public const int FL_ENC_UTF_8;
public const int FL_ENC_CP437;
public const int FL_OPEN_FILE_NOW;
public const int CM_DEFAULT;
public const int CM_STORE;
public const int CM_SHRINK;
public const int CM_DEFLATE;
public const int CM_BZIP2;
public const int CM_LZMA;
public const int CM_ZSTD;
public const int ER_OK;
public const int ER_MULTIDISK;
public const int ER_RENAME;
public const int ER_NOENT;
public const int ER_EXISTS;
public const int ER_OPEN;
public const int ER_MEMORY;
public const int ER_WRONGPASSWD;
public const int AFL_RDONLY;
public const int AFL_IS_TORRENTZIP;
public const int EM_AES_256;
public const string LIBZIP_VERSION;
/* Egenskaper */
public readonly int $lastId;
public readonly int $status;
public readonly int $statusSys;
public readonly int $numFiles;
public readonly string $filename;
public readonly string $comment;
/* Metoder */
public addEmptyDir(string $dirname, int $flags = 0): bool;
public addFile(string $filepath, string $entryname = "", int $flags = 0): bool;
public addFromString(string $name, string $content, int $flags = 0): bool;
public extractTo(string $pathto, array|string|null $files = null): bool;
public close(): bool;
public getFromName(string $name, int $len = 0, int $flags = 0): string|false;
public statName(string $name, int $flags = 0): array|false;
public setPassword(string $password): bool;
public deleteName(string $name): bool;
}
Egenskaper
- lastId - Indexvärdet för senast tillagd post (fil eller katalog).
- status - Status för ZipArchive-arkivet.
- statusSys - Systemstatus för ZipArchive-arkivet.
- numFiles - Antal filer i arkivet.
- filename - Filnamnet i filsystemet.
- comment - Kommentar för arkivet.
Metoder
Lägg till och hantera filer
- ZipArchive::addEmptyDir()
Lägg till en ny katalog i arkivet.
- ZipArchive::addFile()
Lägg till en fil i ZIP-arkivet från angiven sökväg.
- ZipArchive::addFromString()
Lägg till en fil till ett ZIP-arkiv med dess innehåll.
Extrahera filer
- ZipArchive::extractTo()
Extrahera innehållet från arkivet till en angiven katalog.
Läs och hämta filer
- ZipArchive::getFromName()
Hämta innehållet från en post med dess namn.
- ZipArchive::statName()
Få detaljer om en post med dess namn.
Hantera lösenord och kryptering
- ZipArchive::setPassword()
Ange lösenord för det aktiva arkivet.
Ta bort filer
- ZipArchive::deleteName()
Ta bort en post i arkivet med dess namn.
Exempel
Skapa ett Zip-arkiv
<?php
$zip = new [[ZipArchive]]();
$filename = "./example.zip";
if ($zip->open($filename, [[ZipArchive::CREATE]]) !== TRUE) {
exit("Kan inte skapa <$filename>\n");
}
$zip->addFromString("fil1.txt", "Detta är innehållet i fil1.\n");
$zip->addFile("/path/till/fil2.php", "fil2.php");
$zip->close();
echo "Arkivet $filename har skapats.\n";
?>
Extrahera innehåll från ett Zip-arkiv
<?php
$zip = new [[ZipArchive]]();
if ($zip->open('example.zip') === TRUE) {
$zip->extractTo('./extraherad/');
$zip->close();
echo "Extrahering lyckades!\n";
} else {
echo "Misslyckades med att öppna arkivet.\n";
}
?>
Läsa innehåll från en specifik fil i arkivet
<?php
$zip = new [[ZipArchive]]();
if ($zip->open('example.zip') === TRUE) {
$content = $zip->getFromName('fil1.txt');
echo "Innehåll i fil1.txt:\n$content\n";
$zip->close();
} else {
echo "Misslyckades med att öppna arkivet.\n";
}
?>
Sidslut
Orginalhemsidan på Engelska :https://www.php.net/manual/en/class.ziparchive.php
PHP
Funktioner
Funktionsreferens
Komprimerings- och arkivtillägg
Zip Funktioner
Det här är en maskinöversättning av PHP-manualen till svenska. Om du hittar fel är vi tacksamma om du rapporterar dem via formuläret som finns på
https://www.linux.se/kontaka-linux-se/
Tack till Datorhjälp Stockholm som har sponsrat Linux.se med webbhotell.