compress.szip #
Description
szip reads and writes ZIP archives in V, using stored or DEFLATE-compressed entries. ZIP64, multi-disk and encrypted archives are unsupported and return an error. Reading validates entry bounds, uncompressed sizes and CRC-32 checksums. New entry names are written as UTF-8 with the ZIP language encoding flag. Appending preserves the encoding flag and raw name bytes of existing entries.
Opening an entry by name in read-only mode returns an error when that entry is missing. extract_zip_to_dir rejects paths outside the destination and paths containing symbolic links. Extraction and the zip_files and zip_folder helpers propagate filesystem errors. The entry API retains its non-failing close() signature; use the helpers when write errors must be reported.
fn extract_zip_to_dir #
fn extract_zip_to_dir(file string, dir string) !bool
extract_zip_to_dir extracts entries beneath dir and reports invalid paths or I/O failures.
fn open #
fn open(name string, level CompressionLevel, mode OpenMode) !&Zip
fn open_lazy #
fn open_lazy(name string) !&Zip
open_lazy opens a readable archive without decompressing and caching the payload of every entry. Listing entries (total/name/size/is_dir) only needs the central directory, so scanning large archives stays fast; an entry is decoded (and validated) on demand when it is actually read.
fn zip_files #
fn zip_files(path_to_file []string, path_to_export_zip string) !
zip_files writes the supplied files to a ZIP archive and reports write failures.
fn zip_folder #
fn zip_folder(folder string, zip_file string, opt ZipFolderOptions) !
zip_folder archives a directory, optionally omitting empty folders.
fn CompressionLevel.from #
fn CompressionLevel.from[W](input W) !CompressionLevel
from returns the CompressionLevel value of an integer or of a field name, or an error if there is none.
fn OpenMode.from #
fn OpenMode.from[W](input W) !OpenMode
from returns the OpenMode value of an integer or of a field name, or an error if there is none.
enum CompressionLevel #
enum CompressionLevel {
no_compression
best_speed
best_compression
uber_compression
default_level
default_compression
}
enum OpenMode #
enum OpenMode {
write
read_only
append
}
struct Zip #
struct Zip {
pub mut:
path string
mode OpenMode
level CompressionLevel
entries []ZipEntry
// write state
current int = -1
// cached file bytes for read
filedata []u8
mut:
directory_offset int
validated bool
}
fn (Zip) close #
fn (mut z Zip) close()
close flushes pending entries; use zip_files or zip_folder when write errors must be returned.
fn (Zip) total #
fn (mut z Zip) total() !int
fn (Zip) open_entry_by_index #
fn (mut z Zip) open_entry_by_index(index int) !
fn (Zip) open_entry #
fn (mut z Zip) open_entry(name string) !
open_entry finds a name case-insensitively when reading, or appends a new writable entry.
fn (Zip) close_entry #
fn (mut z Zip) close_entry()
fn (Zip) name #
fn (mut z Zip) name() string
fn (Zip) is_dir #
fn (mut z Zip) is_dir() !bool
fn (Zip) size #
fn (mut z Zip) size() u64
fn (Zip) crc32 #
fn (mut z Zip) crc32() u32
fn (Zip) index #
fn (mut z Zip) index() !int
fn (Zip) read_entry_buf #
fn (mut z Zip) read_entry_buf(buf voidptr, in_bsize int) !int
fn (Zip) read_entry_bytes #
fn (mut z Zip) read_entry_bytes() ![]u8
read_entry_bytes returns the fully validated, decompressed bytes of the current entry. Unlike read_entry_buf, the caller does not have to preallocate from the untrusted central-directory size: the entry is checked against its local header, decompressed and checksum-verified first, and the returned buffer is sized from the actual decoded length. The caller owns the returned array and must free it.
fn (Zip) read_entry #
fn (mut z Zip) read_entry() !voidptr
read_entry allocates the current entry's bytes; the caller must free the returned buffer.
fn (Zip) create_entry #
fn (mut z Zip) create_entry(name string) !
create_entry writes the file at name to the current archive entry.
fn (Zip) extract_entry #
fn (mut z Zip) extract_entry(path string) !
extract_entry writes the current archive entry to path.
fn (Zip) write_entry #
fn (mut z Zip) write_entry(data []u8) !
write_entry appends bytes to the currently open writable entry.
struct ZipEntry #
struct ZipEntry {
pub mut:
name string
method u16 // 0 = stored, 8 = deflate
flags u16
crc32 u32
size u64 // uncompressed
csize u64 // compressed
offset u64 // local header offset
is_dir bool
data []u8 // uncompressed data (for write) / cached (for read)
}
struct ZipFolderOptions #
struct ZipFolderOptions {
pub:
omit_empty_folders bool
}