11import { Folder , path , knownFolders , File } from '@nativescript/core' ;
22import { UnZipOptions , ZipOptions } from '.' ;
33
4- declare var qos_class_t ;
5-
6- const background_queue = dispatch_get_global_queue ( qos_class_t . QOS_CLASS_DEFAULT , 0 ) ;
7- const main_queue = dispatch_get_current_queue ( ) ;
8-
94const listeners = [ ] ;
105
116export class Zip {
@@ -23,30 +18,27 @@ export class Zip {
2318 return reject ( 'Directory does not exist, invalid directory path: ' + options ?. directory ) ;
2419 }
2520
26- dispatch_async ( background_queue , ( ) => {
27- const archive = options ?. archive ?? path . join ( knownFolders . temp ( ) . path , `${ NSUUID . UUID ( ) . UUIDString } _archive.zip` ) ;
28- this . debug ( 'Zip.zip - folder=' + options ?. directory + '\n' + 'Zip.zip - destinationPath=' + archive ) ;
29- let lastProgressPercent = 0 ;
30- this . debug ( 'Zip.zip - tempFolder= ' + archive ) ;
31- const result = SSZipArchive . createZipFileAtPathWithContentsOfDirectoryKeepParentDirectoryCompressionLevelPasswordAESProgressHandler ( archive , options ?. directory , options ?. keepParent ?? true , - 1 , options ?. password ?? null , true , ( entryNumber , entriesTotal ) => {
32- if ( typeof options ?. onProgress === 'function' && entriesTotal > 0 ) {
33- const percent = Math . floor ( ( entryNumber / entriesTotal ) * 100 ) ;
34- this . debug ( 'ZipWorker - zipProgress= ' + percent ) ;
35- if ( percent !== lastProgressPercent ) {
36- lastProgressPercent = percent ;
37- dispatch_async ( main_queue , ( ) => {
38- options ?. onProgress ( percent ) ;
39- } ) ;
40- }
21+ const archive = options ?. archive ?? path . join ( knownFolders . temp ( ) . path , `${ NSUUID . UUID ( ) . UUIDString } _archive.zip` ) ;
22+ this . debug ( 'Zip.zip - folder=' + options ?. directory + '\n' + 'Zip.zip - destinationPath=' + archive ) ;
23+ let lastProgressPercent = 0 ;
24+ this . debug ( 'Zip.zip - tempFolder= ' + archive ) ;
25+ const result = SSZipArchive . createZipFileAtPathWithContentsOfDirectoryKeepParentDirectoryCompressionLevelPasswordAESProgressHandler ( archive , options ?. directory , options ?. keepParent ?? true , - 1 , options ?. password ?? null , true , ( entryNumber , entriesTotal ) => {
26+ if ( typeof options ?. onProgress === 'function' && entriesTotal > 0 ) {
27+ const percent = Math . floor ( ( entryNumber / entriesTotal ) * 100 ) ;
28+ this . debug ( 'ZipWorker - zipProgress= ' + percent ) ;
29+ if ( percent !== lastProgressPercent ) {
30+ lastProgressPercent = percent ;
31+
32+ options ?. onProgress ( percent ) ;
4133 }
42- } ) ;
43- this . debug ( 'Zip.zip - after create result=' + result ) ;
44- if ( ! result ) {
45- reject ( 'Error creating zip file.' ) ;
46- } else {
47- resolve ( archive ) ;
4834 }
4935 } ) ;
36+ this . debug ( 'Zip.zip - after create result=' + result ) ;
37+ if ( ! result ) {
38+ reject ( 'Error creating zip file.' ) ;
39+ } else {
40+ resolve ( archive ) ;
41+ }
5042 } ) ;
5143 }
5244
@@ -56,39 +48,37 @@ export class Zip {
5648 return reject ( `File does not exist, invalid archive path: ${ options ?. archive } ` ) ;
5749 }
5850 const destination = options ?. directory ?? path . join ( knownFolders . temp ( ) . path , NSUUID . UUID ( ) . UUIDString ) ;
59- dispatch_async ( background_queue , ( ) => {
60- this . debug ( `Zip.unzip - archive=${ options ?. archive } ` ) ;
61- let lastProgressPercent = 0 ;
62- const error = new interop . Pointer ( ) ;
63- const delegate = ( < any > NSObject ) . extend (
64- {
65- zipArchiveProgressEventTotal ( loaded : number , total : number ) : void {
66- if ( typeof options . onProgress === 'function' ) {
67- const percent = Math . floor ( ( loaded / total ) * 100 ) ;
68- if ( percent !== lastProgressPercent ) {
69- lastProgressPercent = percent ;
70- options ?. onProgress ( percent ) ;
71- }
51+ this . debug ( `Zip.unzip - archive=${ options ?. archive } ` ) ;
52+ let lastProgressPercent = 0 ;
53+ const error = new interop . Pointer ( ) ;
54+ const delegate = ( < any > NSObject ) . extend (
55+ {
56+ zipArchiveProgressEventTotal ( loaded : number , total : number ) : void {
57+ if ( typeof options . onProgress === 'function' ) {
58+ const percent = Math . floor ( ( loaded / total ) * 100 ) ;
59+ if ( percent !== lastProgressPercent ) {
60+ lastProgressPercent = percent ;
61+ options ?. onProgress ( percent ) ;
7262 }
73- } ,
63+ }
7464 } ,
75- {
76- protocols : [ SSZipArchiveDelegate ] ,
77- }
78- ) ;
79- const listener = delegate . new ( ) ;
80- listeners . push ( listener ) ;
81- const result = SSZipArchive . unzipFileAtPathToDestinationOverwritePasswordErrorDelegate ( options ?. archive , destination , options ?. overwrite ?? true , options ?. password ?? null , error , listener ) ;
82- const index = listeners . indexOf ( listener ) ;
83- if ( index > - 1 ) {
84- listeners . splice ( index , 1 ) ;
85- }
86- if ( ! result ) {
87- reject ( error ?. value ?. localizedDescription ?? 'Unknown error' ) ;
88- } else {
89- resolve ( destination ) ;
90- }
91- } ) ;
65+ } ,
66+ {
67+ protocols : [ SSZipArchiveDelegate ] ,
68+ } ,
69+ ) ;
70+ const listener = delegate . new ( ) ;
71+ listeners . push ( listener ) ;
72+ const result = SSZipArchive . unzipFileAtPathToDestinationOverwritePasswordErrorDelegate ( options ?. archive , destination , options ?. overwrite ?? true , options ?. password ?? null , error , listener ) ;
73+ const index = listeners . indexOf ( listener ) ;
74+ if ( index > - 1 ) {
75+ listeners . splice ( index , 1 ) ;
76+ }
77+ if ( ! result ) {
78+ reject ( error ?. value ?. localizedDescription ?? 'Unknown error' ) ;
79+ } else {
80+ resolve ( destination ) ;
81+ }
9282 } ) ;
9383 }
9484
0 commit comments