2019-07-15 19:35:34 +03:00
|
|
|
module szip
|
|
|
|
|
2021-02-22 21:02:45 +03:00
|
|
|
import os
|
|
|
|
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/zip
|
2019-07-15 19:35:34 +03:00
|
|
|
#include "zip.c"
|
2021-02-21 18:05:03 +03:00
|
|
|
|
2022-05-11 16:48:41 +03:00
|
|
|
[params]
|
|
|
|
pub struct ZipFolderOptions {
|
|
|
|
omit_empty_folders bool
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:39:09 +03:00
|
|
|
struct C.zip_t {
|
|
|
|
}
|
2020-09-06 16:04:27 +03:00
|
|
|
|
2020-09-25 13:02:32 +03:00
|
|
|
type Zip = C.zip_t
|
2020-09-06 16:04:27 +03:00
|
|
|
|
2022-05-09 07:45:36 +03:00
|
|
|
pub type Fn_on_extract_entry = fn (&&char, &&char) int
|
|
|
|
|
2021-04-05 10:02:47 +03:00
|
|
|
fn C.zip_open(&char, int, char) &Zip
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_close(&Zip)
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2022-04-15 18:25:45 +03:00
|
|
|
fn C.zip_entry_open(&Zip, &u8) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2022-05-09 07:45:36 +03:00
|
|
|
fn C.zip_entry_openbyindex(&Zip, usize) int
|
2021-07-25 21:29:51 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_entry_close(&Zip) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2022-04-15 18:25:45 +03:00
|
|
|
fn C.zip_entry_name(&Zip) &u8
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_entry_index(&Zip) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_entry_isdir(&Zip) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_entry_size(&Zip) u64
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2020-09-06 16:04:27 +03:00
|
|
|
fn C.zip_entry_crc32(&Zip) u32
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.zip_entry_write(&Zip, voidptr, usize) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2021-04-05 10:02:47 +03:00
|
|
|
fn C.zip_entry_fwrite(&Zip, &char) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.zip_entry_read(&Zip, &voidptr, &usize) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.zip_entry_noallocread(&Zip, voidptr, usize) int
|
2021-07-25 21:29:51 +03:00
|
|
|
|
2021-04-05 10:02:47 +03:00
|
|
|
fn C.zip_entry_fread(&Zip, &char) int
|
2020-10-15 00:39:09 +03:00
|
|
|
|
2022-05-09 07:45:36 +03:00
|
|
|
fn C.zip_entries_total(&Zip) int
|
|
|
|
|
|
|
|
fn C.zip_extract(&char, &char, Fn_on_extract_entry, voidptr) int
|
2019-07-15 19:35:34 +03:00
|
|
|
|
2022-05-09 07:45:36 +03:00
|
|
|
fn cb_zip_extract(filename &&char, arg &&char) int {
|
|
|
|
return 0
|
|
|
|
}
|
2021-04-02 02:59:01 +03:00
|
|
|
|
2021-02-22 21:02:45 +03:00
|
|
|
// CompressionLevel lists compression levels, see in "thirdparty/zip/miniz.h"
|
|
|
|
pub enum CompressionLevel {
|
|
|
|
no_compression = 0
|
|
|
|
best_speed = 1
|
|
|
|
best_compression = 9
|
|
|
|
uber_compression = 10
|
|
|
|
default_level = 6
|
2020-09-06 16:04:27 +03:00
|
|
|
default_compression = -1
|
2021-02-22 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenMode lists the opening modes
|
|
|
|
// .write: opens a file for reading/extracting (the file must exists).
|
|
|
|
// .read_only: creates an empty file for writing.
|
|
|
|
// .append: appends to an existing archive.
|
|
|
|
pub enum OpenMode {
|
|
|
|
write
|
|
|
|
read_only
|
|
|
|
append
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2022-04-15 18:25:45 +03:00
|
|
|
fn (om OpenMode) to_u8() u8 {
|
2021-02-22 21:02:45 +03:00
|
|
|
return match om {
|
|
|
|
.write {
|
|
|
|
`w`
|
|
|
|
}
|
|
|
|
.read_only {
|
|
|
|
`r`
|
|
|
|
}
|
|
|
|
.append {
|
|
|
|
`a`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open opens zip archive with compression level using the given mode.
|
2021-02-23 10:43:19 +03:00
|
|
|
// name: the name of the zip file to open.
|
|
|
|
// level: can be any value of the CompressionLevel enum.
|
|
|
|
// mode: can be any value of the OpenMode enum.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn open(name string, level CompressionLevel, mode OpenMode) !&Zip {
|
2020-09-06 16:04:27 +03:00
|
|
|
if name.len == 0 {
|
|
|
|
return error('szip: name of file empty')
|
|
|
|
}
|
2022-04-15 14:58:56 +03:00
|
|
|
p_zip := unsafe { &Zip(C.zip_open(&char(name.str), int(level), char(mode.to_u8()))) }
|
2020-09-06 16:04:27 +03:00
|
|
|
if isnil(p_zip) {
|
|
|
|
return error('szip: cannot open/create/append new zip archive')
|
|
|
|
}
|
|
|
|
return p_zip
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// close closes the zip archive, releases resources - always finalize.
|
2021-02-22 21:02:45 +03:00
|
|
|
[inline]
|
2020-09-06 16:04:27 +03:00
|
|
|
pub fn (mut z Zip) close() {
|
|
|
|
C.zip_close(z)
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// open_entry opens an entry by name in the zip archive.
|
|
|
|
// For zip archive opened in 'w' or 'a' mode the function will append
|
|
|
|
// a new entry. In readonly mode the function tries to locate the entry
|
|
|
|
// in global dictionary.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) open_entry(name string) ! {
|
2021-04-05 10:31:48 +03:00
|
|
|
res := C.zip_entry_open(zentry, &char(name.str))
|
2020-09-06 16:04:27 +03:00
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot open archive entry')
|
|
|
|
}
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:29:51 +03:00
|
|
|
// open_entry_by_index opens an entry by index in the archive.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut z Zip) open_entry_by_index(index int) ! {
|
2021-07-25 21:29:51 +03:00
|
|
|
res := C.zip_entry_openbyindex(z, index)
|
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot open archive entry at index $index')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// close_entry closes a zip entry, flushes buffer and releases resources.
|
2021-02-22 21:02:45 +03:00
|
|
|
[inline]
|
2020-09-06 16:04:27 +03:00
|
|
|
pub fn (mut zentry Zip) close_entry() {
|
|
|
|
C.zip_entry_close(zentry)
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// name returns a local name of the current zip entry.
|
|
|
|
// The main difference between user's entry name and local entry name
|
|
|
|
// is optional relative path.
|
|
|
|
// Following .ZIP File Format Specification - the path stored MUST not contain
|
|
|
|
// a drive or device letter, or a leading slash.
|
|
|
|
// All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
|
|
|
|
// for compatibility with Amiga and UNIX file systems etc.
|
2020-09-06 16:04:27 +03:00
|
|
|
pub fn (mut zentry Zip) name() string {
|
2022-04-15 14:58:56 +03:00
|
|
|
name := unsafe { &u8(C.zip_entry_name(zentry)) }
|
2020-09-06 16:04:27 +03:00
|
|
|
if name == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2021-02-22 21:02:45 +03:00
|
|
|
return unsafe { name.vstring() }
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-23 10:43:19 +03:00
|
|
|
// index returns an index of the current zip entry.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) index() !int {
|
2020-09-06 16:04:27 +03:00
|
|
|
index := int(C.zip_entry_index(zentry))
|
|
|
|
if index == -1 {
|
|
|
|
return error('szip: cannot get current index of zip entry')
|
|
|
|
}
|
|
|
|
return index // must be check for INVALID_VALUE
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// is_dir determines if the current zip entry is a directory entry.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) is_dir() !bool {
|
2020-09-06 16:04:27 +03:00
|
|
|
isdir := C.zip_entry_isdir(zentry)
|
|
|
|
if isdir < 0 {
|
|
|
|
return error('szip: cannot check entry type')
|
|
|
|
}
|
|
|
|
return isdir == 1
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// size returns an uncompressed size of the current zip entry.
|
2021-02-22 21:02:45 +03:00
|
|
|
[inline]
|
2020-09-06 16:04:27 +03:00
|
|
|
pub fn (mut zentry Zip) size() u64 {
|
|
|
|
return C.zip_entry_size(zentry)
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// crc32 returns CRC-32 checksum of the current zip entry.
|
2021-02-22 21:02:45 +03:00
|
|
|
[inline]
|
2020-09-06 16:04:27 +03:00
|
|
|
pub fn (mut zentry Zip) crc32() u32 {
|
|
|
|
return C.zip_entry_crc32(zentry)
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// write_entry compresses an input buffer for the current zip entry.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) write_entry(data []u8) ! {
|
2022-04-12 19:37:30 +03:00
|
|
|
if int(data[0] & 0xff) == -1 {
|
2020-09-06 16:04:27 +03:00
|
|
|
return error('szip: cannot write entry')
|
|
|
|
}
|
2020-12-20 19:28:40 +03:00
|
|
|
res := C.zip_entry_write(zentry, data.data, data.len)
|
2020-09-06 16:04:27 +03:00
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to write entry')
|
|
|
|
}
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// create_entry compresses a file for the current zip entry.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) create_entry(name string) ! {
|
2021-04-05 10:31:48 +03:00
|
|
|
res := C.zip_entry_fwrite(zentry, &char(name.str))
|
2020-09-06 16:04:27 +03:00
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to create entry')
|
|
|
|
}
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// read_entry extracts the current zip entry into output buffer.
|
|
|
|
// The function allocates sufficient memory for an output buffer.
|
|
|
|
// NOTE: remember to release the memory allocated for an output buffer.
|
|
|
|
// for large entries, please take a look at zip_entry_extract function.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) read_entry() !voidptr {
|
2022-04-15 14:58:56 +03:00
|
|
|
mut buf := &u8(0)
|
2021-09-08 13:09:32 +03:00
|
|
|
mut bsize := usize(0)
|
2021-04-05 10:31:48 +03:00
|
|
|
res := C.zip_entry_read(zentry, unsafe { &voidptr(&buf) }, &bsize)
|
2020-09-06 16:04:27 +03:00
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot read properly data from entry')
|
|
|
|
}
|
|
|
|
return buf
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:29:51 +03:00
|
|
|
// read_entry_buf extracts the current zip entry into user specified buffer
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) read_entry_buf(buf voidptr, in_bsize int) !int {
|
2021-09-08 13:09:32 +03:00
|
|
|
bsize := usize(in_bsize)
|
2021-07-25 21:29:51 +03:00
|
|
|
res := C.zip_entry_noallocread(zentry, buf, bsize)
|
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot read properly data from entry')
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// extract_entry extracts the current zip entry into output file.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) extract_entry(path string) ! {
|
2021-04-05 10:31:48 +03:00
|
|
|
res := C.zip_entry_fread(zentry, &char(path.str))
|
2020-09-06 16:04:27 +03:00
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to extract entry')
|
|
|
|
}
|
2019-07-15 19:35:34 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 02:59:01 +03:00
|
|
|
// extract zip file to directory
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn extract_zip_to_dir(file string, dir string) !bool {
|
2021-04-05 10:31:48 +03:00
|
|
|
if C.access(&char(dir.str), 0) == -1 {
|
2021-04-02 02:59:01 +03:00
|
|
|
return error('szip: cannot open directory for extracting, directory not exists')
|
2021-02-22 15:59:36 +03:00
|
|
|
}
|
2022-05-09 07:45:36 +03:00
|
|
|
res := C.zip_extract(&char(file.str), &char(dir.str), cb_zip_extract, 0)
|
2021-02-22 15:59:36 +03:00
|
|
|
return res == 0
|
2020-09-06 16:04:27 +03:00
|
|
|
}
|
2021-04-02 02:59:01 +03:00
|
|
|
|
|
|
|
// zip files (full path) to zip file
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn zip_files(path_to_file []string, path_to_export_zip string) ! {
|
2021-04-02 02:59:01 +03:00
|
|
|
// open or create new zip
|
2021-04-02 08:26:14 +03:00
|
|
|
mut zip := open(path_to_export_zip, .no_compression, .write) or { panic(err) }
|
2021-04-02 02:59:01 +03:00
|
|
|
|
|
|
|
// add all files from the directory to the archive
|
|
|
|
for file in path_to_file {
|
|
|
|
// add file to zip
|
|
|
|
zip.open_entry(os.base(file)) or { panic(err) }
|
|
|
|
file_as_byte := os.read_bytes(file) or { panic(err) }
|
2021-04-02 08:26:14 +03:00
|
|
|
zip.write_entry(file_as_byte) or { panic(err) }
|
2021-04-02 02:59:01 +03:00
|
|
|
|
2021-04-02 08:26:14 +03:00
|
|
|
zip.close_entry()
|
2021-04-02 02:59:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// close zip
|
|
|
|
defer {
|
|
|
|
zip.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:48:41 +03:00
|
|
|
// zip_folder zips all entries in `folder` *recursively* to the zip file at `zip_file`.
|
|
|
|
// Empty folders will be included, unless specified otherwise in `opt`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn zip_folder(folder string, zip_file string, opt ZipFolderOptions) ! {
|
2022-05-11 16:48:41 +03:00
|
|
|
// get list of files from directory
|
|
|
|
path := folder.trim_right(os.path_separator)
|
|
|
|
mut files := []string{}
|
|
|
|
os.walk_with_context(path, &files, fn (mut files []string, file string) {
|
|
|
|
files << file
|
|
|
|
})
|
2021-04-02 02:59:01 +03:00
|
|
|
|
|
|
|
// open or create new zip
|
2022-10-16 09:28:57 +03:00
|
|
|
mut zip := open(zip_file, .no_compression, .write)!
|
2022-05-11 16:48:41 +03:00
|
|
|
// close zip
|
|
|
|
defer {
|
|
|
|
zip.close()
|
|
|
|
}
|
2021-04-02 02:59:01 +03:00
|
|
|
|
|
|
|
// add all files from the directory to the archive
|
|
|
|
for file in files {
|
2022-05-11 16:48:41 +03:00
|
|
|
is_dir := os.is_dir(file)
|
|
|
|
if opt.omit_empty_folders && is_dir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// strip each zip entry for the path prefix - this way
|
|
|
|
// all files in the archive can be made relative.
|
|
|
|
mut zip_file_entry := file.trim_string_left(path + os.path_separator)
|
|
|
|
// Normalize path on Windows \ -> /
|
|
|
|
$if windows {
|
|
|
|
zip_file_entry = zip_file_entry.replace(os.path_separator, '/')
|
|
|
|
}
|
|
|
|
if is_dir {
|
|
|
|
zip_file_entry += '/' // Tells the implementation that the entry is a directory
|
|
|
|
}
|
|
|
|
// add file or directory (ends with "/") to zip
|
2022-10-16 09:28:57 +03:00
|
|
|
zip.open_entry(zip_file_entry)!
|
2022-05-11 16:48:41 +03:00
|
|
|
if !is_dir {
|
2022-10-16 09:28:57 +03:00
|
|
|
file_as_byte := os.read_bytes(file)!
|
|
|
|
zip.write_entry(file_as_byte)!
|
2022-05-11 16:48:41 +03:00
|
|
|
}
|
|
|
|
zip.close_entry()
|
2021-04-02 02:59:01 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 00:04:23 +03:00
|
|
|
|
2021-02-22 15:59:36 +03:00
|
|
|
// total returns the number of all entries (files and directories) in the zip archive.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut zentry Zip) total() !int {
|
2022-05-09 07:45:36 +03:00
|
|
|
tentry := int(C.zip_entries_total(zentry))
|
2020-09-06 16:04:27 +03:00
|
|
|
if tentry == -1 {
|
|
|
|
return error('szip: cannot count total entries')
|
|
|
|
}
|
|
|
|
return tentry
|
2020-05-17 14:51:18 +03:00
|
|
|
}
|