szip: expose zip_folder (#14356)
parent
3afc7c4c6d
commit
0ec1c8d9f0
|
@ -563,7 +563,7 @@ pub fn walk(path string, f fn (string)) {
|
||||||
pub type FnWalkContextCB = fn (voidptr, string)
|
pub type FnWalkContextCB = fn (voidptr, string)
|
||||||
|
|
||||||
// walk_with_context traverses the given directory `path`.
|
// walk_with_context traverses the given directory `path`.
|
||||||
// For each encountred file, it will call your `fcb` callback,
|
// For each encountred file and directory, it will call your `fcb` callback,
|
||||||
// passing it the arbitrary `context` in its first parameter,
|
// passing it the arbitrary `context` in its first parameter,
|
||||||
// and the path to the file in its second parameter.
|
// and the path to the file in its second parameter.
|
||||||
pub fn walk_with_context(path string, context voidptr, fcb FnWalkContextCB) {
|
pub fn walk_with_context(path string, context voidptr, fcb FnWalkContextCB) {
|
||||||
|
@ -580,10 +580,9 @@ pub fn walk_with_context(path string, context voidptr, fcb FnWalkContextCB) {
|
||||||
}
|
}
|
||||||
for file in files {
|
for file in files {
|
||||||
p := path + local_path_separator + file
|
p := path + local_path_separator + file
|
||||||
|
fcb(context, p)
|
||||||
if is_dir(p) && !is_link(p) {
|
if is_dir(p) && !is_link(p) {
|
||||||
walk_with_context(p, context, fcb)
|
walk_with_context(p, context, fcb)
|
||||||
} else {
|
|
||||||
fcb(context, p)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,6 +5,11 @@ import os
|
||||||
#flag -I @VEXEROOT/thirdparty/zip
|
#flag -I @VEXEROOT/thirdparty/zip
|
||||||
#include "zip.c"
|
#include "zip.c"
|
||||||
|
|
||||||
|
[params]
|
||||||
|
pub struct ZipFolderOptions {
|
||||||
|
omit_empty_folders bool
|
||||||
|
}
|
||||||
|
|
||||||
struct C.zip_t {
|
struct C.zip_t {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,36 +262,48 @@ pub fn zip_files(path_to_file []string, path_to_export_zip string) ? {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// zip_folder zips all entries in `folder` *recursively* to the zip file at `zip_file`.
|
||||||
TODO add
|
// Empty folders will be included, unless specified otherwise in `opt`.
|
||||||
// zip all files in directory to zip file
|
pub fn zip_folder(folder string, zip_file string, opt ZipFolderOptions) ? {
|
||||||
pub fn zip_folder(path_to_dir string, path_to_export_zip string) {
|
// get list of files from directory
|
||||||
|
path := folder.trim_right(os.path_separator)
|
||||||
// get list files from directory
|
mut files := []string{}
|
||||||
files := os.ls(path_to_dir) or { panic(err) }
|
os.walk_with_context(path, &files, fn (mut files []string, file string) {
|
||||||
|
files << file
|
||||||
|
})
|
||||||
|
|
||||||
// open or create new zip
|
// open or create new zip
|
||||||
mut zip := szip.open(path_to_export_zip, .no_compression, .write) or { panic(err) }
|
mut zip := open(zip_file, .no_compression, .write) ?
|
||||||
|
// close zip
|
||||||
|
defer {
|
||||||
|
zip.close()
|
||||||
|
}
|
||||||
|
|
||||||
// add all files from the directory to the archive
|
// add all files from the directory to the archive
|
||||||
for file in files {
|
for file in files {
|
||||||
eprintln('Zipping $file to ${path_to_export_zip}...')
|
is_dir := os.is_dir(file)
|
||||||
println(path_to_dir + file)
|
if opt.omit_empty_folders && is_dir {
|
||||||
|
continue
|
||||||
// add file to zip
|
}
|
||||||
zip.open_entry(file) or { panic(err) }
|
// strip each zip entry for the path prefix - this way
|
||||||
file_as_byte := os.read_bytes(path_to_dir + '/'+ file) or { panic(err) }
|
// all files in the archive can be made relative.
|
||||||
zip.write_entry(file_as_byte) or { panic(err) }
|
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
|
||||||
|
zip.open_entry(zip_file_entry) ?
|
||||||
|
if !is_dir {
|
||||||
|
file_as_byte := os.read_bytes(file) ?
|
||||||
|
zip.write_entry(file_as_byte) ?
|
||||||
|
}
|
||||||
zip.close_entry()
|
zip.close_entry()
|
||||||
}
|
}
|
||||||
|
|
||||||
// close zip
|
|
||||||
zip.close()
|
|
||||||
|
|
||||||
eprintln('Successfully')
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// total returns the number of all entries (files and directories) in the zip archive.
|
// total returns the number of all entries (files and directories) in the zip archive.
|
||||||
pub fn (mut zentry Zip) total() ?int {
|
pub fn (mut zentry Zip) total() ?int {
|
||||||
|
|
|
@ -3,21 +3,33 @@ import os
|
||||||
|
|
||||||
const (
|
const (
|
||||||
test_out_zip = 'v_test_zip.zip'
|
test_out_zip = 'v_test_zip.zip'
|
||||||
|
test_dir_zip = 'v_test_dir_zip.zip'
|
||||||
test_path = 'zip files'
|
test_path = 'zip files'
|
||||||
test_path2 = '.zip folder'
|
test_path2 = '.zip folder'
|
||||||
|
test_path3 = 'test zip folder'
|
||||||
|
test_path3_1 = os.join_path(test_path3, '1', '1')
|
||||||
|
test_path3_2 = os.join_path(test_path3, '2', '1')
|
||||||
|
test_path3_3 = os.join_path(test_path3, '3', '1')
|
||||||
|
test_path3_4 = os.join_path(test_path3, '4', '1')
|
||||||
fname1 = 'file_1.txt'
|
fname1 = 'file_1.txt'
|
||||||
fpath1 = os.join_path(test_path, fname1)
|
fpath1 = os.join_path(test_path, fname1)
|
||||||
fname2 = 'file_2.txt'
|
fname2 = 'file_2.txt'
|
||||||
fpath2 = os.join_path(test_path, fname2)
|
fpath2 = os.join_path(test_path, fname2)
|
||||||
fname3 = '.New Text Document.txt'
|
fname3 = '.New Text Document.txt'
|
||||||
fpath3 = os.join_path(test_path2, fname3)
|
fpath3 = os.join_path(test_path2, fname3)
|
||||||
|
fname4 = 'file.txt'
|
||||||
|
fpath4 = os.join_path(test_path3_1, fname4)
|
||||||
|
fpath5 = os.join_path(test_path3_2, fname4)
|
||||||
|
fpath6 = os.join_path(test_path3_4, fname4)
|
||||||
)
|
)
|
||||||
|
|
||||||
fn cleanup() {
|
fn cleanup() {
|
||||||
os.chdir(os.temp_dir()) or {}
|
os.chdir(os.temp_dir()) or {}
|
||||||
os.rmdir_all(test_path) or {}
|
os.rmdir_all(test_path) or {}
|
||||||
os.rmdir_all(test_path2) or {}
|
os.rmdir_all(test_path2) or {}
|
||||||
|
os.rmdir_all(test_path3) or {}
|
||||||
os.rm(test_out_zip) or {}
|
os.rm(test_out_zip) or {}
|
||||||
|
os.rm(test_dir_zip) or {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testsuite_begin() ? {
|
fn testsuite_begin() ? {
|
||||||
|
@ -110,3 +122,53 @@ fn test_reading_zipping_files() ? {
|
||||||
}
|
}
|
||||||
zp.close()
|
zp.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_zip_folder() ? {
|
||||||
|
cleanup()
|
||||||
|
os.mkdir_all(test_path3_1) ?
|
||||||
|
os.mkdir_all(test_path3_2) ?
|
||||||
|
os.mkdir_all(test_path3_3) ?
|
||||||
|
os.mkdir_all(test_path3_4) ?
|
||||||
|
os.write_file(fpath4, '4') ?
|
||||||
|
os.write_file(fpath5, '5') ?
|
||||||
|
os.write_file(fpath6, '6') ?
|
||||||
|
|
||||||
|
szip.zip_folder(test_path3, test_dir_zip) ?
|
||||||
|
assert os.exists(test_dir_zip)
|
||||||
|
|
||||||
|
os.rmdir_all(test_path3) ?
|
||||||
|
os.mkdir_all(test_path3) ?
|
||||||
|
szip.extract_zip_to_dir(test_dir_zip, test_path3) ?
|
||||||
|
assert os.exists(test_path3_1)
|
||||||
|
assert os.exists(test_path3_2)
|
||||||
|
assert os.exists(test_path3_3) // This is the empty dir
|
||||||
|
assert os.exists(test_path3_4)
|
||||||
|
assert (os.read_file(fpath4) ?) == '4'
|
||||||
|
assert (os.read_file(fpath5) ?) == '5'
|
||||||
|
assert (os.read_file(fpath6) ?) == '6'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_zip_folder_omit_empty_directories() ? {
|
||||||
|
cleanup()
|
||||||
|
os.mkdir_all(test_path3_1) ?
|
||||||
|
os.mkdir_all(test_path3_2) ?
|
||||||
|
os.mkdir_all(test_path3_3) ?
|
||||||
|
os.mkdir_all(test_path3_4) ?
|
||||||
|
os.write_file(fpath4, '4') ?
|
||||||
|
os.write_file(fpath5, '5') ?
|
||||||
|
os.write_file(fpath6, '6') ?
|
||||||
|
|
||||||
|
szip.zip_folder(test_path3, test_dir_zip, omit_empty_folders: true) ?
|
||||||
|
assert os.exists(test_dir_zip)
|
||||||
|
|
||||||
|
os.rmdir_all(test_path3) ?
|
||||||
|
os.mkdir_all(test_path3) ?
|
||||||
|
szip.extract_zip_to_dir(test_dir_zip, test_path3) ?
|
||||||
|
assert os.exists(test_path3_1)
|
||||||
|
assert os.exists(test_path3_2)
|
||||||
|
assert !os.exists(test_path3_3) // This is the empty dir, should be omitted with `omit_empty_folders`
|
||||||
|
assert os.exists(test_path3_4)
|
||||||
|
assert (os.read_file(fpath4) ?) == '4'
|
||||||
|
assert (os.read_file(fpath5) ?) == '5'
|
||||||
|
assert (os.read_file(fpath6) ?) == '6'
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue