| 1 | import compress.szip |
| 2 | import os |
| 3 | |
| 4 | const test_out_zip = 'v_test_zip.zip' |
| 5 | const test_dir_zip = 'v_test_dir_zip.zip' |
| 6 | const test_path = 'zip files' |
| 7 | const test_path2 = '.zip folder' |
| 8 | const test_path3 = 'test zip folder' |
| 9 | const test_path3_1 = os.join_path(test_path3, '1', '1') |
| 10 | const test_path3_2 = os.join_path(test_path3, '2', '1') |
| 11 | const test_path3_3 = os.join_path(test_path3, '3', '1') |
| 12 | const test_path3_4 = os.join_path(test_path3, '4', '1') |
| 13 | const fname1 = 'file_1.txt' |
| 14 | const fpath1 = os.join_path(test_path, fname1) |
| 15 | const fname2 = 'file_2.txt' |
| 16 | const fpath2 = os.join_path(test_path, fname2) |
| 17 | const fname3 = '.New Text Document.txt' |
| 18 | const fpath3 = os.join_path(test_path2, fname3) |
| 19 | const fname4 = 'file.txt' |
| 20 | const fpath4 = os.join_path(test_path3_1, fname4) |
| 21 | const fpath5 = os.join_path(test_path3_2, fname4) |
| 22 | const fpath6 = os.join_path(test_path3_4, fname4) |
| 23 | |
| 24 | fn cleanup() { |
| 25 | os.chdir(os.temp_dir()) or {} |
| 26 | os.rmdir_all(test_path) or {} |
| 27 | os.rmdir_all(test_path2) or {} |
| 28 | os.rmdir_all(test_path3) or {} |
| 29 | os.rm(test_out_zip) or {} |
| 30 | os.rm(test_dir_zip) or {} |
| 31 | } |
| 32 | |
| 33 | fn testsuite_begin() { |
| 34 | cleanup() |
| 35 | } |
| 36 | |
| 37 | fn testsuite_end() { |
| 38 | cleanup() |
| 39 | } |
| 40 | |
| 41 | fn test_szip_create_temp_files() { |
| 42 | os.mkdir(test_path)! |
| 43 | os.mkdir(test_path2)! |
| 44 | os.write_file(fpath1, 'file one')! |
| 45 | os.write_file(fpath2, 'file two')! |
| 46 | os.write_file(fpath3, 'file three')! |
| 47 | assert os.exists(fpath1) |
| 48 | assert os.exists(fpath2) |
| 49 | assert os.exists(fpath3) |
| 50 | } |
| 51 | |
| 52 | fn test_zipping_files() { |
| 53 | mut files := (os.ls(test_path)!).map(os.join_path(test_path, it)) |
| 54 | files << (os.ls(test_path2)!).map(os.join_path(test_path2, it)) |
| 55 | szip.zip_files(files, test_out_zip)! |
| 56 | assert os.exists(test_out_zip) |
| 57 | os.rm(fpath1)! |
| 58 | os.rm(fpath2)! |
| 59 | os.rm(fpath3)! |
| 60 | } |
| 61 | |
| 62 | fn test_extract_zipped_files() { |
| 63 | szip.extract_zip_to_dir(test_out_zip, test_path)! |
| 64 | szip.extract_zip_to_dir(test_out_zip, test_path2)! |
| 65 | assert os.exists(fpath1) |
| 66 | assert os.exists(fpath2) |
| 67 | assert os.exists(fpath3) |
| 68 | assert (os.read_file(fpath1)!) == 'file one' |
| 69 | assert (os.read_file(fpath2)!) == 'file two' |
| 70 | assert (os.read_file(fpath3)!) == 'file three' |
| 71 | cleanup() |
| 72 | } |
| 73 | |
| 74 | fn test_reading_zipping_files() { |
| 75 | n_files := 2 |
| 76 | mut file_name_list := []string{} |
| 77 | for i in 0 .. n_files { |
| 78 | file_name_list << 'file_${i:02}.txt' |
| 79 | } |
| 80 | |
| 81 | cleanup() |
| 82 | os.mkdir(test_path)! |
| 83 | os.mkdir(test_path2)! |
| 84 | os.write_file(fpath3, 'file three')! |
| 85 | for c, f_name in file_name_list { |
| 86 | tmp_path := os.join_path(test_path, f_name) |
| 87 | os.write_file(tmp_path, 'file ${c:02}')! |
| 88 | assert os.exists(tmp_path) |
| 89 | } |
| 90 | files := (os.ls(test_path)!).map(os.join_path(test_path, it)) |
| 91 | |
| 92 | szip.zip_files(files, test_out_zip)! |
| 93 | assert os.exists(test_out_zip) |
| 94 | |
| 95 | mut zp := |
| 96 | szip.open(test_out_zip, szip.CompressionLevel.no_compression, szip.OpenMode.read_only)! |
| 97 | n_entries := zp.total()! |
| 98 | assert n_entries == n_files |
| 99 | |
| 100 | unsafe { |
| 101 | data_len := 'file XX'.len |
| 102 | buf_size := 32 |
| 103 | buf := malloc(data_len * 2) |
| 104 | |
| 105 | for _ in 0 .. n_files { |
| 106 | zp.open_entry_by_index(0)! |
| 107 | name := zp.name() |
| 108 | assert name in file_name_list |
| 109 | |
| 110 | zp.read_entry_buf(buf, buf_size)! |
| 111 | buf[data_len] = 0 |
| 112 | tmp_str := tos(buf, data_len) |
| 113 | |
| 114 | assert tmp_str[0..4] == 'file' |
| 115 | assert tmp_str[5..7] == name[5..7] |
| 116 | |
| 117 | zp.close_entry() |
| 118 | } |
| 119 | |
| 120 | free(buf) |
| 121 | } |
| 122 | zp.close() |
| 123 | } |
| 124 | |
| 125 | fn test_zip_folder() { |
| 126 | cleanup() |
| 127 | os.mkdir_all(test_path3_1)! |
| 128 | os.mkdir_all(test_path3_2)! |
| 129 | os.mkdir_all(test_path3_3)! |
| 130 | os.mkdir_all(test_path3_4)! |
| 131 | os.write_file(fpath4, '4')! |
| 132 | os.write_file(fpath5, '5')! |
| 133 | os.write_file(fpath6, '6')! |
| 134 | |
| 135 | szip.zip_folder(test_path3, test_dir_zip)! |
| 136 | assert os.exists(test_dir_zip) |
| 137 | |
| 138 | os.rmdir_all(test_path3)! |
| 139 | os.mkdir_all(test_path3)! |
| 140 | szip.extract_zip_to_dir(test_dir_zip, test_path3)! |
| 141 | assert os.exists(test_path3_1) |
| 142 | assert os.exists(test_path3_2) |
| 143 | assert os.exists(test_path3_3) // This is the empty dir |
| 144 | assert os.exists(test_path3_4) |
| 145 | assert (os.read_file(fpath4)!) == '4' |
| 146 | assert (os.read_file(fpath5)!) == '5' |
| 147 | assert (os.read_file(fpath6)!) == '6' |
| 148 | } |
| 149 | |
| 150 | fn test_zip_folder_omit_empty_directories() { |
| 151 | cleanup() |
| 152 | os.mkdir_all(test_path3_1)! |
| 153 | os.mkdir_all(test_path3_2)! |
| 154 | os.mkdir_all(test_path3_3)! |
| 155 | os.mkdir_all(test_path3_4)! |
| 156 | os.write_file(fpath4, '4')! |
| 157 | os.write_file(fpath5, '5')! |
| 158 | os.write_file(fpath6, '6')! |
| 159 | |
| 160 | szip.zip_folder(test_path3, test_dir_zip, omit_empty_folders: true)! |
| 161 | assert os.exists(test_dir_zip) |
| 162 | |
| 163 | os.rmdir_all(test_path3)! |
| 164 | os.mkdir_all(test_path3)! |
| 165 | szip.extract_zip_to_dir(test_dir_zip, test_path3)! |
| 166 | assert os.exists(test_path3_1) |
| 167 | assert os.exists(test_path3_2) |
| 168 | assert !os.exists(test_path3_3) // This is the empty dir, should be omitted with `omit_empty_folders` |
| 169 | assert os.exists(test_path3_4) |
| 170 | assert (os.read_file(fpath4)!) == '4' |
| 171 | assert (os.read_file(fpath5)!) == '5' |
| 172 | assert (os.read_file(fpath6)!) == '6' |
| 173 | } |
| 174 | |
| 175 | fn test_zip_folder_empty_file() { |
| 176 | cleanup() |
| 177 | os.mkdir_all(test_path)! |
| 178 | os.write_file('${test_path}/test.txt', '')! // Empty file |
| 179 | szip.zip_folder(test_path, test_dir_zip)! |
| 180 | assert os.exists(test_dir_zip) |
| 181 | } |
| 182 | |