Created
September 12, 2021 20:24
-
-
Save gildas-lormeau/731485821c9237b638d01bb4074a6369 to your computer and use it in GitHub Desktop.
Basic unzip implementation based on zip.js and Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
~/.deno/bin/deno run --allow-net --allow-write --allow-read --unstable unzip.js "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
/* global Deno, Intl */ | |
"use strict"; | |
import { parse } from "https://deno.land/std/flags/mod.ts"; | |
import { exists } from "https://deno.land/std/fs/mod.ts"; | |
import { basename, dirname } from "https://deno.land/std/path/mod.ts"; | |
import { ZipReader, HttpReader, Uint8ArrayReader, Uint8ArrayWriter, ERR_HTTP_RANGE, terminateWorkers } from "https://raw.githubusercontent.com/gildas-lormeau/zip.js/master/index.js"; | |
unzip(parse(Deno.args)).catch(error => console.error(error.toString())); | |
async function unzip(args) { | |
if (args.l) { | |
await listEntries(args.l || args._[0]); | |
} else { | |
const archive = args._.shift(); | |
if (archive) { | |
await unzipEntries(archive, args._); | |
} | |
} | |
} | |
async function unzipEntries(archive, filenames) { | |
const zipReader = new ZipReader(await getReader(archive)); | |
const entries = await zipReader.getEntries(); | |
let selectedEntries; | |
if (filenames.length) { | |
selectedEntries = entries.filter(entry => filenames.includes(entry.filename)); | |
} else { | |
selectedEntries = entries; | |
} | |
await Promise.all(selectedEntries.map(async entry => { | |
const entryDirectory = dirname(entry.filename); | |
if (!await exists(entryDirectory)) { | |
await Deno.mkdir(entryDirectory, { recursive: true }); | |
} | |
if (!entry.directory) { | |
await Deno.writeFile(entry.filename, await entry.getData(new Uint8ArrayWriter())); | |
} | |
})); | |
await terminateWorkers(); | |
} | |
async function listEntries(archive) { | |
const zipReader = new ZipReader(await getReader(archive)); | |
const entries = await zipReader.getEntries(); | |
let totalSize = 0; | |
console.log("Archive: ", archive); | |
let maxNameLength = 0; | |
const formattedData = entries.map(entry => { | |
const length = formatLength(entry.uncompressedSize); | |
const splitDate = entry.lastModDate.toISOString().split("T"); | |
const date = splitDate[0].padStart(11); | |
const time = splitDate[1].match(/([^:]+:[^:]+):/)[1].padEnd(7); | |
const name = entry.filename; | |
totalSize += entry.uncompressedSize; | |
maxNameLength = Math.max(maxNameLength, length.length); | |
return { length, date, time, name }; | |
}); | |
console.log("Length".padStart(maxNameLength - 1, " "), " Date Time Name"); | |
const lengthSeparator = "-".padStart(maxNameLength, "-"); | |
console.log(lengthSeparator, " ---------- ----- ----"); | |
formattedData.forEach(({ length, date, time, name }) => console.log(length.padStart(maxNameLength), date, time, name)); | |
console.log(lengthSeparator, " ----"); | |
console.log(formatLength(totalSize)); | |
} | |
function formatLength(length) { | |
return new Intl.NumberFormat().format(length); | |
} | |
async function getReader(archive) { | |
if (/^https?:/.test(archive)) { | |
try { | |
return new HttpReader(archive, { useRangeHeader: true, forceRangeRequests: true }); | |
} catch (error) { | |
if (error.message == ERR_HTTP_RANGE) { | |
try { | |
return new HttpReader(archive, { useRangeHeader: true }); | |
} catch (error) { | |
if (error.message == ERR_HTTP_RANGE) { | |
return new HttpReader(archive); | |
} else { | |
throw error; | |
} | |
} | |
} else { | |
throw error; | |
} | |
} | |
} else { | |
if (!basename(archive).includes(".")) { | |
archive += ".zip"; | |
} | |
return new Uint8ArrayReader(await Deno.readFile(archive)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
parse()
is used to parse query parameters, it's a standard module in Deno. When you call the script with "-l", it will list entries of a ZIP read from the filesystem or from a URL.