@@ -73,13 +73,17 @@ size_t mz_zip_archivet::get_num_files()
73
73
74
74
std::string mz_zip_archivet::get_filename (const size_t index)
75
75
{
76
- const auto id=static_cast <mz_uint>(index);
77
- std::vector<char > buffer;
78
- buffer.resize (mz_zip_reader_get_filename (m_state.get (), id, nullptr , 0 ));
79
- mz_zip_reader_get_filename (m_state.get (), id, buffer.data (), buffer.size ());
80
- // Buffer may contain junk returned after \0
81
- const auto null_char_it=std::find (buffer.cbegin (), buffer.cend (), ' \0 ' );
82
- return { buffer.cbegin (), null_char_it };
76
+ const auto id = static_cast <mz_uint>(index);
77
+ mz_uint name_size = mz_zip_reader_get_filename (m_state.get (), id, nullptr , 0 );
78
+ if (name_size == 0 )
79
+ return {}; // Failure
80
+ // It is valid to directly write to a string's buffer (see C++11 standard,
81
+ // basic_string general requirements [string.require], 21.4.1.5)
82
+ std::string buffer (name_size, ' \0 ' );
83
+ mz_zip_reader_get_filename (m_state.get (), id, &buffer[0 ], buffer.size ());
84
+ // Buffer contains trailing \0
85
+ buffer.resize (name_size - 1 );
86
+ return buffer;
83
87
}
84
88
85
89
std::string mz_zip_archivet::extract (const size_t index)
@@ -89,12 +93,13 @@ std::string mz_zip_archivet::extract(const size_t index)
89
93
const mz_bool stat_ok=mz_zip_reader_file_stat (m_state.get (), id, &file_stat);
90
94
if (stat_ok==MZ_TRUE)
91
95
{
92
- std::vector<char > buffer (file_stat.m_uncomp_size );
93
- const mz_bool read_ok=mz_zip_reader_extract_to_mem (
94
- m_state.get (), id, buffer.data (), buffer.size (), 0 );
95
- if (read_ok==MZ_TRUE)
96
- return { buffer.cbegin (), buffer.cend () };
96
+ // It is valid to directly write to a string's buffer (see C++11 standard,
97
+ // basic_string general requirements [string.require], 21.4.1.5)
98
+ std::string buffer (file_stat.m_uncomp_size , ' \0 ' );
99
+ const mz_bool read_ok = mz_zip_reader_extract_to_mem (
100
+ m_state.get (), id, &buffer[0 ], buffer.size (), 0 );
101
+ if (read_ok == MZ_TRUE)
102
+ return buffer;
97
103
}
98
104
throw std::runtime_error (" Could not extract the file" );
99
105
}
100
-
0 commit comments