Read and write data in the zip archive format.
import std.stdio : writeln, writefln;
import std.file : read;
import std.zip;
void main(string[] args)
{
// read a zip file into memory
auto zip = 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 member
zip.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.
ZipArchive zip = new ZipArchive();
// add ArchiveMembers
zip.addMember(file1);
zip.addMember(file2);
// Build the archive
void[] compressed_data = zip.build();
// Write to a file
write("test.zip", compressed_data);
}
Thrown on error.
Compression method used by ArchiveMember.
No compression, just archiving.
Deflate algorithm. Use zlib library to compress.
A single file or directory inside the archive.
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.
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.
Comment associated with this member.
Contains some information on how to extract this archive. See original documentation for details.
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.
The zip file format version needed to extract this member.
Cyclic redundancy check (CRC) value.
Size of data of member in compressed form.
Size of data of member in uncompressed form.
Should be 0.
Data of member in compressed form.
Get or set data of member in uncompressed form. When an existing archive is read ZipArchive.expand needs to be called before this can be accessed.
ubyte[] ed
| Expanded Data. |
Get or set the OS specific file attributes for this archive member.
uint attr
| Attributes as obtained by std.file.getAttributes or std.file.DirEntry.attributes. |
Get or set the last modification time for this member.
SysTime time
| Time to set (will be saved as DosFileTime, which is less accurate). |
Get or set compression method used for this member.
CompressionMethod cm
| Compression method. |
CompressionMethodThe index of this archive member within the archive. Set this to a different value for reordering the members of an archive.
uint value
| Index value to set. |
Object representing the entire archive. ZipArchives are collections of ArchiveMembers.
The archive comment. Must be less than 65536 bytes in length.
Array representing the entire contents of the archive.
0 since multi-disk zip archives are not supported.
0 since multi-disk zip archives are not supported.
Number of ArchiveMembers in the directory.
True when the archive is in Zip64 format. Set this to true to force building a Zip64 archive.
bool value
| True, when the archive is forced to be build in Zip64 format. |
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:
ZipArchive archive = new ZipArchive(data);
foreach (ArchiveMember am; archive.directory)
{
writefln("member name is '%s'", am.name);
}
Constructor to use when creating a new archive.
Add a member to the archive. The file is compressed on the fly.
ArchiveMember de
| Member to be added. |
Delete member de from the archive. Uses the name of the member to detect which element to delete.
ArchiveMember de
| Member to be deleted. |
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[].
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.
void[] buffer
| The entire contents of the archive. |
Decompress the contents of a member.
Fills in properties extractVersion, flags, compressionMethod, time, crc32, compressedSize, expandedSize, expandedData[], name[], extra[].
ArchiveMember de
| Member to be decompressed. |
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_zip.html