Skip to content

Commit

Permalink
Fix zip-glob for files made by CLI (#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
martindurant committed Dec 11, 2023
1 parent 51027de commit 5d68d27
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions fsspec/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def ls(self, path, detail=True, **kwargs):
if ppath not in paths:
out = {"name": ppath, "size": 0, "type": "directory"}
paths[ppath] = out
out = sorted(paths.values(), key=lambda _: _["name"])
if detail:
out = sorted(paths.values(), key=lambda _: _["name"])
return out
else:
return [f["name"] for f in out]
return sorted(paths)
Binary file added fsspec/implementations/tests/out.zip
Binary file not shown.
12 changes: 12 additions & 0 deletions fsspec/implementations/tests/test_zip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections.abc
import os.path

import pytest

Expand Down Expand Up @@ -95,6 +96,17 @@ def test_zip_glob_star(m):
outfiles = fs.glob("*")
assert len(outfiles) == 1

fs = fsspec.filesystem("zip", fo="memory://out.zip", mode="w")
fs.mkdir("adir")
fs.pipe("adir/afile", b"data")
outfiles = fs.glob("*")
assert len(outfiles) == 1

fn = f"{os.path.dirname(os.path.abspath((__file__)))}/out.zip"
fs = fsspec.filesystem("zip", fo=fn, mode="r")
outfiles = fs.glob("*")
assert len(outfiles) == 1


def test_append(m, tmpdir):
fs = fsspec.filesystem("zip", fo="memory://out.zip", mode="w")
Expand Down
8 changes: 6 additions & 2 deletions fsspec/implementations/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ def _get_dirs(self):
# not read from the file.
files = self.zip.infolist()
self.dir_cache = {
dirname: {"name": dirname, "size": 0, "type": "directory"}
dirname.rstrip("/"): {
"name": dirname.rstrip("/"),
"size": 0,
"type": "directory",
}
for dirname in self._all_dirnames(self.zip.namelist())
}
for z in files:
f = {s: getattr(z, s, None) for s in zipfile.ZipInfo.__slots__}
f.update(
{
"name": z.filename,
"name": z.filename.rstrip("/"),
"size": z.file_size,
"type": ("directory" if z.is_dir() else "file"),
}
Expand Down

0 comments on commit 5d68d27

Please sign in to comment.