mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:47:36 +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:
parent
df577b457a
commit
857f559a06
3 changed files with 20 additions and 18 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
|
|
||||||
namespace Compress {
|
namespace Compress {
|
||||||
|
|
||||||
|
@ -179,6 +180,22 @@ ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
|
||||||
return output_buffer;
|
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(); }
|
bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); }
|
||||||
|
|
||||||
ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)
|
ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)
|
||||||
|
|
|
@ -52,6 +52,8 @@ public:
|
||||||
virtual void close() override {};
|
virtual void close() override {};
|
||||||
|
|
||||||
static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes);
|
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 Optional<DeprecatedString> describe_header(ReadonlyBytes);
|
||||||
static bool is_likely_compressed(ReadonlyBytes bytes);
|
static bool is_likely_compressed(ReadonlyBytes bytes);
|
||||||
|
|
||||||
|
|
|
@ -11,19 +11,6 @@
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static ErrorOr<void> decompress_file(NonnullOwnPtr<Stream> input_stream, Stream& output_stream)
|
|
||||||
{
|
|
||||||
auto gzip_stream = Compress::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 {};
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments args)
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
{
|
{
|
||||||
Vector<StringView> filenames;
|
Vector<StringView> filenames;
|
||||||
|
@ -51,12 +38,8 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
output_filename = filename;
|
output_filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto input_file = TRY(Core::File::open(input_filename, Core::File::OpenMode::Read));
|
|
||||||
auto buffered_input_file = TRY(Core::BufferedFile::create(move(input_file), 256 * KiB));
|
|
||||||
|
|
||||||
auto output_stream = write_to_stdout ? TRY(Core::File::standard_output()) : TRY(Core::File::open(output_filename, Core::File::OpenMode::Write));
|
auto output_stream = write_to_stdout ? TRY(Core::File::standard_output()) : TRY(Core::File::open(output_filename, Core::File::OpenMode::Write));
|
||||||
|
TRY(Compress::GzipDecompressor::decompress_file(input_filename, move(output_stream)));
|
||||||
TRY(decompress_file(move(buffered_input_file), *output_stream));
|
|
||||||
|
|
||||||
if (!keep_input_files)
|
if (!keep_input_files)
|
||||||
TRY(Core::System::unlink(input_filename));
|
TRY(Core::System::unlink(input_filename));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue