Julia:如何在一个Zip文件中提取其他的Zip文件?

6
我正在使用Julia的ZipFile包来提取和处理csv文件。没有问题,但是当我遇到一个zip文件中的zip文件时,我想要处理它,但是遇到了错误。
这里是Julia ZipFile文档:https://zipfilejl.readthedocs.io/en/latest/ 以下是代码:
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)

调用 ZipFile.Reader 时出现错误:
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>:?

看起来ZipFile包无法处理一个zip文件中的zip文件,因为它无法对其进行seekend操作。

有什么办法可以解决这个问题吗?


我认为你可能需要先解压缩zip文件,然后在它们被提取后进行递归处理。 - gaborous
我应该解压到磁盘文件还是内存文件?我是Julia的新手,不知道如何创建内存文件。 - Chuck Carlson
似乎它只能在文件上工作,但你可以尝试将内存zip对象包装到实现ZipFile所需方法的类中,以模拟文件。但让我们看看是否有更有经验的Julia程序员能够为您提供更优雅的解决方案。 - gaborous
我问这个问题的原因是我不想在磁盘上保留提取的zip文件,我只对处理内容感兴趣。我想我必须弄清楚如何将zip读入类似于ReadableFile的内存对象中。 - Chuck Carlson
谢谢,让我们拭目以待。 - Chuck Carlson
1个回答

3

解决方法是将zip文件读入IOBuffer。ZipFile.Reader能够处理IOBuffer。以下是可行的代码:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接