1

I'm unzipping hundreds of zipped files with python as explained here.

import os
import zipfile

base_dir = '/users/me/myFile' # absolute path to the data folder
extension = ".zip"

os.chdir(base_dir)  # change directory from working dir to dir with files


def unpack_all_in_dir(_dir):
    for item in os.listdir(_dir):  # loop through items in dir
        abs_path = os.path.join(_dir, item)  # absolute path of dir or file
        if item.endswith(extension):  # check for ".zip" extension
            file_name = os.path.abspath(abs_path)  # get full path of file
            zip_ref = zipfile.ZipFile(file_name)  # create zipfile object
            zip_ref.extractall(_dir)  # extract file to dir
            zip_ref.close()  # close file
        elif os.path.isdir(abs_path):
            unpack_all_in_dir(abs_path)  # recurse this function with inner folder


unpack_all_in_dir(base_dir)

When I unzip a file manually it will get its original modification date, whereas when doing it with code I loose this - the modification date turns into now's date.

Any idea of a way the preserve the original creation date?

enter image description here

Effi Q
  • 21
  • 6
  • please add code samples here, and 2-3 reference .osm files for us to recreate the issue. – Monark Unadkat Dec 30 '21 at 12:17
  • does this help? from what @Expurple wrote it seems this a challenge with all types of files, not only OSM – Effi Q Dec 30 '21 at 12:51
  • Yes you are right, The thread shared by Expurple it's 5 years old and there are no changes made in the zip file implementation. You will have to hack around. The thread has working solutions that you can refer to. – Monark Unadkat Dec 30 '21 at 13:01

2 Answers2

1

I don't know zipfile very well, but according to this thread about modification date, preserving metadata is a pain.

You can hack around with calling some CLI archiving program as a subprocess, but you need to make sure that it's installed on the target system. I actually had to bundle 7zip with my script once, because of some issue with Python libraries, even third-party ones

Expurple
  • 877
  • 6
  • 13
0

Actually opening multiple zip files manually keeps the dates, at least on macOS. Search for the files you need to unzip, select -> open.

Effi Q
  • 21
  • 6