shwenzhang/AndResGuard · error · DirectoryException

DirectoryException

Error message

DirectoryException

What it means

FileDirectory.getFileInputLocal wraps a java.io.FileNotFoundException thrown by new FileInputStream(...) into a DirectoryException. It means the named entry could not be opened for reading inside this on-disk directory.

Solutions

  1. Verify the file exists before reading: containsFile(name) or new File(dir, name).exists().
  2. Catch DirectoryException and inspect the cause (FileNotFoundException) to fall back to a default resource.
  3. Re-list the directory (loadDirs) to get current valid names instead of using cached names.

Example fix

// before
InputStream in = fileDir.getFileInput("res/values/strings.xml");
// after
if (fileDir.containsFile("res/values/strings.xml")) {
  InputStream in = fileDir.getFileInput("res/values/strings.xml");
} else {
  throw new IOException("missing resource: res/values/strings.xml");
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!fileDir.containsFile(name)) { /* skip or default */ }

Try / catch

try { return fileDir.getFileInput(name); } catch (DirectoryException e) { if (e.getCause() instanceof FileNotFoundException) return null; throw e; }

Prevention

When it happens

Trigger: Calling getFileInput(name) on a FileDirectory for a name that does not map to an existing file on disk, or the file was deleted between listing and reading.

Common situations: Reading a resource entry that a prior obfuscation step renamed or removed; name mismatch after mapping changes; TOCTOU races in build pipelines.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of shwenzhang/AndResGuard@e4df245d82 (2026-09-12). Data as JSON: /api/errors/a29aed0f98caeea8. Report an issue: GitHub.

Appendix: source

Thrown at AndResGuard-core/src/main/java/com/tencent/mm/directory/FileDirectory.java:53

    if (!dir.isDirectory()) {
      throw new DirectoryException("file must be a directory: " + dir);
    }
    mDir = dir;
  }

  @Override
  protected AbstractDirectory createDirLocal(String name) throws DirectoryException {
    File dir = new File(generatePath(name));
    dir.mkdir();
    return new FileDirectory(dir);
  }

  @Override
  protected InputStream getFileInputLocal(String name) throws DirectoryException {
    try {
      return new FileInputStream(generatePath(name));
    } catch (FileNotFoundException e) {
      throw new DirectoryException(e);
    }
  }

  @Override
  protected OutputStream getFileOutputLocal(String name) throws DirectoryException {
    try {
      return new FileOutputStream(generatePath(name));
    } catch (FileNotFoundException e) {
      throw new DirectoryException(e);
    }
  }

  @Override
  protected void loadDirs() {
    loadAll();
  }

  @Override

View on GitHub (pinned to e4df245d82)