Skip to content

Fix zip-glob for files made by CLI #1454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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