Skip to content

Instantly share code, notes, and snippets.

@bishil06
Created August 6, 2022 08:47
Show Gist options
  • Save bishil06/da0aaf711f0c219001a21055cbe00c36 to your computer and use it in GitHub Desktop.
Save bishil06/da0aaf711f0c219001a21055cbe00c36 to your computer and use it in GitHub Desktop.
web file access api + web stream = unzip application
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 id="selectFile"></h1>
<button id="inputGZ">gz file open</button>
<h1 id="selectSaveDir"></h1>
<button id="inputSaveDir">save ungzip file</button>
<button id="runUGZ">run ungzip</button>
<script src="./main.js"></script>
</body>
</html>
const inputGZ = document.getElementById('inputGZ')
const inputSaveDir = document.getElementById('inputSaveDir')
const runUGZ = document.getElementById('runUGZ')
const selectFile = document.getElementById('selectFile')
const selectSaveDir = document.getElementById('selectSaveDir')
const pickerOpts = {
types: [
{
description: 'gz file',
accept: {
'application/tar+gzip': ['.gz']
}
},
],
excludeAcceptAllOption: true,
multiple: false
};
let fileHandle;
let saveHandle;
inputGZ.addEventListener('click', async (e) => {
[fileHandle] = await window.showOpenFilePicker(pickerOpts);
console.log(fileHandle)
selectFile.innerText = fileHandle.name
})
inputSaveDir.addEventListener('click', async (e) => {
saveHandle = await window.showSaveFilePicker({
suggestedName: fileHandle.name.split('.gz')[0]
});
console.log(saveHandle)
selectSaveDir.innerText = saveHandle.name
})
runUGZ.addEventListener('click', async e => {
if (!fileHandle || !saveHandle) {
return;
}
const file = await fileHandle.getFile()
const rs = file.stream()
const ws = await saveHandle.createWritable();
const ds = new DecompressionStream('gzip');
console.log(file)
console.log(rs)
console.log(ws)
console.log(ds)
rs.pipeThrough(ds).pipeTo(ws)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment