Skip to content

Commit 7f541e7

Browse files
committed
uncompress files OK...first version on Windows.
1 parent 5445c06 commit 7f541e7

File tree

2 files changed

+120
-12
lines changed

2 files changed

+120
-12
lines changed

uuzip.cpp

+119-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
#include<hgl/log/#fo.h>
22
#include<hgl/CodePage.h>
33
#include<hgl/filesystem/FileSystem.h>
4-
#include<hgl/io/FileInputStream.h>
4+
#include<hgl/io/FileOutputStream.h>
55
#include<hgl/type/MemBlock.h>
66
#include<stdio.h>
77
#include<zip.h>
88

99
using namespace hgl;
1010
using namespace hgl::io;
1111

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+
1263
int os_main(int argc,os_char **argv)
1364
{
1465
os_out<<OS_TEXT("uuzip ver 0.01")<<std::endl;
@@ -32,11 +83,33 @@ int os_main(int argc,os_char **argv)
3283
return 0;
3384
}
3485

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);
37107

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+
}
40113

41114
zip_error zerr;
42115
zip_error_init(&zerr);
@@ -59,25 +132,28 @@ int os_main(int argc,os_char **argv)
59132

60133
if(!hgl::stou(argv[1],cp))
61134
{
62-
std::wcerr<<L"error codepage: "<<argv[1]<<std::endl;
135+
LOG_ERROR(OS_TEXT("error codepage: ")+OSString(argv[1]));
63136
return -1;
64137
}
65138

66139
cs.codepage=cp;
67140

68-
os_out<<OS_TEXT("use codepage: ")<<cs.codepage<<std::endl;
141+
LOG_INFO(OS_TEXT("use codepage: ")+OSString::valueOf(cs.codepage));
69142
}
70143
else
71144
{
72-
os_out<<OS_TEXT("use charsets: ")<<argv[1]<<std::endl;
145+
LOG_ERROR(OS_TEXT("use charset: ")+OSString(argv[1]));
73146

74147
strcpy(cs.charset,to_u8(argv[1]));
75148
cs.codepage=FindCodePage(cs.charset);
76149
}
77150

78-
MemBlock<u16char> filename(256);
151+
MemBlock<u16char> filename(1024);
152+
u16char last_char;
153+
OSString os_filename;
154+
OSString full_filename;
79155
int len=0;
80-
int u16len=256;
156+
int u16len=filename.GetMaxLength();
81157

82158
if(zs)
83159
{
@@ -94,7 +170,39 @@ int os_main(int argc,os_char **argv)
94170

95171
to_utf16(cs,filename,filename.GetMaxLength(),sb.name,len);
96172

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+
}
98206
}
99207
}
100208

0 commit comments

Comments
 (0)