@@ -51,7 +51,7 @@ export function readZipEntries(zipFile: ZipFile): Promise<ZipEntry[]> {
5151 } ) ;
5252}
5353
54- export function openZipReadStream (
54+ function openZipReadStream (
5555 zipFile : ZipFile ,
5656 entry : ZipEntry ,
5757) : Promise < Readable > {
@@ -86,7 +86,7 @@ export async function openZipBuffer(
8686 } ) ;
8787}
8888
89- export async function copyStream (
89+ async function copyStream (
9090 readable : Readable ,
9191 writeStream : WriteStream ,
9292) : Promise < void > {
@@ -102,6 +102,44 @@ export async function copyStream(
102102 } ) ;
103103}
104104
105+ /**
106+ * Unzips a single file from a zip archive.
107+ *
108+ * @param zipFile
109+ * @param entry
110+ * @param rootDestinationPath
111+ */
112+ export async function unzipFile (
113+ zipFile : ZipFile ,
114+ entry : ZipEntry ,
115+ rootDestinationPath : string ,
116+ ) : Promise < void > {
117+ const path = join ( rootDestinationPath , entry . fileName ) ;
118+
119+ if ( / \/ $ / . test ( entry . fileName ) ) {
120+ // Directory file names end with '/'
121+
122+ await ensureDir ( path ) ;
123+ } else {
124+ // Ensure the directory exists
125+ await ensureDir ( dirname ( path ) ) ;
126+
127+ const readable = await openZipReadStream ( zipFile , entry ) ;
128+
129+ let mode : number | undefined = entry . externalFileAttributes >>> 16 ;
130+ if ( mode <= 0 ) {
131+ mode = undefined ;
132+ }
133+
134+ const writeStream = createWriteStream ( path , {
135+ autoClose : true ,
136+ mode,
137+ } ) ;
138+
139+ await copyStream ( readable , writeStream ) ;
140+ }
141+ }
142+
105143/**
106144 * Sequentially unzips all files from a zip archive. Please use
107145 * `unzipToDirectoryConcurrently` if you can. This function is only
@@ -124,30 +162,7 @@ export async function unzipToDirectorySequentially(
124162 const entries = await readZipEntries ( zipFile ) ;
125163
126164 for ( const entry of entries ) {
127- const path = join ( destinationPath , entry . fileName ) ;
128-
129- if ( / \/ $ / . test ( entry . fileName ) ) {
130- // Directory file names end with '/'
131-
132- await ensureDir ( path ) ;
133- } else {
134- // Ensure the directory exists
135- await ensureDir ( dirname ( path ) ) ;
136-
137- const readable = await openZipReadStream ( zipFile , entry ) ;
138-
139- let mode : number | undefined = entry . externalFileAttributes >>> 16 ;
140- if ( mode <= 0 ) {
141- mode = undefined ;
142- }
143-
144- const writeStream = createWriteStream ( path , {
145- autoClose : true ,
146- mode,
147- } ) ;
148-
149- await copyStream ( readable , writeStream ) ;
150- }
165+ await unzipFile ( zipFile , entry , destinationPath ) ;
151166 }
152167 } finally {
153168 zipFile . close ( ) ;
0 commit comments