Function decompress [src]

Decompresses the given data from reader into writer. Stops early if more than uncompressed_size bytes are processed and verifies that exactly that number of bytes are decompressed. Returns the CRC-32 of the uncompressed data. writer can be anything with a writeAll(self: *Self, chunk: []const u8) anyerror!void method.

Prototype

pub fn decompress( method: CompressionMethod, uncompressed_size: u64, reader: anytype, writer: anytype, ) !u32

Parameters

method: CompressionMethoduncompressed_size: u64

Source

pub fn decompress( method: CompressionMethod, uncompressed_size: u64, reader: anytype, writer: anytype, ) !u32 { var hash = std.hash.Crc32.init(); var total_uncompressed: u64 = 0; switch (method) { .store => { var buf: [4096]u8 = undefined; while (true) { const len = try reader.read(&buf); if (len == 0) break; try writer.writeAll(buf[0..len]); hash.update(buf[0..len]); total_uncompressed += @intCast(len); } }, .deflate => { var br = std.io.bufferedReader(reader); var decompressor = std.compress.flate.decompressor(br.reader()); while (try decompressor.next()) |chunk| { try writer.writeAll(chunk); hash.update(chunk); total_uncompressed += @intCast(chunk.len); if (total_uncompressed > uncompressed_size) return error.ZipUncompressSizeTooSmall; } if (br.end != br.start) return error.ZipDeflateTruncated; }, _ => return error.UnsupportedCompressionMethod, } if (total_uncompressed != uncompressed_size) return error.ZipUncompressSizeMismatch; return hash.final(); }