1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:48:11 +00:00

gunzip+LibCompress: Move utility to decompress files to GzipDecompressor

This is to allow re-using this method (and any optimization it receives)
by other utilities, like gzip.
This commit is contained in:
Timothy Flynn 2023-03-31 19:01:39 -04:00 committed by Andreas Kling
parent df577b457a
commit 857f559a06
3 changed files with 20 additions and 18 deletions

View file

@ -11,6 +11,7 @@
#include <AK/DeprecatedString.h>
#include <AK/MemoryStream.h>
#include <LibCore/DateTime.h>
#include <LibCore/File.h>
namespace Compress {
@ -179,6 +180,22 @@ ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
return output_buffer;
}
ErrorOr<void> GzipDecompressor::decompress_file(StringView input_filename, NonnullOwnPtr<Stream> output_stream)
{
auto input_file = TRY(Core::File::open(input_filename, Core::File::OpenMode::Read));
auto input_stream = TRY(Core::BufferedFile::create(move(input_file), 256 * KiB));
auto gzip_stream = GzipDecompressor { move(input_stream) };
auto buffer = TRY(ByteBuffer::create_uninitialized(256 * KiB));
while (!gzip_stream.is_eof()) {
auto span = TRY(gzip_stream.read_some(buffer));
TRY(output_stream->write_until_depleted(span));
}
return {};
}
bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); }
ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)