2
2
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
- import admZip from 'adm-zip'
6
5
import * as vscode from 'vscode'
7
6
import path from 'path'
8
7
import { tempDirPath , testGenerationLogsDir } from '../../shared/filesystemUtilities'
@@ -25,6 +24,7 @@ import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/proces
25
24
import { ProjectZipError } from '../../amazonqTest/error'
26
25
import { removeAnsi } from '../../shared/utilities/textUtilities'
27
26
import { normalize } from '../../shared/utilities/pathUtils'
27
+ import { ZipStream } from '../../shared/utilities/zipStream'
28
28
29
29
export interface ZipMetadata {
30
30
rootDir : string
@@ -109,7 +109,7 @@ export class ZipUtil {
109
109
if ( ! uri ) {
110
110
throw new NoActiveFileError ( )
111
111
}
112
- const zip = new admZip ( )
112
+ const zip = new ZipStream ( )
113
113
114
114
const content = await this . getTextContent ( uri )
115
115
@@ -121,14 +121,14 @@ export class ZipUtil {
121
121
// Set includeWorkspaceFolder to false because we are already manually prepending the projectName
122
122
const relativePath = vscode . workspace . asRelativePath ( uri , false )
123
123
const zipEntryPath = this . getZipEntryPath ( projectName , relativePath )
124
- zip . addFile ( zipEntryPath , Buffer . from ( content , 'utf-8' ) )
124
+ zip . writeString ( content , zipEntryPath )
125
125
126
126
if ( scope === CodeWhispererConstants . CodeAnalysisScope . FILE_ON_DEMAND ) {
127
127
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 )
129
129
}
130
130
} else {
131
- zip . addFile ( uri . fsPath , Buffer . from ( content , 'utf-8' ) )
131
+ zip . writeString ( content , uri . fsPath )
132
132
}
133
133
134
134
this . _pickedSourceFiles . add ( uri . fsPath )
@@ -139,7 +139,7 @@ export class ZipUtil {
139
139
throw new FileSizeExceededError ( )
140
140
}
141
141
const zipFilePath = this . getZipDirPath ( FeatureUseCase . CODE_SCAN ) + CodeWhispererConstants . codeScanZipExt
142
- zip . writeZip ( zipFilePath )
142
+ await zip . finalizeToFile ( zipFilePath )
143
143
return zipFilePath
144
144
}
145
145
@@ -170,13 +170,13 @@ export class ZipUtil {
170
170
* await processMetadataDir(zip, '/path/to/directory');
171
171
* ```
172
172
*/
173
- protected async processMetadataDir ( zip : admZip , metadataDir : string ) {
173
+ protected async processMetadataDir ( zip : ZipStream , metadataDir : string ) {
174
174
const metadataDirName = path . basename ( metadataDir )
175
175
// Helper function to add empty directory to zip
176
176
const addEmptyDirectory = ( dirPath : string ) => {
177
177
const relativePath = path . relative ( metadataDir , dirPath )
178
178
const pathWithMetadata = path . join ( metadataDirName , relativePath , '/' )
179
- zip . addFile ( pathWithMetadata , Buffer . from ( '' ) )
179
+ zip . writeString ( '' , pathWithMetadata )
180
180
}
181
181
182
182
// Recursive function to process directories
@@ -189,11 +189,10 @@ export class ZipUtil {
189
189
190
190
if ( fileType === vscode . FileType . File ) {
191
191
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 )
194
193
const relativePath = path . relative ( metadataDir , filePath )
195
194
const pathWithMetadata = path . join ( metadataDirName , relativePath )
196
- zip . addFile ( pathWithMetadata , buffer )
195
+ zip . writeFile ( fileUri . fsPath , pathWithMetadata )
197
196
} catch ( error ) {
198
197
getLogger ( ) . error ( `Failed to add file ${ filePath } to zip: ${ error } ` )
199
198
}
@@ -207,7 +206,7 @@ export class ZipUtil {
207
206
}
208
207
209
208
protected async zipProject ( useCase : FeatureUseCase , projectPath ?: string , metadataDir ?: string ) {
210
- const zip = new admZip ( )
209
+ const zip = new ZipStream ( )
211
210
let projectPaths = [ ]
212
211
if ( useCase === FeatureUseCase . TEST_GENERATION && projectPath ) {
213
212
projectPaths . push ( projectPath )
@@ -232,12 +231,12 @@ export class ZipUtil {
232
231
}
233
232
this . _language = [ ...languageCount . entries ( ) ] . reduce ( ( a , b ) => ( b [ 1 ] > a [ 1 ] ? b : a ) ) [ 0 ]
234
233
const zipFilePath = this . getZipDirPath ( useCase ) + CodeWhispererConstants . codeScanZipExt
235
- zip . writeZip ( zipFilePath )
234
+ await zip . finalizeToFile ( zipFilePath )
236
235
return zipFilePath
237
236
}
238
237
239
238
protected async processCombinedGitDiff (
240
- zip : admZip ,
239
+ zip : ZipStream ,
241
240
projectPaths : string [ ] ,
242
241
filePath ?: string ,
243
242
scope ?: CodeWhispererConstants . CodeAnalysisScope
@@ -254,7 +253,7 @@ export class ZipUtil {
254
253
} )
255
254
}
256
255
if ( gitDiffContent ) {
257
- zip . addFile ( ZipConstants . codeDiffFilePath , Buffer . from ( gitDiffContent , 'utf-8' ) )
256
+ zip . writeString ( gitDiffContent , ZipConstants . codeDiffFilePath )
258
257
}
259
258
}
260
259
@@ -403,7 +402,7 @@ export class ZipUtil {
403
402
}
404
403
405
404
protected async processSourceFiles (
406
- zip : admZip ,
405
+ zip : ZipStream ,
407
406
languageCount : Map < CodewhispererLanguage , number > ,
408
407
projectPaths : string [ ] | undefined ,
409
408
useCase : FeatureUseCase
@@ -444,7 +443,7 @@ export class ZipUtil {
444
443
}
445
444
}
446
445
447
- protected processOtherFiles ( zip : admZip , languageCount : Map < CodewhispererLanguage , number > ) {
446
+ protected processOtherFiles ( zip : ZipStream , languageCount : Map < CodewhispererLanguage , number > ) {
448
447
for ( const document of vscode . workspace . textDocuments
449
448
. filter ( ( document ) => document . uri . scheme === 'file' )
450
449
. filter ( ( document ) => vscode . workspace . getWorkspaceFolder ( document . uri ) === undefined ) ) {
@@ -474,7 +473,7 @@ export class ZipUtil {
474
473
}
475
474
476
475
protected processTextFile (
477
- zip : admZip ,
476
+ zip : ZipStream ,
478
477
uri : vscode . Uri ,
479
478
fileContent : string ,
480
479
languageCount : Map < CodewhispererLanguage , number > ,
@@ -493,10 +492,10 @@ export class ZipUtil {
493
492
this . _totalLines += fileContent . split ( ZipConstants . newlineRegex ) . length
494
493
495
494
this . incrementCountForLanguage ( uri , languageCount )
496
- zip . addFile ( zipEntryPath , Buffer . from ( fileContent , 'utf-8' ) )
495
+ zip . writeString ( fileContent , zipEntryPath )
497
496
}
498
497
499
- protected async processBinaryFile ( zip : admZip , uri : vscode . Uri , zipEntryPath : string ) {
498
+ protected async processBinaryFile ( zip : ZipStream , uri : vscode . Uri , zipEntryPath : string ) {
500
499
const fileSize = ( await fs . stat ( uri . fsPath ) ) . size
501
500
502
501
if (
@@ -508,7 +507,7 @@ export class ZipUtil {
508
507
this . _pickedSourceFiles . add ( uri . fsPath )
509
508
this . _totalSize += fileSize
510
509
511
- zip . addLocalFile ( uri . fsPath , path . dirname ( zipEntryPath ) )
510
+ zip . writeFile ( uri . fsPath , path . dirname ( zipEntryPath ) )
512
511
}
513
512
514
513
protected incrementCountForLanguage ( uri : vscode . Uri , languageCount : Map < CodewhispererLanguage , number > ) {
0 commit comments