11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4+ import * as fs from 'node:fs' ;
5+ import * as path from 'node:path' ;
6+ import * as crypto from 'node:crypto' ;
7+ import * as zlib from 'node:zlib' ;
48import type { ITerminal } from '@rushstack/terminal/lib/ITerminal' ;
5- import * as fs from 'fs' ;
6- import * as path from 'path' ;
7- import * as crypto from 'crypto' ;
8- import * as zlib from 'zlib' ;
99import { type IReadonlyPathTrieNode , LookupByPath } from '@rushstack/lookup-by-path/lib/LookupByPath' ;
1010import { crc32Builder } from './crc32' ;
1111import { DISPOSE_SYMBOL , getDisposableFileHandle , type IDisposableFileHandle } from './disposableFileHandle' ;
@@ -24,7 +24,8 @@ import {
2424 type ZipMetaCompressionMethod ,
2525 type IEndOfCentralDirectory ,
2626 type ICentralDirectoryHeaderParseResult ,
27- type IFileEntry
27+ type IFileEntry ,
28+ dosDateTime
2829} from './zipUtils' ;
2930
3031const METADATA_FILENAME : string = '__zipsync_metadata__.json' ;
@@ -192,7 +193,13 @@ export function zipSync<T extends IZipSyncOptions>(
192193 }
193194 currentOffset += offset ;
194195 }
196+ function writeChunksToZip ( chunks : Uint8Array [ ] ) : void {
197+ for ( const chunk of chunks ) {
198+ writeChunkToZip ( chunk ) ;
199+ }
200+ }
195201
202+ const dosDateTimeNow : { time : number ; date : number } = dosDateTime ( new Date ( ) ) ;
196203 function writeFileEntry ( relativePath : string ) : IFileEntry {
197204 function isLikelyAlreadyCompressed ( filename : string ) : boolean {
198205 return LIKELY_COMPRESSED_EXTENSION_REGEX . test ( filename . toLowerCase ( ) ) ;
@@ -244,10 +251,11 @@ export function zipSync<T extends IZipSyncOptions>(
244251 crc32 : 0 ,
245252 sha1Hash : '' ,
246253 localHeaderOffset : currentOffset ,
247- compressionMethod
254+ compressionMethod,
255+ dosDateTime : dosDateTimeNow
248256 } ;
249257
250- writeChunkToZip ( writeLocalFileHeader ( entry ) ) ;
258+ writeChunksToZip ( writeLocalFileHeader ( entry ) ) ;
251259
252260 const sha1HashBuilder : crypto . Hash = crypto . createHash ( 'sha1' ) ;
253261 let crc32 : number = 0 ;
@@ -350,10 +358,11 @@ export function zipSync<T extends IZipSyncOptions>(
350358 crc32 : crc32Builder ( metadataBuffer ) ,
351359 sha1Hash : calculateSHA1 ( metadataBuffer ) ,
352360 localHeaderOffset : currentOffset ,
353- compressionMethod : metadataCompressionMethod
361+ compressionMethod : metadataCompressionMethod ,
362+ dosDateTime : dosDateTimeNow
354363 } ;
355364
356- writeChunkToZip ( writeLocalFileHeader ( metadataEntry ) ) ;
365+ writeChunksToZip ( writeLocalFileHeader ( metadataEntry ) ) ;
357366 writeChunkToZip ( metadataData , metadataCompressedSize ) ;
358367 writeChunkToZip ( writeDataDescriptor ( metadataEntry ) ) ;
359368
@@ -371,26 +380,15 @@ export function zipSync<T extends IZipSyncOptions>(
371380
372381 markStart ( 'pack.write.centralDirectory' ) ;
373382 const centralDirOffset : number = currentOffset ;
374- let centralDirSize : number = 0 ;
375-
376383 for ( const entry of entries ) {
377- const centralHeader : Buffer = writeCentralDirectoryHeader ( entry ) ;
378- fs . writeSync ( zipFile , centralHeader ) ;
379- centralDirSize += centralHeader . length ;
384+ writeChunksToZip ( writeCentralDirectoryHeader ( entry ) ) ;
380385 }
381- terminal . writeDebugLine (
382- `Central directory written (offset=${ centralDirOffset } , size=${ centralDirSize } )`
383- ) ;
386+ const centralDirSize : number = currentOffset - centralDirOffset ;
384387 markEnd ( 'pack.write.centralDirectory' ) ;
385388
386389 // Write end of central directory
387390 markStart ( 'pack.write.eocd' ) ;
388- const endOfCentralDir : Buffer = writeEndOfCentralDirectory (
389- centralDirOffset ,
390- centralDirSize ,
391- entries . length
392- ) ;
393- fs . writeSync ( zipFile , endOfCentralDir ) ;
391+ writeChunkToZip ( writeEndOfCentralDirectory ( centralDirOffset , centralDirSize , entries . length ) ) ;
394392 terminal . writeDebugLine ( 'EOCD record written' ) ;
395393 markEnd ( 'pack.write.eocd' ) ;
396394 } finally {
0 commit comments