From 0cef40a279f9e6255aed06464df7679f0d521230 Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Thu, 11 Aug 2022 12:40:05 +0200 Subject: [PATCH] allow cloning EntryIterator objects at the current position --- package-lock.json | 4 ++-- package.json | 2 +- src/Archive/Entry/EntryIterator.js | 9 +++++++++ test/archive.test.js | 25 +++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 395098f..5faf858 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "armarius", - "version": "1.3.3", + "version": "1.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "armarius", - "version": "1.3.3", + "version": "1.5.0", "license": "MIT", "dependencies": { "pako": "^2.0.4" diff --git a/package.json b/package.json index c8a6038..dca6230 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "armarius", - "version": "1.4.4", + "version": "1.5.0", "description": "A JavaScript library to read, write, and merge ZIP archives in web browsers.", "repository": "github:aternosorg/armarius", "type": "module", diff --git a/src/Archive/Entry/EntryIterator.js b/src/Archive/Entry/EntryIterator.js index bf43b0a..a3b1d9b 100644 --- a/src/Archive/Entry/EntryIterator.js +++ b/src/Archive/Entry/EntryIterator.js @@ -33,6 +33,15 @@ export default class EntryIterator { this.currentEntry = BigInt(0); } + /** + * @return {Promise} + */ + async clone() { + let cloneReader = await this.reader.clone(); + cloneReader.seek(this.reader.offset); + return new this.constructor(this.archive, cloneReader, false); + } + /** * @returns {Promise} */ diff --git a/test/archive.test.js b/test/archive.test.js index 464721e..bd128c7 100644 --- a/test/archive.test.js +++ b/test/archive.test.js @@ -158,3 +158,28 @@ test('Merge archives', async () => { expect(entries.find(e => e.fileNameString === 'file1.txt')).toBeInstanceOf(ArchiveEntry); expect(entries.find(e => e.fileNameString === 'file2.txt')).toBeInstanceOf(ArchiveEntry); }); + +test('Clone entry iterator', async () => { + let i = 0; + let fileName = 'file.txt'; + let fileComment = 'file-comment'; + let archive = new WriteArchive(() => { + if(i >= 2) { + return null; + } + i++; + return new DataReaderEntrySource(new ArrayBufferReader(encoder.encode(`file-content-${i}`).buffer), { + fileName: `file-${i}.txt`, + }); + }); + let data = await writeArchiveToBuffer(archive); + + let readArchive = new ReadArchive(new ArrayBufferReader(data.buffer, data.byteOffset, data.byteLength)); + await readArchive.init(); + + let entryIterator = await readArchive.getEntryIterator(); + expect((await entryIterator.next()).getFileNameString()).toBe('file-1.txt'); + + let clone = await entryIterator.clone(); + expect((await entryIterator.next()).getFileNameString()).toBe((await clone.next()).getFileNameString()); +});