Skip to content

Commit

Permalink
allow cloning EntryIterator objects at the current position
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Aug 11, 2022
1 parent 98b5dd8 commit 0cef40a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/Archive/Entry/EntryIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export default class EntryIterator {
this.currentEntry = BigInt(0);
}

/**
* @return {Promise<EntryIterator>}
*/
async clone() {
let cloneReader = await this.reader.clone();
cloneReader.seek(this.reader.offset);
return new this.constructor(this.archive, cloneReader, false);
}

/**
* @returns {Promise<?ArchiveEntry>}
*/
Expand Down
25 changes: 25 additions & 0 deletions test/archive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});

0 comments on commit 0cef40a

Please sign in to comment.