6
6
7
7
\*******************************************************************/
8
8
9
+ #include < cstring>
9
10
#include < cassert>
10
11
#include < sstream>
11
12
12
13
#include " jar_file.h"
13
14
14
- #ifdef HAVE_LIBZIP
15
- #include < zip.h>
16
- #endif
17
-
18
15
/* ******************************************************************\
19
16
20
17
Function: jar_filet::open
@@ -29,33 +26,32 @@ Function: jar_filet::open
29
26
30
27
void jar_filet::open (const std::string &filename)
31
28
{
32
- #ifdef HAVE_LIBZIP
33
- if (zip!=nullptr )
34
- // NOLINTNEXTLINE(readability/identifiers)
35
- zip_close (static_cast <struct zip *>(zip));
36
-
37
- int zip_error;
38
- zip=zip_open (filename.c_str (), 0 , &zip_error);
29
+ if (!mz_ok)
30
+ {
31
+ memset (&zip, 0 , sizeof (zip));
32
+ mz_bool mz_open=mz_zip_reader_init_file (&zip, filename.c_str (), 0 );
33
+ mz_ok=mz_open==MZ_TRUE;
34
+ }
39
35
40
- if (zip!= nullptr )
36
+ if (mz_ok )
41
37
{
42
38
std::size_t number_of_files=
43
- // NOLINTNEXTLINE(readability/identifiers)
44
- zip_get_num_entries (static_cast <struct zip *>(zip), 0 );
39
+ mz_zip_reader_get_num_files (&zip);
45
40
46
41
index .reserve (number_of_files);
47
42
48
43
for (std::size_t i=0 ; i<number_of_files; i++)
49
44
{
50
- std::string file_name=
51
- // NOLINTNEXTLINE(readability/identifiers)
52
- zip_get_name (static_cast <struct zip *>(zip), i, 0 );
45
+ mz_uint filename_length=mz_zip_reader_get_filename (&zip, i, nullptr , 0 );
46
+ char *filename_char=new char [filename_length+1 ];
47
+ mz_uint filename_len=
48
+ mz_zip_reader_get_filename (&zip, i, filename_char, filename_length);
49
+ assert (filename_length==filename_len);
50
+ std::string file_name (filename_char);
51
+ delete[] filename_char;
53
52
index .push_back (file_name);
54
53
}
55
54
}
56
- #else
57
- zip=nullptr ;
58
- #endif
59
55
}
60
56
61
57
/* ******************************************************************\
@@ -72,11 +68,11 @@ Function: jar_filet::~jar_filet
72
68
73
69
jar_filet::~jar_filet ()
74
70
{
75
- # ifdef HAVE_LIBZIP
76
- if (zip!= nullptr )
77
- // NOLINTNEXTLINE(readability/identifiers)
78
- zip_close ( static_cast < struct zip *>(zip)) ;
79
- # endif
71
+ if (mz_ok)
72
+ {
73
+ mz_zip_reader_end (&zip);
74
+ mz_ok= false ;
75
+ }
80
76
}
81
77
82
78
/* ******************************************************************\
@@ -91,47 +87,29 @@ Function: jar_filet::get_entry
91
87
92
88
\*******************************************************************/
93
89
94
- #define ZIP_READ_SIZE 10000
95
-
96
90
std::string jar_filet::get_entry (std::size_t i)
97
91
{
98
- if (zip== nullptr )
92
+ if (!mz_ok )
99
93
return std::string (" " );
100
94
101
95
assert (i<index .size ());
102
96
103
97
std::string dest;
104
98
105
- #ifdef HAVE_LIBZIP
106
- void *zip_e=zip; // zip is both a type and a non-type
107
- // NOLINTNEXTLINE(readability/identifiers)
108
- struct zip *zip_p=static_cast <struct zip *>(zip_e);
109
-
110
- // NOLINTNEXTLINE(readability/identifiers)
111
- struct zip_file *zip_file=zip_fopen_index (zip_p, i, 0 );
112
-
113
- if (zip_file==NULL )
114
- {
115
- zip_close (zip_p);
116
- zip=nullptr ;
117
- return std::string (" " ); // error
118
- }
119
-
99
+ mz_zip_archive_file_stat file_stat;
100
+ memset (&file_stat, 0 , sizeof (file_stat));
101
+ mz_bool stat_ok=mz_zip_reader_file_stat (&zip, i, &file_stat);
102
+ if (stat_ok!=MZ_TRUE)
103
+ return std::string ();
120
104
std::vector<char > buffer;
121
- buffer.resize (ZIP_READ_SIZE);
122
-
123
- while (true )
124
- {
125
- int bytes_read=
126
- zip_fread (zip_file, buffer.data (), ZIP_READ_SIZE);
127
- assert (bytes_read<=ZIP_READ_SIZE);
128
- if (bytes_read<=0 )
129
- break ;
130
- dest.insert (dest.end (), buffer.begin (), buffer.begin ()+bytes_read);
131
- }
132
-
133
- zip_fclose (zip_file);
134
- #endif
105
+ size_t bufsize=file_stat.m_uncomp_size ;
106
+ buffer.resize (bufsize);
107
+ mz_bool read_ok=
108
+ mz_zip_reader_extract_to_mem (&zip, i, buffer.data (), bufsize, 0 );
109
+ if (read_ok!=MZ_TRUE)
110
+ return std::string ();
111
+
112
+ dest.insert (dest.end (), buffer.begin (), buffer.end ());
135
113
136
114
return dest;
137
115
}
0 commit comments