mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
LibCompress: Add a utility to GZIP compress an entire file
This is copy-pasted from the gzip utility, along with its existing TODO. This is currently only needed by that utility, but this gives us API symmetry with GzipDecompressor, and helps ensure we won't end up in a situation where only one utility receives optimizations that should be received by all interested parties.
This commit is contained in:
parent
857f559a06
commit
7ec91dfde7
Notes:
sideshowbarker
2024-07-17 09:41:18 +09:00
Author: https://github.com/trflynn89
Commit: 7ec91dfde7
Pull-request: https://github.com/SerenityOS/serenity/pull/18122
2 changed files with 21 additions and 0 deletions
|
@ -12,6 +12,8 @@
|
|||
#include <AK/MemoryStream.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/MappedFile.h>
|
||||
#include <LibCore/System.h>
|
||||
|
||||
namespace Compress {
|
||||
|
||||
|
@ -262,4 +264,22 @@ ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes)
|
|||
return buffer;
|
||||
}
|
||||
|
||||
ErrorOr<void> GzipCompressor::compress_file(StringView input_filename, NonnullOwnPtr<Stream> output_stream)
|
||||
{
|
||||
// We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression)
|
||||
// TODO: automatically fallback to buffered streaming for very large files
|
||||
RefPtr<Core::MappedFile> file;
|
||||
ReadonlyBytes input_bytes;
|
||||
|
||||
if (TRY(Core::System::stat(input_filename)).st_size > 0) {
|
||||
file = TRY(Core::MappedFile::map(input_filename));
|
||||
input_bytes = file->bytes();
|
||||
}
|
||||
|
||||
auto output_bytes = TRY(Compress::GzipCompressor::compress_all(input_bytes));
|
||||
TRY(output_stream->write_until_depleted(output_bytes));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -93,6 +93,7 @@ public:
|
|||
virtual void close() override;
|
||||
|
||||
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes);
|
||||
static ErrorOr<void> compress_file(StringView input_file, NonnullOwnPtr<Stream> output_stream);
|
||||
|
||||
private:
|
||||
MaybeOwned<Stream> m_output_stream;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue