Skip to content
Snippets Groups Projects
Commit df6d49b2 authored by Alexander Rose's avatar Alexander Rose
Browse files

zip utils cleanup

parent 2476bf76
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Alexander Rose <alexander.rose@weirdbyte.de>
*/ */
import { deflate, inflate, parse, encode } from '../zip/zip' import { deflate, inflate, unzip, zip } from '../zip/zip'
describe('zip', () => { describe('zip', () => {
it('roundtrip deflate/inflate', () => { it('roundtrip deflate/inflate', () => {
...@@ -16,11 +16,11 @@ describe('zip', () => { ...@@ -16,11 +16,11 @@ describe('zip', () => {
}) })
it('roundtrip zip', () => { it('roundtrip zip', () => {
const zipped = encode({ const zipped = zip({
'test.foo': new Uint8Array([1, 2, 3, 4, 5, 6, 7]) 'test.foo': new Uint8Array([1, 2, 3, 4, 5, 6, 7])
}) })
console.log(zipped) console.log(zipped)
const unzipped = parse(zipped) const unzipped = unzip(zipped)
console.log(unzipped) console.log(unzipped)
}) })
}) })
\ No newline at end of file
...@@ -8,8 +8,7 @@ ...@@ -8,8 +8,7 @@
*/ */
import { Task, RuntimeContext } from '../mol-task'; import { Task, RuntimeContext } from '../mol-task';
import { parse, ungzip } from './zip/zip'; import { unzip, ungzip } from './zip/zip';
// import { inflate, inflateRaw, parse } from 'uzip-module';
import { utf8Read } from '../mol-io/common/utf8'; import { utf8Read } from '../mol-io/common/utf8';
// polyfill XMLHttpRequest in node.js // polyfill XMLHttpRequest in node.js
...@@ -125,7 +124,7 @@ function decompress(data: Uint8Array, compression: DataCompressionMethod): Uint8 ...@@ -125,7 +124,7 @@ function decompress(data: Uint8Array, compression: DataCompressionMethod): Uint8
case DataCompressionMethod.None: return data case DataCompressionMethod.None: return data
case DataCompressionMethod.Gzip: return ungzip(data) case DataCompressionMethod.Gzip: return ungzip(data)
case DataCompressionMethod.Zip: case DataCompressionMethod.Zip:
const parsed = parse(data.buffer) const parsed = unzip(data.buffer)
const names = Object.keys(parsed) const names = Object.keys(parsed)
if (names.length !== 1) throw new Error('can only decompress zip files with a single entry') if (names.length !== 1) throw new Error('can only decompress zip files with a single entry')
return parsed[names[0]] as Uint8Array return parsed[names[0]] as Uint8Array
......
...@@ -14,7 +14,7 @@ import { crc, adler } from './checksum'; ...@@ -14,7 +14,7 @@ import { crc, adler } from './checksum';
import { _inflate } from './inflate'; import { _inflate } from './inflate';
import { _deflateRaw } from './deflate'; import { _deflateRaw } from './deflate';
export function parse(buf: ArrayBuffer, onlyNames = false) { export function unzip(buf: ArrayBuffer, onlyNames = false) {
const out: { [k: string]: Uint8Array | { size: number, csize: number } } = Object.create(null); const out: { [k: string]: Uint8Array | { size: number, csize: number } } = Object.create(null);
const data = new Uint8Array(buf); const data = new Uint8Array(buf);
let eocd = data.length-4; let eocd = data.length-4;
...@@ -200,7 +200,7 @@ function deflateRaw(data: Uint8Array, opts?: { level: number }) { ...@@ -200,7 +200,7 @@ function deflateRaw(data: Uint8Array, opts?: { level: number }) {
return new Uint8Array(buf.buffer, 0, off); return new Uint8Array(buf.buffer, 0, off);
} }
export function encode(obj: { [k: string]: Uint8Array }, noCmpr = false) { export function zip(obj: { [k: string]: Uint8Array }, noCmpr = false) {
let tot = 0; let tot = 0;
const zpd: { [k: string]: { cpr: boolean, usize: number, crc: number, file: Uint8Array } } = {}; const zpd: { [k: string]: { cpr: boolean, usize: number, crc: number, file: Uint8Array } } = {};
for(const p in obj) { for(const p in obj) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment