{"record":{"id":"418851950f95c632","repo":"SonarSource/sonarqube","slug":"can-not-zip-directory-dir-to-file-tofile","errorCode":null,"errorMessage":"Can not zip directory ${dir} to file ${toFile}","messagePattern":"Can not zip directory (.+?) to file (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java","lineNumber":270,"sourceCode":"  public void zipDirOrThrowIOE(File dir, File toFile) throws IOException {\n    checkOrThrowIOE(dir.exists(), \"Directory %s does not exist\", dir);\n    checkOrThrowIOE(dir.isDirectory(), \"File %s exists but is not a directory\", dir);\n    ZipUtils.zipDir(dir, toFile);\n  }\n\n  /**\n   * Zips the directory {@code dir} to the file {@code toFile}. If {@code toFile} is overridden\n   * if it exists, else it is created.\n   *\n   * @throws IllegalStateException if {@code dir} is a not directory\n   * @throws IllegalStateException if {@code dir} does not exist\n   * @throws IllegalStateException if {@code toFile} can not be created\n   */\n  public void zipDir(File dir, File toFile) {\n    try {\n      zipDirOrThrowIOE(dir, toFile);\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Can not zip directory \" + dir + \" to file \" + toFile, e);\n    }\n  }\n\n  /**\n   * Creates specified directory if it does not exist yet and any non existing parent.\n   *\n   * @throws IllegalStateException if specified File exists but is not a directory\n   * @throws IllegalStateException if directory creation failed\n   */\n  public void createDir(File dir) {\n    Path dirPath = requireNonNull(dir, \"dir can not be null\").toPath();\n    if (dirPath.toFile().exists()) {\n      checkState(dirPath.toFile().isDirectory(), \"%s is not a directory\", dirPath);\n    } else {\n      try {\n        createDirectories(dirPath);\n      } catch (IOException e) {\n        throw new IllegalStateException(format(\"Failed to create directory %s\", dirPath), e);","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java#L252-L288","documentation":"Files2.zipDir wraps any IOException from zipping a directory into an IllegalStateException, treating the failure as fatal for the CE task. It means the directory could not be read or the output zip file could not be written.","triggerScenarios":"Calling zipDir(dir, toFile) when dir does not exist or is unreadable, toFile cannot be created (missing parent, permissions), or disk is full during write.","commonSituations":"Backing up or packaging analysis working directories; read-protected files inside dir; output volume out of space; toFile path points at an existing directory.","solutions":["Verify the source directory exists and is readable","Check the output file's parent directory exists and is writable","Ensure sufficient disk space","Inspect the wrapped IOException cause for the exact failing path"],"exampleFix":"// before\nfiles2.zipDir(dir, toFile);\n// after\nif (!dir.isDirectory() || !dir.canRead()) {\n  throw new IllegalStateException(\"Directory missing or unreadable: \" + dir);\n}\nfiles2.zipDir(dir, toFile);","handlingStrategy":"try-catch","validationCode":"if (!dir.isDirectory() || !dir.canRead()) {\n  throw new IllegalArgumentException(\"Source dir missing or unreadable: \" + dir);\n}\nFile parent = toFile.getAbsoluteFile().getParentFile();\nif (parent == null || !parent.canWrite()) {\n  throw new IllegalArgumentException(\"Output parent not writable: \" + parent);\n}","typeGuard":"boolean canZip(File src, File out) {\n  return src != null && src.isDirectory() && src.canRead()\n    && out != null && !out.isDirectory()\n    && out.getAbsoluteFile().getParentFile().canWrite();\n}","tryCatchPattern":"try {\n  files2.zipDir(dir, toFile);\n} catch (IllegalStateException e) {\n  LOGGER.error(\"Zip failed for {} -> {}\", dir, toFile, e.getCause());\n  // delete partial toFile and report failure\n}","preventionTips":["Ensure source directory is readable before zipping","Reserve enough disk space for the output file","Never point toFile at an existing directory","Check filesystem is mounted read-write"],"tags":["io","zip","filesystem"],"backgroundTag":"file-write-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}