@@ -21,7 +21,7 @@ describe('zipStream', function () {
2121 await fs . delete ( tmpDir , { recursive : true } )
2222 } )
2323
24- it ( 'Should create a zip stream from text content' , async function ( ) {
24+ it ( 'should create a zip stream from text content' , async function ( ) {
2525 const zipStream = new ZipStream ( { hashAlgorithm : 'md5' } )
2626 await zipStream . writeString ( 'foo bar' , 'file.txt' )
2727 const result = await zipStream . finalize ( )
@@ -39,7 +39,29 @@ describe('zipStream', function () {
3939 assert . strictEqual ( result . sizeInBytes , ( await fs . stat ( zipPath ) ) . size )
4040 } )
4141
42- it ( 'Should create a zip stream from file' , async function ( ) {
42+ it ( 'should create a zip stream from binary content' , async function ( ) {
43+ const zipStream = new ZipStream ( { hashAlgorithm : 'md5' } )
44+ await zipStream . writeData ( Buffer . from ( 'foo bar' ) , 'file.txt' )
45+ const result = await zipStream . finalize ( )
46+
47+ const zipBuffer = result . streamBuffer . getContents ( )
48+ assert . ok ( zipBuffer )
49+
50+ const zipPath = path . join ( tmpDir , 'test.zip' )
51+ await fs . writeFile ( zipPath , zipBuffer )
52+ const expectedMd5 = crypto
53+ . createHash ( 'md5' )
54+ . update ( await fs . readFileBytes ( zipPath ) )
55+ . digest ( 'base64' )
56+ assert . strictEqual ( result . hash , expectedMd5 )
57+ assert . strictEqual ( result . sizeInBytes , ( await fs . stat ( zipPath ) ) . size )
58+
59+ const zipContents = await ZipStream . unzip ( zipBuffer )
60+ assert . strictEqual ( zipContents . length , 1 )
61+ assert . strictEqual ( zipContents [ 0 ] . filename , 'file.txt' )
62+ } )
63+
64+ it ( 'should create a zip stream from file' , async function ( ) {
4365 const testFilePath = path . join ( tmpDir , 'test.txt' )
4466 await fs . writeFile ( testFilePath , 'foo bar' )
4567
@@ -71,4 +93,14 @@ describe('zipStream', function () {
7193 const zipEntries = await ZipStream . unzip ( zipBuffer )
7294 assert . strictEqual ( zipEntries [ 0 ] . filename , 'file.txt' )
7395 } )
96+
97+ it ( 'should write contents to file' , async function ( ) {
98+ const zipStream = new ZipStream ( )
99+ await zipStream . writeString ( 'foo bar' , 'file.txt' )
100+ await zipStream . writeData ( Buffer . from ( 'foo bar' ) , 'file2.txt' )
101+ const zipPath = path . join ( tmpDir , 'test.zip' )
102+ const result = await zipStream . finalizeToFile ( path . join ( tmpDir , 'test.zip' ) )
103+
104+ assert . strictEqual ( result . sizeInBytes , ( await fs . stat ( zipPath ) ) . size )
105+ } )
74106} )
0 commit comments