6

I'm using Julia's ZipFile package to extract and process csv files. No problem, but when I encounter a zip file within the zip file, I'd like to process that as well, but am encountering an error.

Julia ZipFile docs are here: https://zipfilejl.readthedocs.io/en/latest/

Here's the code:

using ZipFile
using DataFrames
function process_zip(zip::ZipFile.ReadableFile)

    if split(zip.name,".")[end] == "zip"

        r = ZipFile.Reader(zip) #error: MethodError: no method matching seekend(::ZipFile.ReadableFile)

        for f in r.files
            process_zip(f) 
        end
    end

    if split(zip.name,".")[end] == "csv"
         df = readtable(zip) #for now just read it into a dataframe
    end

end

r = ZipFile.Reader("yourzipfilepathhere");

for f in r.files
    process_zip(f)
end
close(r)

The call to ZipFile.Reader gives the error:

MethodError: no method matching seekend(::ZipFile.ReadableFile)
Closest candidates are:
  seekend(::Base.Filesystem.File) at filesystem.jl:191
  seekend(::IOStream) at iostream.jl:57
  seekend(::Base.AbstractIOBuffer) at iobuffer.jl:178
  ...

Stacktrace:
 [1] _find_enddiroffset(::ZipFile.ReadableFile) at /home/chuck/.julia/v0.6/ZipFile/src/ZipFile.jl:259
 [2] ZipFile.Reader(::ZipFile.ReadableFile, ::Bool) at /home/chuck/.julia/v0.6/ZipFile/src/ZipFile.jl:104
 [3] process_zip(::ZipFile.ReadableFile) at ./In[27]:7
 [4] macro expansion at ./In[27]:18 [inlined]
 [5] anonymous at ./<missing>:?

So it seems ZipFile package cannot process a zip file from a zip file as it cannot do a seekend on it.

Any ideas on how to do this?

5
  • I think you will probably have to first unzip the zip files and then recurse on them after they are extracted.
    – gaborous
    Jul 3, 2017 at 2:10
  • Should I unzip to a diskfile, or can I unzip to a memory file? I'm new to Julia and I don't know how to create a memory file. Jul 3, 2017 at 2:12
  • It seems like it works only on files, but you might try to get around that by wrapping your memory zip object into a file-like class implementing the required methods for ZipFile to manipulate the zip object. But let's see if someone with more experience with Julia can provide you with a more elegant solution.
    – gaborous
    Jul 3, 2017 at 2:17
  • The reason I ask is that I'm not interested in keeping the extracted zip file on disk, I'm only interested in processing the contents. I guess I'll have to figure out how to read the zip into a file like memory object compatable with ReadableFile Jul 3, 2017 at 2:17
  • Thanks, lets wait and see. Jul 3, 2017 at 2:18

1 Answer 1

3

A workaround is to read the zip file into an IOBuffer. ZipFile.Reader is able to process the IOBuffer. Here is the working code:

using ZipFile
using DataFrames
function process_zip(zip::ZipFile.ReadableFile)

    if split(zip.name,".")[end] == "zip"

        iobuffer = IOBuffer(readstring(zip))
        r = ZipFile.Reader(iobuffer)

        for f in r.files
            process_zip(f) 
        end
    end

    if split(zip.name,".")[end] == "csv"
         df = readtable(zip) #for now just read it into a dataframe
    end

end

r = ZipFile.Reader("yourzipfilepathhere");

for f in r.files
    process_zip(f)
end
close(r)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.