|
1 | 1 | //! Archive extraction for .tar.xz and .zip files. |
2 | | -//! Uses the `tar` CLI for .tar.xz (stdlib xz has compile issues in Zig 0.15.2) |
| 2 | +//! Uses the `tar` CLI for .tar.xz (stdlib xz has compile issues in Zig 0.16.0) |
3 | 3 | //! and std.zip for .zip archives. |
4 | 4 |
|
5 | 5 | const std = @import("std"); |
6 | 6 | const builtin = @import("builtin"); |
7 | 7 |
|
8 | 8 | /// Extract a .tar.xz archive using the system `tar` command. |
9 | | -/// This is the primary extraction method on Unix platforms. |
10 | | -pub fn extractTarXz(allocator: std.mem.Allocator, archive_path: []const u8, output_dir: []const u8) !void { |
11 | | - const result = try std.process.Child.run(.{ |
12 | | - .allocator = allocator, |
| 9 | +pub fn extractTarXz(allocator: std.mem.Allocator, io: std.Io, archive_path: []const u8, output_dir: []const u8) !void { |
| 10 | + const result = try std.process.run(allocator, io, .{ |
13 | 11 | .argv = &.{ "tar", "-xf", archive_path, "-C", output_dir }, |
14 | 12 | }); |
15 | 13 | defer allocator.free(result.stdout); |
16 | 14 | defer allocator.free(result.stderr); |
17 | 15 |
|
18 | | - if (result.term != .Exited or result.term.Exited != 0) { |
| 16 | + if (result.term != .exited or result.term.exited != 0) { |
19 | 17 | return error.ExtractionFailed; |
20 | 18 | } |
21 | 19 | } |
22 | 20 |
|
23 | 21 | /// Extract a .zip archive using Zig's stdlib zip implementation. |
24 | | -pub fn extractZip(allocator: std.mem.Allocator, archive_path: []const u8, output_dir: []const u8) !void { |
| 22 | +pub fn extractZip(allocator: std.mem.Allocator, io: std.Io, archive_path: []const u8, output_dir: []const u8) !void { |
25 | 23 | _ = output_dir; |
26 | | - const file = try std.fs.cwd().openFile(archive_path, .{}); |
27 | | - defer file.close(); |
| 24 | + const file = try std.Io.Dir.cwd().openFile(io, archive_path, .{}); |
| 25 | + defer file.close(io); |
28 | 26 |
|
29 | 27 | var reader_buf: [8192]u8 = undefined; |
30 | | - var reader = file.reader(&reader_buf); |
| 28 | + var reader = file.reader(io, &reader_buf); |
31 | 29 |
|
32 | 30 | var diagnostics: std.zip.Diagnostics = .{ .allocator = allocator }; |
33 | 31 | defer diagnostics.deinit(); |
34 | 32 |
|
35 | | - try std.zip.extract(std.fs.cwd(), &reader, .{ |
| 33 | + try std.zip.extract(std.Io.Dir.cwd(), &reader, .{ |
36 | 34 | .diagnostics = &diagnostics, |
37 | 35 | }); |
38 | 36 | } |
39 | 37 |
|
40 | 38 | /// Extract an archive based on its file extension. |
41 | 39 | /// Supports: .tar.xz, .zip, .tar |
42 | | -pub fn extractArchive(allocator: std.mem.Allocator, archive_path: []const u8, output_dir: []const u8) !void { |
| 40 | +pub fn extractArchive(allocator: std.mem.Allocator, io: std.Io, archive_path: []const u8, output_dir: []const u8) !void { |
43 | 41 | if (std.mem.endsWith(u8, archive_path, ".tar.xz")) { |
44 | | - try extractTarXz(allocator, archive_path, output_dir); |
| 42 | + try extractTarXz(allocator, io, archive_path, output_dir); |
45 | 43 | } else if (std.mem.endsWith(u8, archive_path, ".zip")) { |
46 | | - try extractZip(allocator, archive_path, output_dir); |
| 44 | + try extractZip(allocator, io, archive_path, output_dir); |
47 | 45 | } else if (std.mem.endsWith(u8, archive_path, ".tar")) { |
48 | 46 | // Plain tar using CLI |
49 | | - const result = try std.process.Child.run(.{ |
50 | | - .allocator = allocator, |
| 47 | + const result = try std.process.run(allocator, io, .{ |
51 | 48 | .argv = &.{ "tar", "-xf", archive_path, "-C", output_dir }, |
52 | 49 | }); |
53 | 50 | defer allocator.free(result.stdout); |
54 | 51 | defer allocator.free(result.stderr); |
55 | 52 |
|
56 | | - if (result.term != .Exited or result.term.Exited != 0) { |
| 53 | + if (result.term != .exited or result.term.exited != 0) { |
57 | 54 | return error.ExtractionFailed; |
58 | 55 | } |
59 | 56 | } else { |
|
0 commit comments