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

How to get the name from an index? #12

Open
nhz2 opened this issue Mar 17, 2025 · 3 comments
Open

How to get the name from an index? #12

nhz2 opened this issue Mar 17, 2025 · 3 comments

Comments

@nhz2
Copy link

nhz2 commented Mar 17, 2025

If I have ZipArchive and an index, how can I get the entry name as a String?

I am currently using:

name = unsafe_string(LibZip.zip_get_file_info(archive, index).name)

But this seems to be causing segmentation faults in JuliaIO/ZipArchives.jl#89

Is there a less unsafe way to do this?

To clarify, I want the raw bytes of the entry name, I don't want any "automagical" encoding conversions.

@AlexKlo
Copy link
Contributor

AlexKlo commented Mar 27, 2025

Hi!
You can obtain a Ptr{UInt8} using libzip_get_name from LibZip.jl and then convert it as needed.

using LibZip

archive = ZipArchive(; flags = LIBZIP_CREATE)

write(archive, "greetings.txt", b"Hello, from LibZip!")

name_ptr = libzip_get_name(archive.archive_ptr, 0, 0)

julia> unsafe_string(name_ptr)
"greetings.txt"

@AlexKlo
Copy link
Contributor

AlexKlo commented Mar 27, 2025

I noticed that there are faults in JuliaIO/ZipArchives.jl#89 on a 32-bit system with Ubuntu and on Windows.

It seems the issue is with the structure:

mutable struct LibZipStatT
    valid::Int64
    name::Ptr{UInt8}
    index::Int64
    size::Int64
    comp_size::Int64
    time::Int64
    crc::UInt32
    comp_method::UInt16
    encryption_method::UInt16
    flags::Int32

    LibZipStatT() = new(0, Ptr{UInt8}(), 0, 0, 0, 0, 0, 0, 0, 0)
end

The field types need to be correctly defined for different systems.

@gryumov gryumov linked a pull request Mar 28, 2025 that will close this issue
4 tasks
@gryumov gryumov removed a link to a pull request Mar 28, 2025
4 tasks
@nhz2
Copy link
Author

nhz2 commented Mar 31, 2025

Yes, not just this struct; the other functions need to be fixed to match https://github.com/nih-at/libzip/blob/main/lib/zip.h

LibZip.jl/src/LibZip.jl

Lines 384 to 386 in bfa63f4

function libzip_source_stat(source, sb)
return ccall((:zip_source_stat, libzip), Int64, (Ptr{LibZipSourceT}, Ptr{LibZipStatT}), source, sb)
end

For example based on: https://github.com/nih-at/libzip/blob/e9083a7b41463489cd6641310c36f3b9197e1d27/lib/zip.h#L499
Should be changed to:

function libzip_source_stat(source, sb) 
    return ccall((:zip_source_stat, libzip), Cint, (Ptr{LibZipSourceT}, Ref{LibZipStatT}), source, sb) 
end 

You may want to check out https://github.com/JuliaInterop/Clang.jl to help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants