pxb1988/dex2jar · error · IOException

File too small to be a dex/zip

Error message

File too small to be a dex/zip

What it means

ZipUtil.readDex(byte[]) throws IOException when the input byte array is shorter than 3 bytes, since at least 3 bytes are required to test the 'dex' magic or 'PK' zip prefix before extracting classes.dex. The data cannot be a dex or zip, so it is rejected up front.

Solutions

  1. Verify the source file exists and its size > 0 before reading it into bytes.
  2. Re-download/re-extract the APK.
  3. Ensure the byte array is fully populated from the input stream.
  4. Validate data.length before calling readDex.

Example fix

// before
byte[] dex = ZipUtil.readDex(data);
// after
if (data == null || data.length < 3) {
    throw new IllegalArgumentException("input too small: " + (data == null ? -1 : data.length));
}
byte[] dex = ZipUtil.readDex(data);
Defensive patterns

Strategy: validation

Validate before calling

if (data == null || data.length < 3) {
    throw new IllegalArgumentException("cannot readDex from " + (data == null ? "null" : data.length + " bytes"));
}

Try / catch

try {
    byte[] dex = ZipUtil.readDex(data);
} catch (IOException e) {
    LOG.error("readDex failed on undersized input: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling ZipUtil.readDex(data) with an empty or <3-byte array, typically from an empty file, truncated read, or a failed fetch that produced no bytes.

Common situations: Reading a 0-byte APK from a failed download; passing a stream read that returned -1/empty; handing the wrong buffer to readDex.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pxb1988/dex2jar@b5bda4fb49 (2026-09-08). Data as JSON: /api/errors/76bc555d02e96fa2. Report an issue: GitHub.

Appendix: source

Thrown at dex-reader/src/main/java/com/googlecode/d2j/reader/zip/ZipUtil.java:75

    public static byte[] readDex(Path file) throws IOException {
        return readDex(Files.readAllBytes(file));
    }

    public static byte[] readDex(InputStream in) throws IOException {
        return readDex(toByteArray(in));
    }

    /**
     * read the dex file from byte array, if the byte array is a zip stream, it will return the content of classes.dex
     * in the zip stream.
     * 
     * @param data
     * @return the content of classes.dex
     * @throws IOException
     */
    public static byte[] readDex(byte[] data) throws IOException {
        if (data.length < 3) {
            throw new IOException("File too small to be a dex/zip");
        }
        if ("dex".equals(new String(data, 0, 3, StandardCharsets.ISO_8859_1))) {// dex
            return data;
        } else if ("PK".equals(new String(data, 0, 2, StandardCharsets.ISO_8859_1))) {// ZIP
            try (ZipFile zipFile = new ZipFile(data)) {
                ZipEntry classes = zipFile.findFirstEntry("classes.dex");
                if (classes != null) {
                    return toByteArray(zipFile.getInputStream(classes));
                } else {
                    throw new IOException("Can not find classes.dex in zip file");
                }
            }
        }
        throw new IOException("the src file not a .dex or zip file");
    }
}

View on GitHub (pinned to b5bda4fb49)