22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5- import admZip from 'adm-zip'
65import * as vscode from 'vscode'
76import path from 'path'
87import { tempDirPath , testGenerationLogsDir } from '../../shared/filesystemUtilities'
@@ -25,6 +24,7 @@ import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/proces
2524import { ProjectZipError } from '../../amazonqTest/error'
2625import { removeAnsi } from '../../shared/utilities/textUtilities'
2726import { normalize } from '../../shared/utilities/pathUtils'
27+ import { ZipStream } from '../../shared/utilities/zipStream'
2828
2929export interface ZipMetadata {
3030 rootDir : string
@@ -109,7 +109,7 @@ export class ZipUtil {
109109 if ( ! uri ) {
110110 throw new NoActiveFileError ( )
111111 }
112- const zip = new admZip ( )
112+ const zip = new ZipStream ( )
113113
114114 const content = await this . getTextContent ( uri )
115115
@@ -121,14 +121,14 @@ export class ZipUtil {
121121 // Set includeWorkspaceFolder to false because we are already manually prepending the projectName
122122 const relativePath = vscode . workspace . asRelativePath ( uri , false )
123123 const zipEntryPath = this . getZipEntryPath ( projectName , relativePath )
124- zip . addFile ( zipEntryPath , Buffer . from ( content , 'utf-8' ) )
124+ zip . writeString ( content , zipEntryPath )
125125
126126 if ( scope === CodeWhispererConstants . CodeAnalysisScope . FILE_ON_DEMAND ) {
127127 const gitDiffContent = `+++ b/${ normalize ( zipEntryPath ) } ` // Sending file path in payload for LLM code review
128- zip . addFile ( ZipConstants . codeDiffFilePath , Buffer . from ( gitDiffContent , 'utf-8' ) )
128+ zip . writeString ( gitDiffContent , ZipConstants . codeDiffFilePath )
129129 }
130130 } else {
131- zip . addFile ( uri . fsPath , Buffer . from ( content , 'utf-8' ) )
131+ zip . writeString ( content , uri . fsPath )
132132 }
133133
134134 this . _pickedSourceFiles . add ( uri . fsPath )
@@ -139,7 +139,7 @@ export class ZipUtil {
139139 throw new FileSizeExceededError ( )
140140 }
141141 const zipFilePath = this . getZipDirPath ( FeatureUseCase . CODE_SCAN ) + CodeWhispererConstants . codeScanZipExt
142- zip . writeZip ( zipFilePath )
142+ await zip . finalizeToFile ( zipFilePath )
143143 return zipFilePath
144144 }
145145
@@ -170,13 +170,13 @@ export class ZipUtil {
170170 * await processMetadataDir(zip, '/path/to/directory');
171171 * ```
172172 */
173- protected async processMetadataDir ( zip : admZip , metadataDir : string ) {
173+ protected async processMetadataDir ( zip : ZipStream , metadataDir : string ) {
174174 const metadataDirName = path . basename ( metadataDir )
175175 // Helper function to add empty directory to zip
176176 const addEmptyDirectory = ( dirPath : string ) => {
177177 const relativePath = path . relative ( metadataDir , dirPath )
178178 const pathWithMetadata = path . join ( metadataDirName , relativePath , '/' )
179- zip . addFile ( pathWithMetadata , Buffer . from ( '' ) )
179+ zip . writeString ( '' , pathWithMetadata )
180180 }
181181
182182 // Recursive function to process directories
@@ -189,11 +189,10 @@ export class ZipUtil {
189189
190190 if ( fileType === vscode . FileType . File ) {
191191 try {
192- const fileContent = await vscode . workspace . fs . readFile ( vscode . Uri . file ( filePath ) )
193- const buffer = Buffer . from ( fileContent )
192+ const fileUri = vscode . Uri . file ( filePath )
194193 const relativePath = path . relative ( metadataDir , filePath )
195194 const pathWithMetadata = path . join ( metadataDirName , relativePath )
196- zip . addFile ( pathWithMetadata , buffer )
195+ zip . writeFile ( fileUri . fsPath , pathWithMetadata )
197196 } catch ( error ) {
198197 getLogger ( ) . error ( `Failed to add file ${ filePath } to zip: ${ error } ` )
199198 }
@@ -207,7 +206,7 @@ export class ZipUtil {
207206 }
208207
209208 protected async zipProject ( useCase : FeatureUseCase , projectPath ?: string , metadataDir ?: string ) {
210- const zip = new admZip ( )
209+ const zip = new ZipStream ( )
211210 let projectPaths = [ ]
212211 if ( useCase === FeatureUseCase . TEST_GENERATION && projectPath ) {
213212 projectPaths . push ( projectPath )
@@ -232,12 +231,12 @@ export class ZipUtil {
232231 }
233232 this . _language = [ ...languageCount . entries ( ) ] . reduce ( ( a , b ) => ( b [ 1 ] > a [ 1 ] ? b : a ) ) [ 0 ]
234233 const zipFilePath = this . getZipDirPath ( useCase ) + CodeWhispererConstants . codeScanZipExt
235- zip . writeZip ( zipFilePath )
234+ await zip . finalizeToFile ( zipFilePath )
236235 return zipFilePath
237236 }
238237
239238 protected async processCombinedGitDiff (
240- zip : admZip ,
239+ zip : ZipStream ,
241240 projectPaths : string [ ] ,
242241 filePath ?: string ,
243242 scope ?: CodeWhispererConstants . CodeAnalysisScope
@@ -254,7 +253,7 @@ export class ZipUtil {
254253 } )
255254 }
256255 if ( gitDiffContent ) {
257- zip . addFile ( ZipConstants . codeDiffFilePath , Buffer . from ( gitDiffContent , 'utf-8' ) )
256+ zip . writeString ( gitDiffContent , ZipConstants . codeDiffFilePath )
258257 }
259258 }
260259
@@ -403,7 +402,7 @@ export class ZipUtil {
403402 }
404403
405404 protected async processSourceFiles (
406- zip : admZip ,
405+ zip : ZipStream ,
407406 languageCount : Map < CodewhispererLanguage , number > ,
408407 projectPaths : string [ ] | undefined ,
409408 useCase : FeatureUseCase
@@ -444,7 +443,7 @@ export class ZipUtil {
444443 }
445444 }
446445
447- protected processOtherFiles ( zip : admZip , languageCount : Map < CodewhispererLanguage , number > ) {
446+ protected processOtherFiles ( zip : ZipStream , languageCount : Map < CodewhispererLanguage , number > ) {
448447 for ( const document of vscode . workspace . textDocuments
449448 . filter ( ( document ) => document . uri . scheme === 'file' )
450449 . filter ( ( document ) => vscode . workspace . getWorkspaceFolder ( document . uri ) === undefined ) ) {
@@ -474,7 +473,7 @@ export class ZipUtil {
474473 }
475474
476475 protected processTextFile (
477- zip : admZip ,
476+ zip : ZipStream ,
478477 uri : vscode . Uri ,
479478 fileContent : string ,
480479 languageCount : Map < CodewhispererLanguage , number > ,
@@ -493,10 +492,10 @@ export class ZipUtil {
493492 this . _totalLines += fileContent . split ( ZipConstants . newlineRegex ) . length
494493
495494 this . incrementCountForLanguage ( uri , languageCount )
496- zip . addFile ( zipEntryPath , Buffer . from ( fileContent , 'utf-8' ) )
495+ zip . writeString ( fileContent , zipEntryPath )
497496 }
498497
499- protected async processBinaryFile ( zip : admZip , uri : vscode . Uri , zipEntryPath : string ) {
498+ protected async processBinaryFile ( zip : ZipStream , uri : vscode . Uri , zipEntryPath : string ) {
500499 const fileSize = ( await fs . stat ( uri . fsPath ) ) . size
501500
502501 if (
@@ -508,7 +507,7 @@ export class ZipUtil {
508507 this . _pickedSourceFiles . add ( uri . fsPath )
509508 this . _totalSize += fileSize
510509
511- zip . addLocalFile ( uri . fsPath , path . dirname ( zipEntryPath ) )
510+ zip . writeFile ( uri . fsPath , path . dirname ( zipEntryPath ) )
512511 }
513512
514513 protected incrementCountForLanguage ( uri : vscode . Uri , languageCount : Map < CodewhispererLanguage , number > ) {
0 commit comments