Skip to content

Commit

Permalink
add zip cmdline tool
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed May 4, 2024
1 parent 8068ac0 commit 3e002f3
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 4 deletions.
10 changes: 10 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ pub fn build(b: *std.Build) !void {
const install = b.addInstallArtifact(unzip, .{});
b.step("unzip", "Build/install the unzip cmdline tool").dependOn(&install.step);
}
{
const zip = b.addExecutable(.{
.name = "zip",
.root_source_file = b.path("zip.zig"),
.target = target,
.optimize = optimize,
});
const install = b.addInstallArtifact(zip, .{});
b.step("zip", "Build/install the zip cmdline tool").dependOn(&install.step);
}
}

fn addTest(
Expand Down
6 changes: 4 additions & 2 deletions lib/zip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub const ExtraHeader = enum(u16) {

const GeneralPurposeFlags = packed struct(u16) {
encrypted: bool,
_: u15,
_0: u2 = 0,
local_header_zero_fields: bool,
_1: u12 = 0,
};

pub const LocalFileHeader = extern struct {
Expand Down Expand Up @@ -192,7 +194,7 @@ pub fn decompress(
return hash.final();
}

fn isBadFilename(filename: []const u8) bool {
pub fn isBadFilename(filename: []const u8) bool {
if (filename.len == 0 or filename[0] == '/')
return true;

Expand Down
4 changes: 2 additions & 2 deletions unzip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn fatal(comptime fmt: []const u8, args: anytype) noreturn {
}

fn usage() noreturn {
std.log.err("Usage: unzip [-d DIR] ZIP_FILE", .{});
std.io.getStdErr().writer().print("Usage: unzip [-d DIR] ZIP_FILE\n", .{}) catch |e| @panic(@errorName(e));
std.process.exit(1);
}

Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn main() !void {
const zip_file = std.fs.cwd().openFile(zip_file_arg, .{}) catch |err|
fatal("open '{s}' failed: {s}", .{zip_file_arg, @errorName(err)});
defer zip_file.close();
try @import("zip.zig").extract(out_dir, zip_file.seekableStream(), .{
try @import("lib/zip.zig").extract(out_dir, zip_file.seekableStream(), .{
.allow_backslashes = true,
});
}
Loading

0 comments on commit 3e002f3

Please sign in to comment.