Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jul 27, 2024
1 parent f598cf0 commit bf6ca62
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/extract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ const std = @import("std");
const builtin = @import("builtin");
const tools = @import("tools.zig");

const xz = std.compress.xz;
const tar = std.tar;

/// extract tar.xz to dir
pub fn extract_tarxz_to_dir(allocator: std.mem.Allocator, out_dir: std.fs.Dir, file: std.fs.File) !void {
var buffered_reader = std.io.bufferedReader(file.reader());
var decompressed = try std.compress.xz.decompress(allocator, buffered_reader.reader());

var decompressed = try xz.decompress(allocator, buffered_reader.reader());
defer decompressed.deinit();
try std.tar.pipeToFileSystem(out_dir, decompressed.reader(), .{ .mode_mode = .executable_bit_only, .strip_components = 1 });

try tar.pipeToFileSystem(
out_dir,
decompressed.reader(),
.{ .mode_mode = .executable_bit_only, .strip_components = 1 },
);
}

/// extract zip to directory
pub fn extract_zip_dir(out_dir: std.fs.Dir, file: std.fs.File) !void {
var arena = std.heap.ArenaAllocator.init(tools.get_allocator());
defer arena.deinit();

const allocator = arena.allocator();
// for decompressing zig, we need to make a temp directory
const tmp_path = try tools.get_zvm_path_segment(allocator, "tmpdir");
defer std.fs.deleteDirAbsolute(tmp_path) catch unreachable;

try std.fs.makeDirAbsolute(tmp_path);
var tmp_dir = try std.fs.openDirAbsolute(tmp_path, .{ .iterate = true });

// extract zig
try std.zip.extract(tmp_dir, file.seekableStream(), .{});

var iterate = tmp_dir.iterate();
Expand Down

0 comments on commit bf6ca62

Please sign in to comment.