2019-07-15 18:35:34 +02:00
|
|
|
module szip
|
|
|
|
|
2021-02-22 19:02:45 +01:00
|
|
|
import os
|
|
|
|
|
2019-07-15 19:30:53 +02:00
|
|
|
#flag -I @VROOT/thirdparty/zip
|
2019-07-15 18:35:34 +02:00
|
|
|
#include "zip.c"
|
|
|
|
#include "zip.h"
|
2021-02-21 16:05:03 +01:00
|
|
|
|
2020-10-14 23:39:09 +02:00
|
|
|
struct C.zip_t {
|
|
|
|
}
|
2020-09-06 15:04:27 +02:00
|
|
|
|
2020-09-25 12:02:32 +02:00
|
|
|
type Zip = C.zip_t
|
2020-09-06 15:04:27 +02:00
|
|
|
|
|
|
|
fn C.zip_open(byteptr, int, byte) &Zip
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_close(&Zip)
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_open(&Zip, byteptr) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_close(&Zip) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_name(&Zip) byteptr
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_index(&Zip) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_isdir(&Zip) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_size(&Zip) u64
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_crc32(&Zip) u32
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_write(&Zip, voidptr, int) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_fwrite(&Zip, byteptr) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_read(&Zip, byteptr, int) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_entry_fread(&Zip, byteptr) int
|
2020-10-14 23:39:09 +02:00
|
|
|
|
2020-09-06 15:04:27 +02:00
|
|
|
fn C.zip_total_entries(&Zip) int
|
2019-07-15 18:35:34 +02:00
|
|
|
|
2021-02-22 19:02:45 +01: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 15:04:27 +02:00
|
|
|
default_compression = -1
|
2021-02-22 19:02:45 +01: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]
|
|
|
|
fn (om OpenMode) to_byte() byte {
|
|
|
|
return match om {
|
|
|
|
.write {
|
|
|
|
`w`
|
|
|
|
}
|
|
|
|
.read_only {
|
|
|
|
`r`
|
|
|
|
}
|
|
|
|
.append {
|
|
|
|
`a`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open opens zip archive with compression level using the given mode.
|
|
|
|
// 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
|
|
|
|
pub fn open(name string, level CompressionLevel, mode OpenMode) ?&Zip {
|
2020-09-06 15:04:27 +02:00
|
|
|
if name.len == 0 {
|
|
|
|
return error('szip: name of file empty')
|
|
|
|
}
|
2021-02-22 19:02:45 +01:00
|
|
|
p_zip := &Zip(C.zip_open(name.str, int(level), mode.to_byte()))
|
2020-09-06 15:04:27 +02:00
|
|
|
if isnil(p_zip) {
|
|
|
|
return error('szip: cannot open/create/append new zip archive')
|
|
|
|
}
|
|
|
|
return p_zip
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// close closes the zip archive, releases resources - always finalize.
|
2021-02-22 19:02:45 +01:00
|
|
|
[inline]
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut z Zip) close() {
|
|
|
|
C.zip_close(z)
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01: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.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) open_entry(name string) ? {
|
|
|
|
res := C.zip_entry_open(zentry, name.str)
|
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot open archive entry')
|
|
|
|
}
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// close_entry closes a zip entry, flushes buffer and releases resources.
|
2021-02-22 19:02:45 +01:00
|
|
|
[inline]
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) close_entry() {
|
|
|
|
C.zip_entry_close(zentry)
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01: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 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) name() string {
|
|
|
|
name := C.zip_entry_name(zentry)
|
|
|
|
if name == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2021-02-22 19:02:45 +01:00
|
|
|
return unsafe { name.vstring() }
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
/// index returns an index of the current zip entry.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) index() ?int {
|
|
|
|
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 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// is_dir determines if the current zip entry is a directory entry.
|
|
|
|
pub fn (mut zentry Zip) is_dir() ?bool {
|
2020-09-06 15:04:27 +02:00
|
|
|
isdir := C.zip_entry_isdir(zentry)
|
|
|
|
if isdir < 0 {
|
|
|
|
return error('szip: cannot check entry type')
|
|
|
|
}
|
|
|
|
return isdir == 1
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// size returns an uncompressed size of the current zip entry.
|
2021-02-22 19:02:45 +01:00
|
|
|
[inline]
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) size() u64 {
|
|
|
|
return C.zip_entry_size(zentry)
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// crc32 returns CRC-32 checksum of the current zip entry.
|
2021-02-22 19:02:45 +01:00
|
|
|
[inline]
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) crc32() u32 {
|
|
|
|
return C.zip_entry_crc32(zentry)
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// write_entry compresses an input buffer for the current zip entry.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) write_entry(data []byte) ? {
|
|
|
|
if (data[0] & 0xff) == -1 {
|
|
|
|
return error('szip: cannot write entry')
|
|
|
|
}
|
2020-12-20 17:28:40 +01:00
|
|
|
res := C.zip_entry_write(zentry, data.data, data.len)
|
2020-09-06 15:04:27 +02:00
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to write entry')
|
|
|
|
}
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// create_entry compresses a file for the current zip entry.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) create_entry(name string) ? {
|
|
|
|
res := C.zip_entry_fwrite(zentry, name.str)
|
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to create entry')
|
|
|
|
}
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01: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.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) read_entry() ?voidptr {
|
|
|
|
mut buf := voidptr(0)
|
|
|
|
mut bsize := i64(0)
|
|
|
|
res := C.zip_entry_read(zentry, &buf, &bsize)
|
|
|
|
if res == -1 {
|
|
|
|
return error('szip: cannot read properly data from entry')
|
|
|
|
}
|
|
|
|
return buf
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// extract_entry extracts the current zip entry into output file.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) extract_entry(path string) ? {
|
2021-02-22 19:02:45 +01:00
|
|
|
if !os.is_file(path) {
|
|
|
|
return error('szip: cannot open file for extracting, "$path" not exists')
|
2020-09-06 15:04:27 +02:00
|
|
|
}
|
|
|
|
res := C.zip_entry_fread(zentry, path.str)
|
|
|
|
if res != 0 {
|
|
|
|
return error('szip: failed to extract entry')
|
|
|
|
}
|
2019-07-15 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
/*
|
|
|
|
extract extracts the current zip entry using a callback function (on_extract).
|
2020-09-06 15:04:27 +02:00
|
|
|
fn (mut zentry Zip) extract(path string) bool {
|
2021-02-22 13:59:36 +01:00
|
|
|
if C.access(path.str, 0) == -1 {
|
|
|
|
return false
|
2021-02-22 19:02:45 +01:00
|
|
|
// return error('szip: cannot open directory for extracting, directory not exists')
|
2021-02-22 13:59:36 +01:00
|
|
|
}
|
|
|
|
res := C.zip_extract(zentry, path.str, 0, 0)
|
|
|
|
return res == 0
|
2020-09-06 15:04:27 +02:00
|
|
|
}
|
|
|
|
*/
|
2020-06-07 23:04:23 +02:00
|
|
|
|
2021-02-22 13:59:36 +01:00
|
|
|
// total returns the number of all entries (files and directories) in the zip archive.
|
2020-09-06 15:04:27 +02:00
|
|
|
pub fn (mut zentry Zip) total() ?int {
|
|
|
|
tentry := int(C.zip_total_entries(zentry))
|
|
|
|
if tentry == -1 {
|
|
|
|
return error('szip: cannot count total entries')
|
|
|
|
}
|
|
|
|
return tentry
|
2020-05-17 13:51:18 +02:00
|
|
|
}
|