diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-03-31 19:01:39 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-01 08:15:49 +0200 |
commit | 857f559a06904a968c2514427032925a7e5ebe9d (patch) | |
tree | 5ec7cccc42ab0996ebef16a14a017f9d97a34ea9 /Userland/Libraries | |
parent | df577b457ace4d0162c10141c2530ebaa735b721 (diff) | |
download | serenity-857f559a06904a968c2514427032925a7e5ebe9d.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCompress/Gzip.cpp | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibCompress/Gzip.h | 2 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCompress/Gzip.cpp b/Userland/Libraries/LibCompress/Gzip.cpp index 10b6788b6b..d64a8fab6f 100644 --- a/Userland/Libraries/LibCompress/Gzip.cpp +++ b/Userland/Libraries/LibCompress/Gzip.cpp @@ -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) diff --git a/Userland/Libraries/LibCompress/Gzip.h b/Userland/Libraries/LibCompress/Gzip.h index de5d103a9c..6dcfbd5113 100644 --- a/Userland/Libraries/LibCompress/Gzip.h +++ b/Userland/Libraries/LibCompress/Gzip.h @@ -52,6 +52,8 @@ public: virtual void close() override {}; static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes); + static ErrorOr<void> decompress_file(StringView input_file, NonnullOwnPtr<Stream> output_stream); + static Optional<DeprecatedString> describe_header(ReadonlyBytes); static bool is_likely_compressed(ReadonlyBytes bytes); |