@@ -97,6 +97,13 @@ async function copyStream(
9797 } ) ;
9898}
9999
100+ type UnzipProgress = {
101+ filesExtracted : number ;
102+ totalFiles : number ;
103+ } ;
104+
105+ export type UnzipProgressCallback = ( progress : UnzipProgress ) => void ;
106+
100107/**
101108 * Unzips a single file from a zip archive.
102109 *
@@ -143,10 +150,12 @@ async function unzipFile(
143150 * @param archivePath
144151 * @param destinationPath
145152 * @param taskRunner A function that runs the tasks (either sequentially or concurrently).
153+ * @param progress
146154 */
147155export async function unzipToDirectory (
148156 archivePath : string ,
149157 destinationPath : string ,
158+ progress : UnzipProgressCallback | undefined ,
150159 taskRunner : ( tasks : Array < ( ) => Promise < void > > ) => Promise < void > ,
151160) : Promise < void > {
152161 const zipFile = await openZip ( archivePath , {
@@ -158,8 +167,23 @@ export async function unzipToDirectory(
158167 try {
159168 const entries = await readZipEntries ( zipFile ) ;
160169
170+ let filesExtracted = 0 ;
171+ const totalFiles = entries . length ;
172+
173+ const reportProgress = ( ) => {
174+ progress ?.( {
175+ filesExtracted,
176+ totalFiles,
177+ } ) ;
178+ } ;
179+
161180 await taskRunner (
162- entries . map ( ( entry ) => ( ) => unzipFile ( zipFile , entry , destinationPath ) ) ,
181+ entries . map ( ( entry ) => async ( ) => {
182+ await unzipFile ( zipFile , entry , destinationPath ) ;
183+
184+ filesExtracted ++ ;
185+ reportProgress ( ) ;
186+ } ) ,
163187 ) ;
164188 } finally {
165189 zipFile . close ( ) ;
@@ -173,14 +197,21 @@ export async function unzipToDirectory(
173197 *
174198 * @param archivePath
175199 * @param destinationPath
200+ * @param progress
176201 */
177202export async function unzipToDirectorySequentially (
178203 archivePath : string ,
179204 destinationPath : string ,
205+ progress ?: UnzipProgressCallback ,
180206) : Promise < void > {
181- return unzipToDirectory ( archivePath , destinationPath , async ( tasks ) => {
182- for ( const task of tasks ) {
183- await task ( ) ;
184- }
185- } ) ;
207+ return unzipToDirectory (
208+ archivePath ,
209+ destinationPath ,
210+ progress ,
211+ async ( tasks ) => {
212+ for ( const task of tasks ) {
213+ await task ( ) ;
214+ }
215+ } ,
216+ ) ;
186217}
0 commit comments