Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.zip

Read and write data in thezip archiveformat.
Standards:
The current implementation mostly conforms toISO/IEC 21320-1:2015,which means,
  • that files can only be stored uncompressed or using the deflate mechanism,
  • that encryption features are not used,
  • that digital signature features are not used,
  • that patched data features are not used, and
  • that archives may not span multiple volumes.
Additionally, archives are checked for malware attacks and rejected if detected.This includes
  • zip bombs which generate gigantic amounts of unpacked data
  • zip archives that contain overlapping records
  • chameleon zip archives which generate different unpacked data, depending on the implementation of the unpack algorithm
The current implementation makes use of the zlib compression library.

UsageThere are two main ways of usage: Extracting files from a zip archiveand storing files into a zip archive. These can be mixed though (e.g.read an archive, remove some files, add others and write the newarchive).

Examples:
Example for reading an existing zip archive:
import std.stdio : writeln, writefln;import std.file : read;import std.zip;void main(string[] args){// read a zip file into memoryautozip =new ZipArchive(read(args[1]));// iterate over all zip members    writefln("%-10s  %-8s  Name","Length","CRC-32");foreach (name, am;zip.directory)    {// print some data about each member        writefln("%10s  %08x  %s", am.expandedSize, am.crc32, name);assert(am.expandedData.length == 0);// decompress the archive memberzip.expand(am);assert(am.expandedData.length == am.expandedSize);    }}
Example for writing files into a zip archive:
import std.file : write;import std.string : representation;import std.zip;void main(){// Create an ArchiveMembers for each file.    ArchiveMember file1 =new ArchiveMember();    file1.name ="test1.txt";    file1.expandedData("Test data.\n".dup.representation);    file1.compressionMethod = CompressionMethod.none;// don't compress    ArchiveMember file2 =new ArchiveMember();    file2.name ="test2.txt";    file2.expandedData("More test data.\n".dup.representation);    file2.compressionMethod = CompressionMethod.deflate;// compress// Create an archive and add the member.    ZipArchivezip =new ZipArchive();// add ArchiveMemberszip.addMember(file1);zip.addMember(file2);// Build the archivevoid[] compressed_data =zip.build();// Write to a file    write("test.zip", compressed_data);}
License:
Boost License 1.0.
Authors:
Walter Bright

Sourcestd/zip.d

classZipException:object.Exception;
Thrown on error.
enumCompressionMethod: ushort;
Compression method used byArchiveMember.
none
No compression, just archiving.
deflate
Deflate algorithm. Use zlib library to compress.
classArchiveMember;
A single file or directory inside the archive.
stringname;
The name of the archive member; it is used to index the archive directory for the member. Each member must have a unique name. Do not change without removing member from the directory first.
ubyte[]extra;
The content of the extra data field for this member. See original documentation for a description of the general format of this data. May contain undocumented 3rd-party data.
stringcomment;
Comment associated with this member.
ushortflags;
Contains some information on how to extract this archive. See original documentation for details.
ushortinternalAttributes;
Internal attributes. Bit 1 is set, if the member is apparently in binary format and bit 2 is set, if each record is preceded by the length of the record.
pure nothrow @nogc @property @safe ushortextractVersion() const;
The zip file format version needed to extract this member.
Returns:
Format version needed to extract this member.
pure nothrow @nogc @property @safe uintcrc32() const;
Cyclic redundancy check (CRC) value.
Returns:
CRC32 value.
pure nothrow @nogc @property @safe uintcompressedSize() const;
Size of data of member in compressed form.
Returns:
Size of the compressed archive.
pure nothrow @nogc @property @safe uintexpandedSize() const;
Size of data of member in uncompressed form.
Returns:
Size of uncompressed archive.
pure nothrow @nogc @property @safe ubyte[]compressedData();
Data of member in compressed form.
Returns:
The file data in compressed form.
pure nothrow @nogc @property @safe ubyte[]expandedData();

@property @safe voidexpandedData(ubyte[]ed);
Get or set data of member in uncompressed form. When an existing archive is readZipArchive.expand needs to be called before this can be accessed.
Parameters:
ubyte[]edExpanded Data.
Returns:
The file data.
@property @safe voidfileAttributes(uintattr);

nothrow @nogc @property uintfileAttributes() const;
Get or set the OS specific file attributes for this archive member.
Parameters:
uintattrAttributes as obtained bystd.file.getAttributes orstd.file.DirEntry.attributes.
Returns:
The file attributes or 0 if the file attributes were encoded for an incompatible OS (Windows vs. POSIX).
pure nothrow @nogc @property @safe DosFileTimetime() const;

@property voidtime(SysTimetime);

pure nothrow @nogc @property @safe voidtime(DosFileTimetime);
Get or set the last modification time for this member.
Parameters:
SysTimetimeTime to set (will be saved as DosFileTime, which is less accurate).
Returns:
The last modification time in DosFileFormat.
pure nothrow @nogc @property @safe CompressionMethodcompressionMethod() const;

pure @property @safe voidcompressionMethod(CompressionMethodcm);
Get or set compression method used for this member.
Parameters:
CompressionMethodcmCompression method.
Returns:
Compression method.
pure nothrow @nogc @property @safe uintindex(uintvalue);

pure nothrow @nogc @property @safe uintindex() const;
The index of this archive member within the archive. Set this to a different value for reordering the members of an archive.
Parameters:
uintvalueIndex value to set.
Returns:
The index.
classZipArchive;
Object representing the entire archive. ZipArchives are collections of ArchiveMembers.
stringcomment;
The archive comment. Must be less than 65536 bytes in length.
pure nothrow @nogc @property @safe ubyte[]data();
Array representing the entire contents of the archive.
Returns:
Data of the entire contents of the archive.
pure nothrow @nogc @property @safe uinttotalEntries() const;
Number of ArchiveMembers in the directory.
Returns:
The number of files in this archive.
pure nothrow @nogc @property @safe boolisZip64() const;

pure nothrow @nogc @property @safe voidisZip64(boolvalue);
True when the archive is in Zip64 format. Set this to true to force building a Zip64 archive.
Parameters:
boolvalueTrue, when the archive is forced to be build in Zip64 format.
Returns:
True, when the archive is in Zip64 format.
pure nothrow @nogc @property @safe ArchiveMember[string]directory();
Associative array indexed by the name of each member of the archive.
All the members of the archive can be accessed with a foreach loop:

Example

ZipArchive archive =new ZipArchive(data);foreach (ArchiveMember am; archive.directory){    writefln("member name is '%s'", am.name);}

Returns:
Associative array with all archive members.
pure nothrow @nogc @safe this();
Constructor to use when creating a new archive.
@safe voidaddMember(ArchiveMemberde);
Add a member to the archive. The file is compressed on the fly.
Parameters:
ArchiveMemberdeMember to be added.
Throws:
ZipException when an unsupported compression method is used or when compression failed.
@safe voiddeleteMember(ArchiveMemberde);
Delete memberde from the archive. Uses the name of the member to detect which element to delete.
Parameters:
ArchiveMemberdeMember to be deleted.
pure @safe void[]build();
Construct the entire contents of the current members of the archive.
Fills in the properties data[], totalEntries, and directory[]. For each ArchiveMember, fills in properties crc32, compressedSize, compressedData[].
Returns:
Array representing the entire archive.
Throws:
ZipException when the archive could not be build.
this(void[]buffer);
Constructor to use when reading an existing archive.
Fills in the properties data[], totalEntries, comment[], and directory[]. For each ArchiveMember, fills in properties madeVersion, extractVersion, flags, compressionMethod, time, crc32, compressedSize, expandedSize, compressedData[], internalAttributes, externalAttributes, name[], extra[], comment[]. Use expand() to get the expanded data for each ArchiveMember.
Parameters:
void[]bufferThe entire contents of the archive.
Throws:
ZipException when the archive was invalid or when malware was detected.
ubyte[]expand(ArchiveMemberde);
Decompress the contents of a member.
Fills in properties extractVersion, flags, compressionMethod, time, crc32, compressedSize, expandedSize, expandedData[], name[], extra[].
Parameters:
ArchiveMemberdeMember to be decompressed.
Returns:
The expanded data.
Throws:
ZipException when the entry is invalid or the compression method is not supported.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 04:08:12 2026

[8]ページ先頭

©2009-2026 Movatter.jp