Skip to content

Commit 3310bb4

Browse files
committed
Fix reading ZIP files where no compression is used in the zip file.
1 parent 2b2df72 commit 3310bb4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/ZipArchive.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,25 @@ size_t read_file_from_zip( std::istream &instrm,
211211

212212
inflateEnd( &strm );
213213
return total_uncompressed;
214-
}else if( header->compression_type == UNCOMPRESSED )
214+
}else if( (header->compression_type == UNCOMPRESSED)
215+
&& (header->compressed_size == header->uncompressed_size) )
215216
{
216-
const size_t nread = std::min( buffer_size - 4,
217-
header->uncompressed_size - total_read );
218-
instrm.read( (char*)(out+4), nread );
219-
return static_cast<size_t>( instrm.gcount() );
217+
size_t num_written = 0;
218+
size_t num_bytes = header->compressed_size;
219+
while( num_bytes > 0 )
220+
{
221+
const size_t nread = std::min( buffer_size, static_cast<const unsigned int>(num_bytes) );
222+
instrm.read( (char *)in, nread );
223+
std::streamsize bytes_read = instrm.gcount(); // Get the number of bytes actually read
224+
output.write( (const char *)in, bytes_read );
225+
num_bytes -= std::min( bytes_read, static_cast<std::streamsize>(num_bytes) );
226+
num_written += bytes_read;
227+
228+
if( bytes_read < static_cast<std::streamsize>(nread) )
229+
break;
230+
}//while( num_bytes > 0 )
231+
232+
return num_written;
220233
}else
221234
{
222235
throw runtime_error( "ZipArchive: unrecognized compression" );

0 commit comments

Comments
 (0)