【问题标题】:Julia: Extract Zip files within a Zip fileJulia:在 Zip 文件中提取 Zip 文件
【发布时间】:2017-07-03 02:08:53
【问题描述】:

我正在使用 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 文件,因为它无法对其进行搜索。

关于如何做到这一点的任何想法?

【问题讨论】:

  • 我认为您可能必须先解压缩 zip 文件,然后在解压缩后对其进行递归。
  • 我应该解压缩到磁盘文件,还是可以解压缩到内存文件?我是 Julia 新手,不知道如何创建内存文件。
  • 它似乎只适用于文件,但您可以尝试通过将内存 zip 对象包装到类似文件的类中来解决此问题,该类实现 ZipFile 操作 zip 对象所需的方法。但是让我们看看是否有更多 Julia 经验的人可以为您提供更优雅的解决方案。
  • 我问的原因是我对将提取的 zip 文件保存在磁盘上不感兴趣,我只对处理内容感兴趣。我想我必须弄清楚如何将 zip 读入一个文件,比如与 ReadableFile 兼容的内存对象
  • 谢谢,让我们拭目以待。

标签: julia zipfile


【解决方案1】:

一种解决方法是将 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)

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多