Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base.parent(r::ZipReader) to get underlying buffer #72

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ end
struct ZipReader{T<:AbstractVector{UInt8}}
ZipReader(buffer::AbstractVector{UInt8})

Create a reader for a zip archive in `buffer`.
View the bytes in `buffer` as a ZIP archive.

The array must not be modified while being read.

`zip_nentries(r::ZipReader)::Int` returns the
number of entries in the archive.
`zip_nentries(r::ZipReader)::Int` returns the
number of entries in the archive.

`zip_names(r::ZipReader)::Vector{String}` returns the names of all the entries in the archive.

Expand All @@ -666,12 +666,12 @@ Entries are indexed from `1:zip_nentries(r)`

`zip_openentry` and `zip_readentry` can be used to read data from an entry.

A `ZipReader` object does not need to be closed, and cannot be closed.
The `parent` function can be used to get the underlying buffer.

# Multi threading

The returned `ZipReader` object can safely be used from multiple threads;
however, the streams returned by `zip_openentry`
The returned `ZipReader` object can safely be used from multiple threads;
however, the streams returned by `zip_openentry`
should only be accessed by one thread at a time.
"""
function ZipReader(buffer::AbstractVector{UInt8})
Expand Down Expand Up @@ -712,6 +712,8 @@ function Base.show(io::IO, ::MIME"text/plain", r::ZipReader)
end
end

Base.parent(r::ZipReader) = r.buffer

"""
zip_entry_data_offset(r::ZipReader, i::Integer)::Int64

Expand Down
1 change: 1 addition & 0 deletions test/test_simple-usage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ using Test: @testset, @test, @test_throws
# Don't modify `data` while reading it through a ZipReader.
r = ZipReader(data)
zip_nentries(r) == 3
@test parent(r) === data

@test zip_names(r) == ["test/test1.txt", "test/empty.txt", "test/test2.txt", "test/compressed.txt"]
@test zip_name(r, 3) == "test/test2.txt"
Expand Down
Loading