|
| 1 | +import { Folder, File, path, knownFolders } from '@nativescript/core'; |
| 2 | +import { UnZipOptions, ZipOptions } from '.'; |
| 3 | + |
| 4 | +declare function nszip_zip(directory: string, archive: string, keepParent: number, password: string | null, progress: interop.Pointer, complete: interop.Pointer): void; |
| 5 | +declare function nszip_unzip(archive: string, destination: string, overwrite: number, password: string | null, progress: interop.Pointer, complete: interop.Pointer): void; |
| 6 | +declare const nativescript: { ref(): void; unref(): void }; |
| 7 | + |
| 8 | +function uuid(): string { |
| 9 | + return (globalThis as any).crypto?.randomUUID?.() ?? `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}`; |
| 10 | +} |
| 11 | + |
| 12 | +function run(onProgress: ((percent: number) => void) | undefined, start: (progress: interop.Pointer, complete: interop.Pointer) => void): Promise<void> { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + const native: any = interop; |
| 15 | + const progressFn = (percent: number) => { |
| 16 | + if (typeof onProgress === 'function') { |
| 17 | + onProgress(percent); |
| 18 | + } |
| 19 | + }; |
| 20 | + const completeFn = (error: any) => { |
| 21 | + const failure = error ? native.stringFromCString(error) : null; |
| 22 | + setTimeout(() => { |
| 23 | + native.release(progressFn); |
| 24 | + native.release(completeFn); |
| 25 | + nativescript.unref(); |
| 26 | + }); |
| 27 | + if (failure) { |
| 28 | + reject(failure); |
| 29 | + } else { |
| 30 | + resolve(); |
| 31 | + } |
| 32 | + }; |
| 33 | + const progressPtr = native.callback(progressFn, { returns: native.types.void, args: [native.types.int32] }); |
| 34 | + const completePtr = native.callback(completeFn, { returns: native.types.void, args: [native.types.pointer] }); |
| 35 | + nativescript.ref(); |
| 36 | + start(progressPtr, completePtr); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +export class Zip { |
| 41 | + static debugEnabled = false; |
| 42 | + |
| 43 | + private static debug(arg) { |
| 44 | + if (this.debugEnabled && console && console.log) { |
| 45 | + console.log(arg); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static zip(options: ZipOptions): Promise<string> { |
| 50 | + if (!options?.directory || !Folder.exists(options.directory)) { |
| 51 | + return Promise.reject('Directory does not exist, invalid directory path: ' + options?.directory); |
| 52 | + } |
| 53 | + const archive = options.archive ?? path.join(knownFolders.temp().path, `${uuid()}_archive.zip`); |
| 54 | + const keepParent = (options.keepParent ?? true) ? 1 : 0; |
| 55 | + this.debug('Zip.zip - folder=' + options.directory + '\n' + 'Zip.zip - destinationPath=' + archive); |
| 56 | + return run(options.onProgress, (progress, complete) => nszip_zip(options.directory, archive, keepParent, options.password || null, progress, complete)).then(() => archive); |
| 57 | + } |
| 58 | + |
| 59 | + public static unzip(options: UnZipOptions): Promise<any> { |
| 60 | + if (!options?.archive || !File.exists(options.archive)) { |
| 61 | + return Promise.reject(`File does not exist, invalid archive path: ${options?.archive}`); |
| 62 | + } |
| 63 | + const destination = options.directory ?? path.join(knownFolders.temp().path, uuid()); |
| 64 | + const overwrite = (options.overwrite ?? true) ? 1 : 0; |
| 65 | + this.debug(`Zip.unzip - archive=${options.archive}`); |
| 66 | + return run(options.onProgress, (progress, complete) => nszip_unzip(options.archive, destination, overwrite, options.password || null, progress, complete)).then(() => destination); |
| 67 | + } |
| 68 | +} |
0 commit comments