mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:48:12 +00:00
LibArchive+Utilities: Port ZipOutputStream to Core::Stream
This commit is contained in:
parent
012645bdf9
commit
8238f926fd
3 changed files with 90 additions and 80 deletions
|
@ -9,11 +9,6 @@
|
||||||
|
|
||||||
namespace Archive {
|
namespace Archive {
|
||||||
|
|
||||||
OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method)
|
|
||||||
{
|
|
||||||
return stream << to_underlying(method);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Zip::find_end_of_central_directory_offset(ReadonlyBytes buffer, size_t& offset)
|
bool Zip::find_end_of_central_directory_offset(ReadonlyBytes buffer, size_t& offset)
|
||||||
{
|
{
|
||||||
for (size_t backwards_offset = 0; backwards_offset <= UINT16_MAX; backwards_offset++) // the file may have a trailing comment of an arbitrary 16 bit length
|
for (size_t backwards_offset = 0; backwards_offset <= UINT16_MAX; backwards_offset++) // the file may have a trailing comment of an arbitrary 16 bit length
|
||||||
|
@ -108,8 +103,8 @@ bool Zip::for_each_member(Function<IterationDecision(ZipMember const&)> callback
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipOutputStream::ZipOutputStream(OutputStream& stream)
|
ZipOutputStream::ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream> stream)
|
||||||
: m_stream(stream)
|
: m_stream(move(stream))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,12 +114,12 @@ static u16 minimum_version_needed(ZipCompressionMethod method)
|
||||||
return method == ZipCompressionMethod::Deflate ? 20 : 10;
|
return method == ZipCompressionMethod::Deflate ? 20 : 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZipOutputStream::add_member(ZipMember const& member)
|
ErrorOr<void> ZipOutputStream::add_member(ZipMember const& member)
|
||||||
{
|
{
|
||||||
VERIFY(!m_finished);
|
VERIFY(!m_finished);
|
||||||
VERIFY(member.name.length() <= UINT16_MAX);
|
VERIFY(member.name.length() <= UINT16_MAX);
|
||||||
VERIFY(member.compressed_data.size() <= UINT32_MAX);
|
VERIFY(member.compressed_data.size() <= UINT32_MAX);
|
||||||
m_members.append(member);
|
TRY(m_members.try_append(member));
|
||||||
|
|
||||||
LocalFileHeader local_file_header {
|
LocalFileHeader local_file_header {
|
||||||
.minimum_version = minimum_version_needed(member.compression_method),
|
.minimum_version = minimum_version_needed(member.compression_method),
|
||||||
|
@ -141,10 +136,10 @@ void ZipOutputStream::add_member(ZipMember const& member)
|
||||||
.extra_data = nullptr,
|
.extra_data = nullptr,
|
||||||
.compressed_data = member.compressed_data.data(),
|
.compressed_data = member.compressed_data.data(),
|
||||||
};
|
};
|
||||||
local_file_header.write(m_stream);
|
return local_file_header.write(*m_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZipOutputStream::finish()
|
ErrorOr<void> ZipOutputStream::finish()
|
||||||
{
|
{
|
||||||
VERIFY(!m_finished);
|
VERIFY(!m_finished);
|
||||||
m_finished = true;
|
m_finished = true;
|
||||||
|
@ -175,7 +170,7 @@ void ZipOutputStream::finish()
|
||||||
.comment = nullptr,
|
.comment = nullptr,
|
||||||
};
|
};
|
||||||
file_header_offset += sizeof(LocalFileHeader::signature) + (sizeof(LocalFileHeader) - (sizeof(u8*) * 3)) + member.name.length() + member.compressed_data.size();
|
file_header_offset += sizeof(LocalFileHeader::signature) + (sizeof(LocalFileHeader) - (sizeof(u8*) * 3)) + member.name.length() + member.compressed_data.size();
|
||||||
central_directory_record.write(m_stream);
|
TRY(central_directory_record.write(*m_stream));
|
||||||
central_directory_size += central_directory_record.size();
|
central_directory_size += central_directory_record.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +184,7 @@ void ZipOutputStream::finish()
|
||||||
.comment_length = 0,
|
.comment_length = 0,
|
||||||
.comment = nullptr,
|
.comment = nullptr,
|
||||||
};
|
};
|
||||||
end_of_central_directory.write(m_stream);
|
return end_of_central_directory.write(*m_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/IterationDecision.h>
|
#include <AK/IterationDecision.h>
|
||||||
#include <AK/Stream.h>
|
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/Stream.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
namespace Archive {
|
namespace Archive {
|
||||||
|
@ -55,18 +55,23 @@ struct [[gnu::packed]] EndOfCentralDirectory {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(OutputStream& stream) const
|
ErrorOr<void> write(Core::Stream::Stream& stream) const
|
||||||
{
|
{
|
||||||
stream.write_or_error(signature);
|
auto write_value = [&stream](auto value) {
|
||||||
stream << disk_number;
|
return stream.write_entire_buffer({ &value, sizeof(value) });
|
||||||
stream << central_directory_start_disk;
|
};
|
||||||
stream << disk_records_count;
|
|
||||||
stream << total_records_count;
|
TRY(stream.write_entire_buffer(signature));
|
||||||
stream << central_directory_size;
|
TRY(write_value(disk_number));
|
||||||
stream << central_directory_offset;
|
TRY(write_value(central_directory_start_disk));
|
||||||
stream << comment_length;
|
TRY(write_value(disk_records_count));
|
||||||
|
TRY(write_value(total_records_count));
|
||||||
|
TRY(write_value(central_directory_size));
|
||||||
|
TRY(write_value(central_directory_offset));
|
||||||
|
TRY(write_value(comment_length));
|
||||||
if (comment_length > 0)
|
if (comment_length > 0)
|
||||||
stream.write_or_error({ comment, comment_length });
|
TRY(stream.write_entire_buffer({ comment, comment_length }));
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,8 +105,6 @@ union ZipGeneralPurposeFlags {
|
||||||
};
|
};
|
||||||
static_assert(sizeof(ZipGeneralPurposeFlags) == sizeof(u16));
|
static_assert(sizeof(ZipGeneralPurposeFlags) == sizeof(u16));
|
||||||
|
|
||||||
OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method);
|
|
||||||
|
|
||||||
struct [[gnu::packed]] CentralDirectoryRecord {
|
struct [[gnu::packed]] CentralDirectoryRecord {
|
||||||
static constexpr Array<u8, signature_length> signature = { 0x50, 0x4b, 0x01, 0x02 }; // 'PK\x01\x02'
|
static constexpr Array<u8, signature_length> signature = { 0x50, 0x4b, 0x01, 0x02 }; // 'PK\x01\x02'
|
||||||
|
|
||||||
|
@ -138,31 +141,36 @@ struct [[gnu::packed]] CentralDirectoryRecord {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(OutputStream& stream) const
|
ErrorOr<void> write(Core::Stream::Stream& stream) const
|
||||||
{
|
{
|
||||||
stream.write_or_error(signature);
|
auto write_value = [&stream](auto value) {
|
||||||
stream << made_by_version;
|
return stream.write_entire_buffer({ &value, sizeof(value) });
|
||||||
stream << minimum_version;
|
};
|
||||||
stream << general_purpose_flags.flags;
|
|
||||||
stream << compression_method;
|
TRY(stream.write_entire_buffer(signature));
|
||||||
stream << modification_time;
|
TRY(write_value(made_by_version));
|
||||||
stream << modification_date;
|
TRY(write_value(minimum_version));
|
||||||
stream << crc32;
|
TRY(write_value(general_purpose_flags.flags));
|
||||||
stream << compressed_size;
|
TRY(write_value(compression_method));
|
||||||
stream << uncompressed_size;
|
TRY(write_value(modification_time));
|
||||||
stream << name_length;
|
TRY(write_value(modification_date));
|
||||||
stream << extra_data_length;
|
TRY(write_value(crc32));
|
||||||
stream << comment_length;
|
TRY(write_value(compressed_size));
|
||||||
stream << start_disk;
|
TRY(write_value(uncompressed_size));
|
||||||
stream << internal_attributes;
|
TRY(write_value(name_length));
|
||||||
stream << external_attributes;
|
TRY(write_value(extra_data_length));
|
||||||
stream << local_file_header_offset;
|
TRY(write_value(comment_length));
|
||||||
|
TRY(write_value(start_disk));
|
||||||
|
TRY(write_value(internal_attributes));
|
||||||
|
TRY(write_value(external_attributes));
|
||||||
|
TRY(write_value(local_file_header_offset));
|
||||||
if (name_length > 0)
|
if (name_length > 0)
|
||||||
stream.write_or_error({ name, name_length });
|
TRY(stream.write_entire_buffer({ name, name_length }));
|
||||||
if (extra_data_length > 0)
|
if (extra_data_length > 0)
|
||||||
stream.write_or_error({ extra_data, extra_data_length });
|
TRY(stream.write_entire_buffer({ extra_data, extra_data_length }));
|
||||||
if (comment_length > 0)
|
if (comment_length > 0)
|
||||||
stream.write_or_error({ comment, comment_length });
|
TRY(stream.write_entire_buffer({ comment, comment_length }));
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] size_t size() const
|
[[nodiscard]] size_t size() const
|
||||||
|
@ -202,25 +210,30 @@ struct [[gnu::packed]] LocalFileHeader {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(OutputStream& stream) const
|
ErrorOr<void> write(Core::Stream::Stream& stream) const
|
||||||
{
|
{
|
||||||
stream.write_or_error(signature);
|
auto write_value = [&stream](auto value) {
|
||||||
stream << minimum_version;
|
return stream.write_entire_buffer({ &value, sizeof(value) });
|
||||||
stream << general_purpose_flags.flags;
|
};
|
||||||
stream << compression_method;
|
|
||||||
stream << modification_time;
|
TRY(stream.write_entire_buffer(signature));
|
||||||
stream << modification_date;
|
TRY(write_value(minimum_version));
|
||||||
stream << crc32;
|
TRY(write_value(general_purpose_flags.flags));
|
||||||
stream << compressed_size;
|
TRY(write_value(compression_method));
|
||||||
stream << uncompressed_size;
|
TRY(write_value(modification_time));
|
||||||
stream << name_length;
|
TRY(write_value(modification_date));
|
||||||
stream << extra_data_length;
|
TRY(write_value(crc32));
|
||||||
|
TRY(write_value(compressed_size));
|
||||||
|
TRY(write_value(uncompressed_size));
|
||||||
|
TRY(write_value(name_length));
|
||||||
|
TRY(write_value(extra_data_length));
|
||||||
if (name_length > 0)
|
if (name_length > 0)
|
||||||
stream.write_or_error({ name, name_length });
|
TRY(stream.write_entire_buffer({ name, name_length }));
|
||||||
if (extra_data_length > 0)
|
if (extra_data_length > 0)
|
||||||
stream.write_or_error({ extra_data, extra_data_length });
|
TRY(stream.write_entire_buffer({ extra_data, extra_data_length }));
|
||||||
if (compressed_size > 0)
|
if (compressed_size > 0)
|
||||||
stream.write_or_error({ compressed_data, compressed_size });
|
TRY(stream.write_entire_buffer({ compressed_data, compressed_size }));
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -254,12 +267,13 @@ private:
|
||||||
|
|
||||||
class ZipOutputStream {
|
class ZipOutputStream {
|
||||||
public:
|
public:
|
||||||
ZipOutputStream(OutputStream&);
|
ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream>);
|
||||||
void add_member(ZipMember const&);
|
|
||||||
void finish();
|
ErrorOr<void> add_member(ZipMember const&);
|
||||||
|
ErrorOr<void> finish();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OutputStream& m_stream;
|
NonnullOwnPtr<Core::Stream::Stream> m_stream;
|
||||||
Vector<ZipMember> m_members;
|
Vector<ZipMember> m_members;
|
||||||
|
|
||||||
bool m_finished { false };
|
bool m_finished { false };
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/FileStream.h>
|
|
||||||
#include <LibCore/Stream.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibCrypto/Checksum/CRC32.h>
|
#include <LibCrypto/Checksum/CRC32.h>
|
||||||
|
@ -48,11 +47,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto file_stream = TRY(Core::OutputFileStream::open(zip_file_path));
|
|
||||||
|
|
||||||
outln("Archive: {}", zip_file_path);
|
outln("Archive: {}", zip_file_path);
|
||||||
|
auto file_stream = TRY(Core::Stream::File::open(zip_file_path, Core::Stream::OpenMode::Write));
|
||||||
Archive::ZipOutputStream zip_stream { file_stream };
|
Archive::ZipOutputStream zip_stream(move(file_stream));
|
||||||
|
|
||||||
auto add_file = [&](DeprecatedString path) -> ErrorOr<void> {
|
auto add_file = [&](DeprecatedString path) -> ErrorOr<void> {
|
||||||
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
||||||
|
@ -76,11 +73,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
Crypto::Checksum::CRC32 checksum { file_buffer.bytes() };
|
Crypto::Checksum::CRC32 checksum { file_buffer.bytes() };
|
||||||
member.crc32 = checksum.digest();
|
member.crc32 = checksum.digest();
|
||||||
member.is_directory = false;
|
member.is_directory = false;
|
||||||
zip_stream.add_member(member);
|
return zip_stream.add_member(member);
|
||||||
return {};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto add_directory = [&](DeprecatedString path, auto handle_directory) -> void {
|
auto add_directory = [&](DeprecatedString path, auto handle_directory) -> ErrorOr<void> {
|
||||||
auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path));
|
auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path));
|
||||||
Archive::ZipMember member {};
|
Archive::ZipMember member {};
|
||||||
member.name = canonicalized_path;
|
member.name = canonicalized_path;
|
||||||
|
@ -89,30 +85,35 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
member.uncompressed_size = 0;
|
member.uncompressed_size = 0;
|
||||||
member.crc32 = 0;
|
member.crc32 = 0;
|
||||||
member.is_directory = true;
|
member.is_directory = true;
|
||||||
zip_stream.add_member(member);
|
TRY(zip_stream.add_member(member));
|
||||||
outln(" adding: {} (stored 0%)", canonicalized_path);
|
outln(" adding: {} (stored 0%)", canonicalized_path);
|
||||||
|
|
||||||
if (!recurse)
|
if (!recurse)
|
||||||
return;
|
return {};
|
||||||
|
|
||||||
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
||||||
while (it.has_next()) {
|
while (it.has_next()) {
|
||||||
auto child_path = it.next_full_path();
|
auto child_path = it.next_full_path();
|
||||||
if (Core::File::is_link(child_path))
|
if (Core::File::is_link(child_path))
|
||||||
return;
|
return {};
|
||||||
if (!Core::File::is_directory(child_path)) {
|
if (!Core::File::is_directory(child_path)) {
|
||||||
auto result = add_file(child_path);
|
auto result = add_file(child_path);
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
warnln("Couldn't add file '{}': {}", child_path, result.error());
|
warnln("Couldn't add file '{}': {}", child_path, result.error());
|
||||||
} else {
|
} else {
|
||||||
handle_directory(child_path, handle_directory);
|
auto result = handle_directory(child_path, handle_directory);
|
||||||
|
if (result.is_error())
|
||||||
|
warnln("Couldn't add directory '{}': {}", child_path, result.error());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto const& source_path : source_paths) {
|
for (auto const& source_path : source_paths) {
|
||||||
if (Core::File::is_directory(source_path)) {
|
if (Core::File::is_directory(source_path)) {
|
||||||
add_directory(source_path, add_directory);
|
auto result = add_directory(source_path, add_directory);
|
||||||
|
if (result.is_error())
|
||||||
|
warnln("Couldn't add directory '{}': {}", source_path, result.error());
|
||||||
} else {
|
} else {
|
||||||
auto result = add_file(source_path);
|
auto result = add_file(source_path);
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
|
@ -120,7 +121,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zip_stream.finish();
|
TRY(zip_stream.finish());
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue