started working on decompressing
This commit is contained in:
parent
1bb0de9d0e
commit
e3f4b3bdd0
44
src/main.zig
44
src/main.zig
@ -36,6 +36,8 @@ const ZipFileHeader = struct {
|
||||
file_name: []u8,
|
||||
extra_field: []u8,
|
||||
compressed_content: []u8,
|
||||
uncompressed_content: []u8,
|
||||
decompressed: bool,
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
@ -61,6 +63,8 @@ const ZipFileHeader = struct {
|
||||
.file_name = undefined,
|
||||
.extra_field = undefined,
|
||||
.compressed_content = undefined,
|
||||
.uncompressed_content = undefined,
|
||||
.decompressed = false,
|
||||
};
|
||||
|
||||
self.file_name = try allocator.alloc(u8, self.file_name_length);
|
||||
@ -74,10 +78,48 @@ const ZipFileHeader = struct {
|
||||
return self;
|
||||
}
|
||||
|
||||
fn extract(self: *Self) !void {
|
||||
if (self.decompressed) {
|
||||
return error.AlreadyDecompressed;
|
||||
}
|
||||
|
||||
if (self.compression_method == 0) {
|
||||
return error.uncompressed_file;
|
||||
}
|
||||
|
||||
if (self.compression_method != 8) {
|
||||
return error.unsuported_compression_method;
|
||||
}
|
||||
|
||||
self.uncompressed_content = try self.allocator.alloc(u8, self.uncompressed_size);
|
||||
errdefer self.allocator.free(self.uncompressed_size);
|
||||
|
||||
var byte = self.compressed_content[0];
|
||||
var lastBlock = byte & 0b1000_0000 == 0b1000_0000;
|
||||
var blockType = @shrExact(byte & 0b0110_0000, 5);
|
||||
|
||||
if (lastBlock) {
|
||||
print("last block", .{});
|
||||
} else {
|
||||
print("not last block", .{});
|
||||
}
|
||||
|
||||
print("blockType: {}", .{blockType});
|
||||
|
||||
if (blockType != 2) {
|
||||
return error.unsuported_block_type;
|
||||
}
|
||||
|
||||
self.decompressed = true;
|
||||
}
|
||||
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.file_name);
|
||||
self.allocator.free(self.extra_field);
|
||||
self.allocator.free(self.compressed_content);
|
||||
if (self.decompressed) {
|
||||
self.allocator.free(self.uncompressed_content);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -140,6 +182,8 @@ pub fn main() !void {
|
||||
var second_file = try ZipFileHeader.init(allocator, reader);
|
||||
defer second_file.deinit();
|
||||
|
||||
try second_file.extract();
|
||||
|
||||
print("G: {s}", .{second_file.file_name});
|
||||
print("GI: {}", .{second_file.compression_method});
|
||||
print("xml stuff:\n{s}", .{second_file.compressed_content});
|
||||
|
Loading…
Reference in New Issue
Block a user