You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia>using ZipFile
julia> io = ZipFile.Reader("test/gml/polblogs.zip").files[1]
ZipFile.ReadableFile(name=polblogs.gml, method=Deflate, uncompresssedsize=977839, compressedsize=93369, mtime=1.156468828e9)
julia>readline(io)
"Creator \"Lada Adamic on Tue Aug 15 2006\"\n"
julia>readline(io)
ERROR: SystemError: seek: Bad file descriptor
in seek at ./iostream.jl:49in read at /home/andrew/.julia/v0.4/ZipFile/src/ZipFile.jl:410in readuntil at io.jl:174in readuntil at io.jl:156in readline at io.jl:217
if i do zmore or similar at the command line it has plenty more lines.
am i doing something dumb or is this an issue in your library? i was hoping it would a simple IO instance i could treat like a file (including rewind).
thanks.
edit:
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.4.0-dev+5928 (2015-07-12 04:57 UTC)
_/ |\__'_|_|_|\__'_| | Commit a9e0dd2 (5 days old master)
|__/ | x86_64-suse-linux
ZipFile was latest (Pkg.update()) at time of posting.
[edit2: cut + paste header from wrong julia - this was with 0.4 trunk, as updated above]
Reacted by Jeremiah, Nick Robinson, Sylvan and mattBrzezinski
I have the same problem with v0.8.1, it seems to be related to the finalizer in ZipFile.Reader,
once the Reader reference goes out of scope, the zip file IO stream will be closed and seek fails
this workaround works for me in a similar function: global r = ZipFile.Reader(inputfilename)
Reacted by Jeremiah, Bernhard König, Luis Benet, Jan and Sylvan
I believe I'm facing this issue as well. First you need to keep the dir object assigned before you do the "read", you can't just keep one of the file objects. Furthermore, this dir object apparently has to be in the global scope.
I just ran into this - has there ever been a reliable workaround? I'm doing this:
for zf ∈ zip_files
rf = only(ZipFile.Reader(zf).files)
if rf.name ∉ readdir(zip_folder_path) # Check whether file exists already to avoid duplication
read_file = read(rf)
target_dir = normpath(zip_folder_path, rf.name)
write(target_dir, read_file)
end
end
This randomly errors for some files, but when I run it multiple times I'll eventually get through the whole list of files (I've got 86 zip files in the directory), i.e. there aren't any "bad" files in there.
I could of course just add a try/catch and then wrap the whole loop in a for _ in 1:50; ...; end loop which hopefully successfully unzips all files, but that seems a bit brittle...
your suggested try/catch loop could be improved with an while loop where you check for success for each file to be unzipped. Still brittle, but no need to loop to 50 :)
Just ran into this. I think the problem is the underlying file gets closed if Julia garbage-collects the reader. To ensure that doesn't happen you can use a pattern like this:
rdr = ZipFile.Reader(filename)
# ... do stuff with rdr.filesclose(rdr)
The last call to close is very important as referencing the reader here prevents the compiler/garbage collector from trashing the reader before we get here, as it knows we will still need it at this point for the close call. If you are opening a ZipFile with an existing io object, close() will be a no-op but I think it should still prevent gc.
Reacted by David Horsley, Jason A Eckstein and SWSAmor
I think this could be fixed on the ZipFile side by having a reference to the original Reader inside ReadableFile (and similarly for Writer/WritableFile). That way the Reader can't be gc'ed while there are still ReadableFile instances in scope.
So, is this because the garbage collector throws away the original ZipFile.Reader object? If that's the case, referring to the object in the global scope will be a workaround.
I'm using julia 1.9.2 and ZipFile v0.10.1 .
Here is my sample code that crashes with "seek: Bad file descriptor" while reading from a large-ish file in the zip archive:
using ZipFile
functionopenzipstream()
r = ZipFile.Reader("tmp.zip")
display(r.files)
return r.files[2]
endfunctionprintout()
is =openzipstream()
println(readline(is))
cnt =1for line ineachline(is)
println("$(cnt): $(line)")
cnt +=1endendprintout()
Activity
andrewcooke commentedon Jul 17, 2015
works fine on 0.3 with the same file / machine by the way.
andrewcooke commentedon Jul 17, 2015
well, updating julia to the latest from git seemed to fix this, so i guess it was a problem with trunk 5 days ago!
andrewcooke commentedon Jul 17, 2015
spoke too soon! it now occurs after some random number of readlines, around 2000 or 3000.
(and so does 0.3 if you wait long enough!)
(that's 0.3 from git, not a released 0.3)
jonathanBieler commentedon Oct 24, 2018
I'm getting the same error with this, from
Immerse.jl:Anything wrong here ?
beniacm commentedon May 29, 2019
I have the same problem with v0.8.1, it seems to be related to the
finalizerin ZipFile.Reader,once the Reader reference goes out of scope, the zip file IO stream will be closed and seek fails
this workaround works for me in a similar function:
global r = ZipFile.Reader(inputfilename)sylvaticus commentedon Jul 18, 2019
It seems we have to explicitly close the file.
Would it be possible for Zipfile to support the
doblock like a normal file IO operation ? e.g.:kafisatz commentedon Aug 1, 2020
I just ran into this too.
I was about to compare runtimes of unzipping versus reading from the zip.
Especially when using
@btimethe error occurs.¨The global approach works, but seems suboptimal.
MWE
nlw0 commentedon Aug 15, 2020
I believe I'm facing this issue as well. First you need to keep the
dirobject assigned before you do the "read", you can't just keep one of the file objects. Furthermore, thisdirobject apparently has to be in the global scope.nilshg commentedon Apr 5, 2022
I just ran into this - has there ever been a reliable workaround? I'm doing this:
This randomly errors for some files, but when I run it multiple times I'll eventually get through the whole list of files (I've got 86 zip files in the directory), i.e. there aren't any "bad" files in there.
I could of course just add a
try/catchand then wrap the whole loop in afor _ in 1:50; ...; endloop which hopefully successfully unzips all files, but that seems a bit brittle...kafisatz commentedon May 6, 2022
your suggested try/catch loop could be improved with an while loop where you check for success for each file to be unzipped. Still brittle, but no need to loop to 50 :)
mattwigway commentedon May 24, 2022
Just ran into this. I think the problem is the underlying file gets closed if Julia garbage-collects the reader. To ensure that doesn't happen you can use a pattern like this:
The last call to
closeis very important as referencing the reader here prevents the compiler/garbage collector from trashing the reader before we get here, as it knows we will still need it at this point for the close call. If you are opening a ZipFile with an existing io object, close() will be a no-op but I think it should still prevent gc.close zipfile when done to avoid early gc, see fhs/ZipFile.jl#14
mattwigway commentedon May 24, 2022
I think this could be fixed on the ZipFile side by having a reference to the original Reader inside ReadableFile (and similarly for Writer/WritableFile). That way the Reader can't be gc'ed while there are still ReadableFile instances in scope.
ryofurue commentedon Aug 2, 2023
So, is this because the garbage collector throws away the original ZipFile.Reader object? If that's the case, referring to the object in the global scope will be a workaround.
I'm using julia 1.9.2 and ZipFile v0.10.1 .
Here is my sample code that crashes with "seek: Bad file descriptor" while reading from a large-ish file in the zip archive:
Bugfix getting garbage collected