1
1
#include < hgl/log/#fo.h>
2
2
#include < hgl/CodePage.h>
3
3
#include < hgl/filesystem/FileSystem.h>
4
- #include < hgl/io/FileInputStream .h>
4
+ #include < hgl/io/FileOutputStream .h>
5
5
#include < hgl/type/MemBlock.h>
6
6
#include < stdio.h>
7
7
#include < zip.h>
8
8
9
9
using namespace hgl ;
10
10
using namespace hgl ::io;
11
11
12
+ constexpr size_t TEMP_BUF_SIZE=HGL_SIZE_1MB;
13
+ char temp_buffer[TEMP_BUF_SIZE];
14
+
15
+ bool UnzipFile (const OSString &filename,zip_file_t *zf,const int filesize)
16
+ {
17
+ int fs=0 ;
18
+ int size;
19
+
20
+ FileOutputStream fos;
21
+
22
+ if (!fos.CreateTrunc (filename))
23
+ {
24
+ LOG_ERROR (OS_TEXT (" Create file failed,filename: " )+filename);
25
+ zip_fclose (zf);
26
+ return (false );
27
+ }
28
+
29
+ while (fs<filesize)
30
+ {
31
+ size=zip_fread (zf,temp_buffer,TEMP_BUF_SIZE);
32
+
33
+ if (size<0 )
34
+ {
35
+ LOG_ERROR (OS_TEXT (" read file failed at zip" ));
36
+ break ;
37
+ }
38
+
39
+ if (fos.WriteFully (temp_buffer,size)!=size)
40
+ {
41
+ LOG_ERROR (OS_TEXT (" write file failed at " )+OSString::valueOf (fs));
42
+ break ;
43
+ }
44
+
45
+ fs+=size;
46
+ }
47
+
48
+ fos.Close ();
49
+ zip_fclose (zf);
50
+
51
+ if (fs==filesize)
52
+ {
53
+ return (true );
54
+ }
55
+ else
56
+ {
57
+ filesystem::FileDelete (filename);
58
+ LOG_ERROR (" Uncompress failed." );
59
+ return (false );
60
+ }
61
+ }
62
+
12
63
int os_main (int argc,os_char **argv)
13
64
{
14
65
os_out<<OS_TEXT (" uuzip ver 0.01" )<<std::endl;
@@ -32,11 +83,33 @@ int os_main(int argc,os_char **argv)
32
83
return 0 ;
33
84
}
34
85
35
- const OSString charset_name=argv[1 ];
36
- const OSString zip_filename=argv[2 ];
86
+ logger::InitLogger (OS_TEXT (" UUZip" ));
87
+
88
+ OSString output_directory;
89
+
90
+ if (argc>3 )
91
+ {
92
+ if (argv[3 ][0 ]==' .'
93
+ &&argv[3 ][1 ]==0 )
94
+ filesystem::GetCurrentPath (output_directory);
95
+ else
96
+ {
97
+ output_directory=argv[3 ];
98
+ }
99
+ }
100
+ else
101
+ {
102
+ filesystem::GetCurrentPath (output_directory);
103
+ }
104
+
105
+ LOG_INFO (OS_TEXT (" zip file: " )+OSString (argv[2 ]));
106
+ LOG_INFO (OS_TEXT (" output directory: " )+output_directory);
37
107
38
- os_out<<OS_TEXT (" zip file: " )<<argv[2 ]<<std::endl;
39
- os_out<<OS_TEXT (" char set: " )<<argv[1 ]<<std::endl;
108
+ if (!filesystem::IsDirectory (output_directory))
109
+ {
110
+ LOG_ERROR (" output directory error!" );
111
+ return 1 ;
112
+ }
40
113
41
114
zip_error zerr;
42
115
zip_error_init (&zerr);
@@ -59,25 +132,28 @@ int os_main(int argc,os_char **argv)
59
132
60
133
if (!hgl::stou (argv[1 ],cp))
61
134
{
62
- std::wcerr<< L " error codepage: " << argv[1 ]<<std::endl ;
135
+ LOG_ERROR ( OS_TEXT ( " error codepage: " )+ OSString ( argv[1 ])) ;
63
136
return -1 ;
64
137
}
65
138
66
139
cs.codepage =cp;
67
140
68
- os_out<< OS_TEXT (" use codepage: " )<< cs.codepage <<std::endl ;
141
+ LOG_INFO ( OS_TEXT (" use codepage: " )+ OSString::valueOf ( cs.codepage )) ;
69
142
}
70
143
else
71
144
{
72
- os_out<< OS_TEXT (" use charsets : " )<< argv[1 ]<<std::endl ;
145
+ LOG_ERROR ( OS_TEXT (" use charset : " )+ OSString ( argv[1 ])) ;
73
146
74
147
strcpy (cs.charset ,to_u8 (argv[1 ]));
75
148
cs.codepage =FindCodePage (cs.charset );
76
149
}
77
150
78
- MemBlock<u16char> filename (256 );
151
+ MemBlock<u16char> filename (1024 );
152
+ u16char last_char;
153
+ OSString os_filename;
154
+ OSString full_filename;
79
155
int len=0 ;
80
- int u16len=256 ;
156
+ int u16len=filename. GetMaxLength () ;
81
157
82
158
if (zs)
83
159
{
@@ -94,7 +170,39 @@ int os_main(int argc,os_char **argv)
94
170
95
171
to_utf16 (cs,filename,filename.GetMaxLength (),sb.name ,len);
96
172
97
- os_out<<i<<OS_TEXT (" : " )<<filename.data ()<<OS_TEXT (" , size: " )<<sb.size <<std::endl;
173
+ last_char=filename[u16len-1 ];
174
+
175
+ os_filename=OSString (filename);
176
+ full_filename=filesystem::MergeFilename (output_directory,os_filename);
177
+
178
+ if (sb.size ==0
179
+ &&(last_char==' /'
180
+ ||last_char==' \\ ' ))
181
+ {
182
+ LOG_INFO (OS_TEXT (" Path: " )+os_filename);
183
+
184
+ if (!filesystem::IsDirectory (full_filename))
185
+ if (!filesystem::MakePath (full_filename))
186
+ {
187
+ LOG_ERROR (OS_TEXT (" make path error: " )+full_filename);
188
+ return 1 ;
189
+ }
190
+ }
191
+ else
192
+ {
193
+ LOG_INFO (OSString::valueOf (i)+OS_TEXT (" : " )+os_filename+OS_TEXT (" , size: " )+OSString::valueOf (sb.size ));
194
+
195
+ zip_file_t *zf=zip_fopen_index (za,i,0 );
196
+
197
+ if (!zf)
198
+ {
199
+ LOG_ERROR (OS_TEXT (" open file failed at zip,filename: " )+os_filename);
200
+ }
201
+ else
202
+ {
203
+ UnzipFile (full_filename,zf,sb.size );
204
+ }
205
+ }
98
206
}
99
207
}
100
208
0 commit comments