Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directories are ignored in zip file #419

Closed
jackdewhurst opened this issue Feb 24, 2022 · 2 comments · Fixed by #515
Closed

Directories are ignored in zip file #419

jackdewhurst opened this issue Feb 24, 2022 · 2 comments · Fixed by #515

Comments

@jackdewhurst
Copy link

jackdewhurst commented Feb 24, 2022

I've got a zip file created from https://makeappicon.com. It consists of three subdirectories each containing some image files and folders.

Screenshot 2022-02-24 at 11 54 11

When opening this .zip file with adm-zip (v0.5.9) and reading the getEntries() contents, there are no directories, just files.

Screenshot 2022-02-24 at 11 42 57

However when using extractAllTo(), the folder structure is preserved and files are extracted into subdirectories.

How to I get the files from within one subdirectory? Using extractEntryTo("ios/", false, true) returns an entry not found error.

@nitedani
Copy link

nitedani commented Sep 8, 2022

Same issue
Windows 11
Node.js v18.9.0

@direvus
Copy link

direvus commented Aug 3, 2023

Likewise, I tried to list all the child entries out of a particular directory inside the zip archive.

getEntryChildren sounded like what I was looking for, so I tried code like:

const dir = zip.getEntry(dirName);
for (const entry of zip.getEntryChildren(dir) {
  if (!entry.isDirectory) {
    ...
  }
}

But it didn't work. When I went debugging, I found that dir was null even though dirName is definitely a directory within the zip.

I think it's normal for directories to not show up as distinct "entries" in the zip archive. Maybe there's multiple ways to represent this in an archive, but certainly I'm used to seeing entries for regular files only. So this approach taken by adm-zip of first getting the directory "entry" is just plain not going to work. ZipFile.getEntryChildren will never return anything, and ZipEntry.isDirectory will always be false.

I worked around it by iterating over all the entries and testing for the directory prefix, using code like the below. Not ideal but right now I think it's the best we can do.

/*
 * Get a list of regular file entries under a directory.
 *
 * All descendants of the directory are returned, regardless of depth.
 */
export function getChildEntries(zip: Zip, dirName: string): ZipEntry[] {
  let prefix = dirName;
  if (!prefix.endsWith('/')) {
    prefix += '/';
  }
  const entries = zip.getEntries();
  if (!entries) {
    return [];
  }
  // seems like `isDirectory` can never be true anyway, but just to be safe ...
  return entries.filter(x => !x.isDirectory && x.entryName.startsWith(prefix));
}

@5saviahv 5saviahv linked a pull request Jun 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants