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