(in_dir: str, zip_fname: str, date_time: Optional[datetime] = None)
Make a zip file `zip_fname` with contents of directory `in_dir`

    The recorded filenames are relative to `in_dir`, so doing a standard zip
    unpack of the resulting `zip_fname` in an empty directory will result in
    the original directory contents.

    Parameters
    ----------
    in_dir : str
        Directory path containing files to go in the zip archive
    zip_fname : str
        Filename of zip archive to write
    date_time : Optional[datetime]
        Time stamp to set on each file in the archive
Make a zip file `zip_fname` with contents of directory `in_dir`
Make a zip file zip_fname with contents of directory in_dir
def dir2zip(in_dir: str, zip_fname: str, date_time: Optional[datetime] = None) -> None:
    """Make a zip file `zip_fname` with contents of directory `in_dir`

    The recorded filenames are relative to `in_dir`, so doing a standard zip
    unpack of the resulting `zip_fname` in an empty directory will result in
    the original directory contents.

    Parameters
    ----------
    in_dir : str
        Directory path containing files to go in the zip archive
    zip_fname : str
        Filename of zip archive to write
    date_time : Optional[datetime]
        Time stamp to set on each file in the archive
    """
    if date_time is None:
        st = os.stat(in_dir)
        date_time = datetime.fromtimestamp(st.st_mtime, tz=timezone.utc)
    date_time_args = date_time.timetuple()[:6]
    with zipfile.ZipFile(zip_fname, "w", compression=zipfile.ZIP_DEFLATED) as z:
        for root, dirs, files in os.walk(in_dir):
            for dir in dirs:
                dname = os.path.join(root, dir)
                out_dname = os.path.relpath(dname, in_dir) + "/"
                zinfo = zipfile.ZipInfo(out_dname, date_time=date_time_args)
                zinfo.external_attr = os.stat(dname).st_mode << 16
                z.writestr(zinfo, "")
            for file in files:
                fname = os.path.join(root, file)
                out_fname = os.path.relpath(fname, in_dir)
                zinfo = zipfile.ZipInfo(out_fname, date_time=date_time_args)
                zinfo.external_attr = os.stat(fname).st_mode << 16
                with open(fname, "rb") as fp:
                    z.writestr(zinfo, fp.read())
def dir2zip ( in_dir : str , zip_fname : str , date_time : Optional [ datetime ] = None ) -> None : if date_time is None : st = os . stat ( in_dir ) date_time = datetime . fromtimestamp ( st . st_mtime , tz = timezone . utc ) date_time_args = date_time . timetuple ( ) [ : 6 ] with zipfile . ZipFile ( zip_fname , "w" , compression = zipfile . ZIP_DEFLATED ) as z : for root , dirs , files in os . walk ( in_dir ) : for dir in dirs : dname = os . path . join ( root , dir ) out_dname = os . path . relpath ( dname , in_dir ) + "/" zinfo = zipfile . ZipInfo ( out_dname , date_time = date_time_args ) zinfo . external_attr = os . stat ( dname ) . st_mode << 16 z . writestr ( zinfo , "" ) for file in files : fname = os . path . join ( root , file ) out_fname = os . path . relpath ( fname , in_dir ) zinfo = zipfile . ZipInfo ( out_fname , date_time = date_time_args ) zinfo . external_attr = os . stat ( fname ) . st_mode << 16 with open ( fname , "rb" ) as fp : z . writestr ( zinfo , fp . read ( ) )