1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

szip: fix extracting dot folders (#13387)

This commit is contained in:
Dialga
2022-05-09 16:45:36 +12:00
committed by GitHub
parent 8519996201
commit 4400f9891e
5 changed files with 8484 additions and 4332 deletions

View File

@ -4,20 +4,21 @@ import os
#flag -I @VEXEROOT/thirdparty/zip
#include "zip.c"
#include "zip.h"
struct C.zip_t {
}
type Zip = C.zip_t
pub type Fn_on_extract_entry = fn (&&char, &&char) int
fn C.zip_open(&char, int, char) &Zip
fn C.zip_close(&Zip)
fn C.zip_entry_open(&Zip, &u8) int
fn C.zip_entry_openbyindex(&Zip, int) int
fn C.zip_entry_openbyindex(&Zip, usize) int
fn C.zip_entry_close(&Zip) int
@ -41,9 +42,13 @@ fn C.zip_entry_noallocread(&Zip, voidptr, usize) int
fn C.zip_entry_fread(&Zip, &char) int
fn C.zip_total_entries(&Zip) int
fn C.zip_entries_total(&Zip) int
fn C.zip_extract_without_callback(&char, &char) int
fn C.zip_extract(&char, &char, Fn_on_extract_entry, voidptr) int
fn cb_zip_extract(filename &&char, arg &&char) int {
return 0
}
// CompressionLevel lists compression levels, see in "thirdparty/zip/miniz.h"
pub enum CompressionLevel {
@ -216,9 +221,6 @@ pub fn (mut zentry Zip) read_entry_buf(buf voidptr, in_bsize int) ?int {
// extract_entry extracts the current zip entry into output file.
pub fn (mut zentry Zip) extract_entry(path string) ? {
if !os.is_file(path) {
return error('szip: cannot open file for extracting, "$path" not exists')
}
res := C.zip_entry_fread(zentry, &char(path.str))
if res != 0 {
return error('szip: failed to extract entry')
@ -230,7 +232,7 @@ pub fn extract_zip_to_dir(file string, dir string) ?bool {
if C.access(&char(dir.str), 0) == -1 {
return error('szip: cannot open directory for extracting, directory not exists')
}
res := C.zip_extract_without_callback(&char(file.str), &char(dir.str))
res := C.zip_extract(&char(file.str), &char(dir.str), cb_zip_extract, 0)
return res == 0
}
@ -288,7 +290,7 @@ pub fn zip_folder(path_to_dir string, path_to_export_zip string) {
// total returns the number of all entries (files and directories) in the zip archive.
pub fn (mut zentry Zip) total() ?int {
tentry := int(C.zip_total_entries(zentry))
tentry := int(C.zip_entries_total(zentry))
if tentry == -1 {
return error('szip: cannot count total entries')
}

View File

@ -4,15 +4,19 @@ import os
const (
test_out_zip = 'v_test_zip.zip'
test_path = 'zip files'
test_path2 = '.zip folder'
fname1 = 'file_1.txt'
fpath1 = os.join_path(test_path, fname1)
fname2 = 'file_2.txt'
fpath2 = os.join_path(test_path, fname2)
fname3 = '.New Text Document.txt'
fpath3 = os.join_path(test_path2, fname3)
)
fn cleanup() {
os.chdir(os.temp_dir()) or {}
os.rmdir_all(test_path) or {}
os.rmdir_all(test_path2) or {}
os.rm(test_out_zip) or {}
}
@ -26,28 +30,35 @@ fn testsuite_end() ? {
fn test_szip_create_temp_files() ? {
os.mkdir(test_path) ?
os.mkdir(test_path2) ?
os.write_file(fpath1, 'file one') ?
os.write_file(fpath2, 'file two') ?
os.write_file(fpath3, 'file three') ?
assert os.exists(fpath1)
assert os.exists(fpath2)
assert os.exists(fpath3)
}
fn test_zipping_files() ? {
files := (os.ls(test_path) ?).map(os.join_path(test_path, it))
mut files := (os.ls(test_path) ?).map(os.join_path(test_path, it))
files << (os.ls(test_path2) ?).map(os.join_path(test_path2, it))
szip.zip_files(files, test_out_zip) ?
assert os.exists(test_out_zip)
os.rm(fpath1) ?
os.rm(fpath2) ?
os.rm(fpath3) ?
}
fn test_extract_zipped_files() ? {
os.rm(fpath1) ?
os.rm(fpath2) ?
szip.extract_zip_to_dir(test_out_zip, test_path) ?
szip.extract_zip_to_dir(test_out_zip, test_path2) ?
assert os.exists(fpath1)
assert os.exists(fpath2)
assert os.exists(fpath3)
assert (os.read_file(fpath1) ?) == 'file one'
assert (os.read_file(fpath2) ?) == 'file two'
os.rmdir_all(test_path) ?
os.rm(test_out_zip) or {}
assert (os.read_file(fpath3) ?) == 'file three'
cleanup()
}
fn test_reading_zipping_files() ? {
@ -57,9 +68,10 @@ fn test_reading_zipping_files() ? {
file_name_list << 'file_${i:02}.txt'
}
os.chdir(os.temp_dir()) or {}
os.rmdir_all(test_path) or {}
cleanup()
os.mkdir(test_path) ?
os.mkdir(test_path2) ?
os.write_file(fpath3, 'file three') ?
for c, f_name in file_name_list {
tmp_path := os.join_path(test_path, f_name)
os.write_file(tmp_path, 'file ${c:02}') ?