gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.33k stars 506 forks source link

extract `lastModified` property from zip entries #461

Closed bwkam closed 8 months ago

bwkam commented 8 months ago

Hi, I recently discovered this library and it seems pretty good. I was just wondering if I can access the lastModified property from files in the zip because I need that info in my app.

Thanks.

gildas-lormeau commented 8 months ago

The last modification date is accessible via the Entry#lastModDate property. See the example below.

<!doctype html>
<html>

<head>
  <title>Display last modification dates of entries in a zip file</title>
  <style>
    table {
      margin-block-start: 8px;
      border-collapse: collapse;
    }
    td {
      border: 1px dotted grey;
      padding: 4px;
    }
  </style>
</head>

<body>
  <form>
    <input type=file id=zipFileInput>
  </form>
  <div id=result>
  </div>
  <script type=module>

import { ZipReader, BlobReader, BlobWriter } from "https://unpkg.com/@zip.js/zip.js/index.js";

zipFileInput.onchange = () => displayImages().catch(error => alert(error));

async function displayImages() {
  const zipReader = new ZipReader(new BlobReader(zipFileInput.files[0]));
  const entries = await zipReader.getEntries();
  const tableElement = document.createElement("table");
  entries.forEach(entry => {
    const rowElement = document.createElement("tr");
    const filenameElement = document.createElement("td");
    const lastModDateElement = document.createElement("td");
    filenameElement.textContent = entry.filename;
    lastModDateElement.textContent = entry.lastModDate;
    rowElement.appendChild(filenameElement);
    rowElement.appendChild(lastModDateElement);
    tableElement.appendChild(rowElement);
  });
  result.innerHTML = "";
  result.appendChild(tableElement);
}

  </script>
</body>

</html>

Run it on plnkr: https://plnkr.co/edit/X1MwY0hbk2aCxOcM