Files
itflow/js/zip_reader.js

126 lines
5.0 KiB
JavaScript

/*
* A .zip read in the browser - the files in it, and each file taken out - for the upload window
* (agent/modals/file/file_upload.php), which unpacks a zip of Markdown pages, an export from
* Notion, Obsidian or a wiki, and sends what's in it up as a folder. Nothing is sent to read it.
*
* A zip keeps its list of files at its end, so only that is read to see what's in it; each file
* is read when it's taken out. Stored and deflated files - what every zip tool writes - are taken
* out, deflated ones through the browser's DecompressionStream. A zip64 (over 4 GB or 65,535
* files) or one with a password isn't.
*/
(function () {
'use strict';
function u16(view, at) {
return view.getUint16(at, true);
}
function u32(view, at) {
return view.getUint32(at, true);
}
function read(file, start, end) {
return file.slice(start, end).arrayBuffer().then(function (buffer) {
return new DataView(buffer);
});
}
// A name is UTF-8 when the zip says so; older tools wrote the computer's own code page
var strictUtf8 = new TextDecoder('utf-8', { fatal: true });
function entryName(view, at, length, utf8) {
var raw = new Uint8Array(view.buffer, view.byteOffset + at, length);
if (utf8) {
return new TextDecoder('utf-8').decode(raw);
}
try {
return strictUtf8.decode(raw);
} catch (e) {
return new TextDecoder('windows-1252').decode(raw);
}
}
/** What's in a zip: [{path, size, packed, method, encrypted, local}], folders left out. Rejects with why it can't be read. */
function list(file) {
var tail = Math.min(file.size, 65535 + 22);
return read(file, file.size - tail, file.size).then(function (view) {
var end = -1;
for (var i = view.byteLength - 22; i >= 0; i--) {
if (u32(view, i) === 0x06054b50) {
end = i;
break;
}
}
if (end < 0) {
throw new Error('it isn\'t a zip');
}
var count = u16(view, end + 10);
var size = u32(view, end + 12);
var offset = u32(view, end + 16);
if (count === 0xFFFF || size === 0xFFFFFFFF || offset === 0xFFFFFFFF) {
throw new Error('it\'s too big to open here');
}
if (offset + size > file.size) {
throw new Error('it isn\'t a complete zip');
}
return read(file, offset, offset + size).then(function (dir) {
var entries = [];
var at = 0;
for (var n = 0; n < count; n++) {
if (at + 46 > dir.byteLength || u32(dir, at) !== 0x02014b50) {
throw new Error('it isn\'t a complete zip');
}
var flags = u16(dir, at + 8);
var nameLength = u16(dir, at + 28);
var path = entryName(dir, at + 46, nameLength, flags & 0x800).replace(/\\/g, '/').replace(/^\/+/, '');
var entry = {
path: path,
method: u16(dir, at + 10),
packed: u32(dir, at + 20),
size: u32(dir, at + 24),
encrypted: (flags & 1) === 1,
local: u32(dir, at + 42)
};
at += 46 + nameLength + u16(dir, at + 30) + u16(dir, at + 32);
if (path !== '' && !/\/$/.test(path)) {
entries.push(entry);
}
}
return entries;
});
});
}
/** One file taken out of the zip, as a File with its own name. */
function unpack(file, entry) {
if (entry.encrypted) {
return Promise.reject(new Error('it has a password'));
}
if (entry.method !== 0 && entry.method !== 8) {
return Promise.reject(new Error('it\'s packed in a way the browser can\'t open'));
}
return read(file, entry.local, entry.local + 30).then(function (head) {
if (u32(head, 0) !== 0x04034b50) {
throw new Error('it isn\'t a complete zip');
}
var start = entry.local + 30 + u16(head, 26) + u16(head, 28);
var packed = file.slice(start, start + entry.packed);
var name = entry.path.split('/').pop();
if (entry.method === 0) {
return new File([packed], name);
}
return new Response(packed.stream().pipeThrough(new DecompressionStream('deflate-raw'))).blob().then(function (blob) {
return new File([blob], name);
});
});
}
var canUnpack = false;
try {
canUnpack = typeof DecompressionStream === 'function' && !!new DecompressionStream('deflate-raw');
} catch (e) {
canUnpack = false;
}
window.itflowZip = { canUnpack: canUnpack, list: list, unpack: unpack };
})();