ZipArchive — ZipArchive-klassen: Skillnad mellan sidversioner
Admin (diskussion | bidrag) (Skapade sidan med '== Fördefinierade Konstanter == Följande konstanter är definierade av denna extension och är endast tillgängliga när extensionen antingen har kompilerats in i PHP eller laddats dynamiskt vid körning. ''ZipArchive'' använder klasskonstanter. Det finns olika typer av konstanter, där de viktigaste är: '''Flaggor''' (prefixerade med ''FL_''), '''Globala flaggor''' (prefixerade med ''AFL_''), '''Fel''' (prefixerade med ''ER_'') och '''lägen''' (utan prefix). ===...') |
Admin (diskussion | bidrag) Ingen redigeringssammanfattning |
||
| Rad 1: | Rad 1: | ||
== | == 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 === | ||
* | <pre> | ||
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; | |||
} | |||
</pre> | |||
=== | === Egenskaper === | ||
* ''' | * '''lastId''' - Indexvärdet för senast tillagd post (fil eller katalog). | ||
* '''status''' - Status för Zip-arkivet. | |||
* '''statusSys''' - Systemstatus för Zip-arkivet. | |||
* '''numFiles''' - Antal filer i arkivet. | |||
* ''' | * '''filename''' - Filnamnet i filsystemet. | ||
* '''comment''' - Kommentar för arkivet. | |||
* ''' | |||
* ''' | |||
* ''' | |||
* ''' | |||
=== | === Metoder === | ||
=== | ==== Lägg till och hantera filer ==== | ||
* '''ZipArchive:: | * '''ZipArchive::addEmptyDir()''' | ||
Lägg till en ny katalog i arkivet. | |||
* '''ZipArchive:: | * '''ZipArchive::addFile()''' | ||
Lägg till en fil i ZIP-arkivet från angiven sökväg. | |||
* '''ZipArchive:: | * '''ZipArchive::addFromString()''' | ||
Lägg till en fil till ett ZIP-arkiv med dess innehåll. | |||
=== | ==== Extrahera filer ==== | ||
* '''ZipArchive:: | * '''ZipArchive::extractTo()''' | ||
Extrahera innehållet från arkivet till en angiven katalog. | |||
=== | ==== Läs och hämta filer ==== | ||
* '''ZipArchive:: | * '''ZipArchive::getFromName()''' | ||
Hämta innehållet från en post med dess namn. | |||
* '''ZipArchive::statName()''' | |||
Få detaljer om en post med dess namn. | |||
* '''ZipArchive:: | |||
''( | ==== 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 ==== | |||
<pre> | |||
<?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"; | |||
?> | |||
</pre> | |||
==== Extrahera innehåll från ett Zip-arkiv ==== | |||
<pre> | |||
<?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"; | |||
} | |||
?> | |||
</pre> | |||
==== Läsa innehåll från en specifik fil i arkivet ==== | |||
<pre> | |||
<?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"; | |||
} | |||
?> | |||
</pre> | |||
= Sidslut = | = Sidslut = | ||
Orginalhemsidan på Engelska :https://www.php.net/manual/en/ | Orginalhemsidan på Engelska :https://www.php.net/manual/en/class.ziparchive.php | ||
<BR>[[PHP]] | <BR>[[PHP]] | ||
[[Funktioner]] | [[Funktioner]] | ||
Versionen från 17 november 2024 kl. 12.42
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 Zip-arkivet.
- statusSys - Systemstatus för Zip-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.