Skip to contents

archive_stream() inflates one member and calls callback with each chunk of its bytes, in order, so a member larger than memory can be processed. The member is found and checked as archive_read() does.

Usage

archive_stream(z, name, callback, chunk_size = 65536)

Arguments

z

A handle from archive_open().

name

The member's name: a single string, compared with the members' names after conversion to UTF-8.

callback

A function called as callback(chunk) for each chunk, chunk being a raw vector. Its value is ignored.

chunk_size

The size of each chunk in bytes, the last excepted. A whole number from 1 to .Machine$integer.max.

Value

The number of bytes handed to callback, invisibly.

Details

Chunks are handed over as they are inflated, so the CRC-32 can only be compared after the last one: when it does not match, callback has already seen every byte of the member, and the error's delivered field says how many. A member that is corrupt part-way stops at the first corrupt byte, and delivered counts the bytes handed over until then; it never exceeds the size the archive declares. Every chunk but the last of a complete member has exactly chunk_size bytes, so the bytes inflated after the last whole chunk and before the corrupt byte are not handed over.

A Compound File stream is stored, not compressed, and has no CRC-32. Its whole sector chain is checked before the first chunk, so a broken stream raises an error with delivered 0 and callback is never called. A tar member is stored too, and its data was found whole when the archive was opened; a file that shrinks while it is read stops at the last whole chunk. A compressed tar's whole stream is decoded and checked when the archive is opened, so a corrupt one raises an error with delivered 0 before callback is called.

An error raised by callback stops the stream and is passed on as it is; the archive is closed on the way out, so the handle can be read again at once.

Conditions

Those of archive_read(), each with delivered, the number of bytes callback received before the failure, and:

  • zuarchive_error_limit: chunk_size is not a whole number from 1 to .Machine$integer.max.

  • zuarchive_error: callback is not a function.

See also

Examples

z <- archive_open(system.file("extdata", "hello.zip", package = "zuarchive"))
archive_stream(z, "hello.txt", function(chunk) print(rawToChar(chunk)),
  chunk_size = 2)
#> [1] "he"
#> [1] "ll"
#> [1] "o"