Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Extract Zip files within a Zip file

Tags:

zip

julia

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?

like image 412
Chuck Carlson Avatar asked Jul 03 '17 02:07

Chuck Carlson


People also ask

How do I extract zip files into a zip file?

To unzip (extract) files or folders from a zipped folder To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location.

How do you extract parts of a zip file?

Right-click on any of the zip files that are a part of the collection and click on the "Extract here" or "Extract to folder" option in the pop-up menu. Your zip application will load up and begin decompressing all the files.

Can I extract one file from a zip?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.


1 Answers

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)
like image 146
Chuck Carlson Avatar answered Sep 28 '22 04:09

Chuck Carlson