Fork me on GitHub
#babashka-sci-dev
<
2022-07-18
borkdude09:07:39

If someone here wants to help with some research: https://github.com/babashka/fs/issues/67 The issue is that fs/zip should be able to add directories while omitting part of the the directory structure. So `foo/bar/ -&gt; bar/ for example. The homework is to find out how other platforms (Ruby, Python, Node and possible other libs do this.

lispyclouds09:07:16

In Python you need to use the lower level zipfile module and recurse down the dir structure and manually supply the filename in the write method: https://docs.python.org/3.10/library/zipfile.html#zipfile.ZipFile.write The dir walk is bit eased with Pathlib:

from pathlib import Path 

def zipdir(parent_dir : str , ziph : ZipFile) -> None:
    
    for file in Path(parent_dir).rglob('*'): # gets all child items
        ziph.write(file, file.name) 
the file.name 2nd arg to ziph.write is whats flattening it

borkdude09:07:45

yeah, this is too hard in Python if you ask me :)

borkdude09:07:55

We could just provide a function to do the renaming

lispyclouds09:07:12

theres no abstracted way to do this in py afaik

lispyclouds09:07:44

maybe an option like {:flatten true} or {:preserve-heirarchy true}

borkdude10:07:45

flatten true would not cover the use case of adding foo/bar/baz and then have bar/baz in the zip

lispyclouds10:07:32

so its more of a selective filter?

lispyclouds10:07:03

hierarchal filter of sorts?

borkdude10:07:29

you don't know this use case? you have a directory src/foo/bar.clj but you would like to have foo/bar.clj in the zip

lispyclouds10:07:30

yeah i get around it with zipping src/* and others manually, havent had a need to just stripping one level yet

borkdude10:07:53

how do you "zip manually"

lispyclouds10:07:35

manually as in add all the needed parts step by step, not specifying what i need in a full dir

lispyclouds10:07:06

(zip archive "src/foo/*") (zip archive "src/bar/*") and more such calls

borkdude10:07:38

this is not using fs/zip which is what the issue is about :)

lispyclouds10:07:05

yep got it, just never thought of a need for this 😛

lispyclouds10:07:07

Ruby has a python like api too, seeing these two i think a :rename-fn would be the most similar

borkdude10:07:26

yeah, seems good to me