From 3863f038661606c9fa7493a7b53074b12502176c Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Thu, 23 Mar 2023 20:02:05 -0400 Subject: [PATCH] easier to use high level function with tar example --- archive.d | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/archive.d b/archive.d index 5e18be2..31c9c7d 100644 --- a/archive.d +++ b/archive.d @@ -121,8 +121,8 @@ enum TarFileType { bool processTar( TarFileHeader* header, long* bytesRemainingOnCurrentFile, - ubyte[] dataBuffer, - scope void delegate(TarFileHeader* header, bool isNewFile, bool fileFinished, ubyte[] data) handleData + const(ubyte)[] dataBuffer, + scope void delegate(TarFileHeader* header, bool isNewFile, bool fileFinished, const(ubyte)[] data) handleData ) { assert(dataBuffer.length == 512); @@ -227,7 +227,10 @@ void decompressLzma(scope void delegate(in ubyte[] chunk) chunkReceiver, scope u compressedData = decoder.unprocessed; while(!decoder.finished) { - chunkReceiver(decoder.processData(chunkBuffer, compressedData)); + auto chunk = decoder.processData(chunkBuffer, compressedData); + + if(chunk.length) + chunkReceiver(chunk); if(decoder.needsMoreData) { import core.stdc.string; @@ -243,6 +246,33 @@ void decompressLzma(scope void delegate(in ubyte[] chunk) chunkReceiver, scope u } } +/// [decompressLzma] and [processTar] can be used together like this: +unittest { + import arsd.archive; + + void main() { + import std.stdio; + auto file = File("test.tar.xz"); + + TarFileHeader tfh; + long size; + ubyte[512] tarBuffer; + + decompressLzma( + (in ubyte[] chunk) => cast(void) processTar(&tfh, &size, chunk, + (header, isNewFile, fileFinished, data) { + if(isNewFile) + writeln("**** " , header.filename, " ", header.size); + //write(cast(string) data); + if(fileFinished) + writeln("+++++++++++++++"); + }), + (ubyte[] buffer) => file.rawRead(buffer), + tarBuffer[] + ); + } +} + /++ A simple .xz file decoder.