-
Notifications
You must be signed in to change notification settings - Fork 274
Cleanups in loading of jar files #4698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tautschnig
merged 5 commits into
diffblue:develop
from
NathanJPhillips:cleanup/jar-loading
May 31, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5381af9
Avoid copying memory for zip file contents twice during extraction
NathanJPhillips 1cb05e3
Add ability to extract contents of zip archive to a file
NathanJPhillips d3ec3d1
Move string into map rather than copy it
NathanJPhillips f315e79
Remove hack only needed for old version of Visual Studio
NathanJPhillips 72936d1
Corrected copy and pasted comment
NathanJPhillips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,13 +73,17 @@ size_t mz_zip_archivet::get_num_files() | |
|
||
std::string mz_zip_archivet::get_filename(const size_t index) | ||
{ | ||
const auto id=static_cast<mz_uint>(index); | ||
std::vector<char> buffer; | ||
buffer.resize(mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0)); | ||
mz_zip_reader_get_filename(m_state.get(), id, buffer.data(), buffer.size()); | ||
// Buffer may contain junk returned after \0 | ||
const auto null_char_it=std::find(buffer.cbegin(), buffer.cend(), '\0'); | ||
return { buffer.cbegin(), null_char_it }; | ||
const auto id = static_cast<mz_uint>(index); | ||
mz_uint name_size = mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0); | ||
if(name_size == 0) | ||
return {}; // Failure | ||
// It is valid to directly write to a string's buffer (see C++11 standard, | ||
// basic_string general requirements [string.require], 21.4.1.5) | ||
std::string buffer(name_size, '\0'); | ||
mz_zip_reader_get_filename(m_state.get(), id, &buffer[0], buffer.size()); | ||
// Buffer contains trailing \0 | ||
buffer.resize(name_size - 1); | ||
return buffer; | ||
} | ||
|
||
std::string mz_zip_archivet::extract(const size_t index) | ||
|
@@ -89,12 +93,26 @@ std::string mz_zip_archivet::extract(const size_t index) | |
const mz_bool stat_ok=mz_zip_reader_file_stat(m_state.get(), id, &file_stat); | ||
if(stat_ok==MZ_TRUE) | ||
{ | ||
std::vector<char> buffer(file_stat.m_uncomp_size); | ||
const mz_bool read_ok=mz_zip_reader_extract_to_mem( | ||
m_state.get(), id, buffer.data(), buffer.size(), 0); | ||
if(read_ok==MZ_TRUE) | ||
return { buffer.cbegin(), buffer.cend() }; | ||
// It is valid to directly write to a string's buffer (see C++11 standard, | ||
// basic_string general requirements [string.require], 21.4.1.5) | ||
std::string buffer(file_stat.m_uncomp_size, '\0'); | ||
const mz_bool read_ok = mz_zip_reader_extract_to_mem( | ||
m_state.get(), id, &buffer[0], buffer.size(), 0); | ||
if(read_ok == MZ_TRUE) | ||
return buffer; | ||
} | ||
throw std::runtime_error("Could not extract the file"); | ||
} | ||
|
||
void mz_zip_archivet::extract_to_file( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't seem to match the PR title. |
||
const size_t index, | ||
const std::string &path) | ||
{ | ||
const auto id = static_cast<mz_uint>(index); | ||
if( | ||
mz_zip_reader_extract_to_file(m_state.get(), id, path.c_str(), 0) != | ||
MZ_TRUE) | ||
{ | ||
throw std::runtime_error("Could not extract the file"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces round multi-line
if