so I was wiritng some code to decompress a u8 slice containing gzipped data and with the new Io change in 15.2 I want to see if my soution is....idomatic pub fn decompressGzip(alloc: std.mem.Allocator, compressed: []const u8) ![]u8 { std.debug.print("compressed len: {d}\n", .{compressed.len}); std.debug.print("compressed first bytes: {x}\n", .{compressed[0..4]}); var stream: std.Io.Reader = .fixed(compressed); //create Io.Reader that contains a fixed data buffer var decomp = std.compress.flate.Decompress.init(&stream, .gzip, &.{}); //decomp is the deflated byte buffer no decomp buffer since we auto read into a eriter var writer_alloc: std.Io.Writer.Allocating = .init(alloc);//dynamic buffer to hold the new data errdefer writer_alloc.deinit(); const n = try decomp.reader.streamRemaining(&writer_alloc.writer); //Stream decomp buffer into alloc std.debug.print("bytes streamed: {d}\n", .{n}); return writer_alloc.toOwnedSlice(); //we don't care about the writter anymore so the caller now owns the slice }
Just feel a lilttle weird working with buffers this way and feels really complicated to me
I guess by that same metric is this the right way to read a file contents into a buffer which will be the input for decompress?
const file = try std.fs.openFileAbsolute(path, .{});
defer file.close();
var read_buf: [4096]u8 = undefined;
var file_reader = file.reader(&read_buf);
var writter_alloc = std.Io.Writer.Allocating.init(alloc);
errdefer writter_alloc.deinit();
_ = try file_reader.interface.stream(&writter_alloc.writer, .unlimited);
const buffer = try writter_alloc.toOwnedSlice();
defer alloc.free(buffer);
const decomp = try ird.decompressGzip(alloc, buffer);```