Created
November 11, 2026 14:18
-
-
Save binarycraft007/776a39d276b72909726e717c1722ec26 to your computer and use it in GitHub Desktop.
zig zip compression revived
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Struct File allows for associating a file's contents with the corresponding path in the Zip file. | |
| pub const File = struct { | |
| name: []const u8, | |
| content: []const u8, | |
| }; | |
| /// Used to store any data from writing a file to the zip archive that's needed | |
| /// when writing the corresponding central directory record. | |
| pub const FileStore = struct { | |
| compression: zip.CompressionMethod, | |
| file_offset: u64, | |
| crc32: u32, | |
| compressed_size: u32, | |
| uncompressed_size: usize, | |
| name: []const u8, | |
| }; | |
| // The backing writer to write to | |
| writer: *std.Io.Writer, | |
| // We'll need to do some allocations when deflating files... | |
| allocator: std.mem.Allocator, | |
| // Keep track of the current index; it's necessary for the Central Directory (CD) entries | |
| offset: u64 = 0, | |
| // How many entries do we have on the CD? | |
| central_count: u64 = 0, | |
| // Offset of the CD. | |
| first_central_offset: ?u64 = null, | |
| const Self = @This(); | |
| pub fn init(writer: *std.Io.Writer, allocator: std.mem.Allocator) Self { | |
| return .{ .writer = writer, .allocator = allocator }; | |
| } | |
| pub fn writeFiles(self: *Self, files: []const File, compression: zip.CompressionMethod) !void { | |
| var entries: std.ArrayList(FileStore) = .empty; | |
| defer entries.deinit(self.allocator); | |
| for (files) |file| { | |
| const entry = try self.writeFile(file.name, file.content, compression); | |
| try entries.append(self.allocator, entry); | |
| } | |
| for (entries.items) |entry| { | |
| try self.writeCentralRecord(entry); | |
| } | |
| try self.writeEndRecord(); | |
| } | |
| fn writeFile(self: *Self, name: []const u8, content: []const u8, compression: zip.CompressionMethod) !FileStore { | |
| const writer = self.writer; | |
| // Get a hold of the LocalFileHeader offset before writing anything else. | |
| const current_offset = self.offset; | |
| const crc32 = std.hash.Crc32.hash(content); | |
| // Allocate the underlying buffer in advance to avoid resizing | |
| var deflate_writer: std.Io.Writer.Allocating = try .initCapacity(self.allocator, 1024 * 1024 * content.len); | |
| // Don't trip asserts in flate.Compress.init; this will likely do nothing | |
| // as the initial capacity will be greater though... | |
| try deflate_writer.ensureTotalCapacity(flate.max_window_len + 16); | |
| defer deflate_writer.deinit(); | |
| // We can also use .default to speed up compression significantly | |
| var buf: [flate.max_window_len]u8 = undefined; | |
| var compressor: flate.Compress = try .init(&deflate_writer.writer, &buf, .raw, .default); | |
| const compressed = switch (compression) { | |
| .store => content, | |
| .deflate => blk: { | |
| try compressor.writer.writeAll(content); | |
| try compressor.finish(); | |
| break :blk deflate_writer.written(); | |
| }, | |
| else => return error.Unsupported, | |
| }; | |
| const hdr: zip.LocalFileHeader = .{ | |
| .signature = zip.local_file_header_sig, | |
| .version_needed_to_extract = 10, | |
| .flags = .{ .encrypted = false, ._ = 0 }, | |
| .compression_method = compression, | |
| .last_modification_time = 0, | |
| .last_modification_date = 0, | |
| .crc32 = crc32, | |
| .compressed_size = @intCast(compressed.len), | |
| .uncompressed_size = @intCast(content.len), | |
| .filename_len = @intCast(name.len), | |
| .extra_len = 0, | |
| }; | |
| try writer.writeStruct(hdr, .little); | |
| try writer.writeAll(name); | |
| try writer.writeAll(compressed); | |
| self.offset += @sizeOf(@TypeOf(hdr)) + name.len + compressed.len; | |
| return .{ | |
| .uncompressed_size = @intCast(content.len), | |
| .compressed_size = @intCast(compressed.len), | |
| .compression = compression, | |
| .crc32 = crc32, | |
| .file_offset = current_offset, | |
| .name = name, | |
| }; | |
| } | |
| fn writeCentralRecord(self: *Self, store: FileStore) !void { | |
| if (self.first_central_offset == null) { | |
| self.first_central_offset = self.offset; | |
| } | |
| self.central_count += 1; | |
| const hdr: zip.CentralDirectoryFileHeader = .{ | |
| .signature = zip.central_file_header_sig, | |
| .version_made_by = 0, | |
| .version_needed_to_extract = 10, | |
| .flags = .{ .encrypted = false, ._ = 0 }, | |
| .compression_method = store.compression, | |
| .last_modification_time = 0, | |
| .last_modification_date = 0, | |
| .crc32 = store.crc32, | |
| .compressed_size = store.compressed_size, | |
| .uncompressed_size = @intCast(store.uncompressed_size), | |
| .filename_len = @intCast(store.name.len), | |
| .extra_len = 0, | |
| .comment_len = 0, | |
| .disk_number = 0, | |
| .internal_file_attributes = 0, | |
| .external_file_attributes = 0, | |
| .local_file_header_offset = @intCast(store.file_offset), | |
| }; | |
| try self.writer.writeStruct(hdr, .little); | |
| try self.writer.writeAll(store.name); | |
| self.offset += @sizeOf(@TypeOf(hdr)) + store.name.len; | |
| } | |
| fn writeEndRecord(self: *Self) !void { | |
| const cd_offset = self.first_central_offset orelse 0; | |
| const cd_end = self.offset; | |
| const hdr: zip.EndRecord = .{ | |
| .signature = zip.end_record_sig, | |
| .disk_number = 0, | |
| .central_directory_disk_number = 0, | |
| .record_count_disk = @intCast(self.central_count), | |
| .record_count_total = @intCast(self.central_count), | |
| .central_directory_size = @intCast(cd_end - cd_offset), | |
| .central_directory_offset = @intCast(cd_offset), | |
| .comment_len = 0, | |
| }; | |
| try self.writer.writeStruct(hdr, .little); | |
| } | |
| const std = @import("std"); | |
| const zip = std.zip; | |
| const flate = std.compress.flate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment